Click here to Skip to main content
15,886,626 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a XML method, and I am trying reference the return from xml string from CreateXML() into the WRequest() method, but I am keep experiencing the following error -- An object reference is required for the non-static field, method, or property 'MailSender.createXMLPub()'.

C#
    public static string WRequest(string URL, string method, string postData)
    {
        URL = "################";
        method = "POST";
		// how to read the xml data into this method
        postData = createXMLPub();

        string responseData = "";
        try
        {
            if (hwrequest.Method == "POST")
            {  
            }
            // Attempt to receive the WebResponse to the WebRequest.
            using (HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse())
            {
                if (hwresponse != null)
                { // If we have valid WebResponse then read it.
                    using (StreamReader reader = new StreamReader(hwresponse.GetResponseStream()))
                    {
                        reader.Close();
                    }
                }
                hwresponse.Close();
            }
        }
        catch (Exception e)
        {
            responseData = "An error occurred: " + e.Message;
        }
        return responseData;
    }

    // Updated Method - currently giving new errors.
    public static void createXMLPub(string data )
    {
        XElement xeRoot = new XElement("pub");
        XElement xeName = new XElement("name", "###");
        xeRoot.Add(xeName);
        XElement xeCategory = new XElement("category", "#####");
        xeRoot.Add(xeCategory);
		XDocument xDoc = new XDocument(xeRoot);
        xDoc.Save(Server.MapPath("products-string2.xml"));
   
        //Response.Redirect("products-string2.xml");//load web browser
        //return xDoc;

        data = xDoc.ToString();
        return data;
    }
}

New errors:
1. No overload for method 'createXMLPub' takes 0 arguments

2.An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get
'

Any advice, would be most welcomed. Thank you for your help and time.
Posted
Updated 9-Sep-14 0:03am
v3
Comments
[no name] 9-Sep-14 7:42am    
And you are trying return a string from a method that is marked as a void method.

It looks like you are calling a non static property from a static method. You will need to either make the property static,
 
Share this answer
 
Comments
Herman<T>.Instance 9-Sep-14 5:49am    
createXMLPub is not static method but it is called from a static method. That's the problem
Thanks7872 9-Sep-14 5:51am    
Please post this as solution. Thats the correct one.
vr reddy 9-Sep-14 5:51am    
try...
public static void createXMLPub()
miss786 9-Sep-14 6:01am    
Hi, Apology for the late response. I had previously tried this but this approach gave me sadly more compiling errors, such as
1. No overload for method 'createXMLPub' takes 0 arguments

2.An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'

To the code below:
public static void createXMLPub(string data )
{
XElement xeRoot = new XElement("pub");
XElement xeName = new XElement("name", "###");
xeRoot.Add(xeName);
XElement xeCategory = new XElement("category", "#####");
xeRoot.Add(xeCategory);
XDocument xDoc = new XDocument(xeRoot);
xDoc.Save(Server.MapPath("products-string2.xml"));

//Response.Redirect("products-string2.xml");//load web browser
//return xDoc;

data = xDoc.ToString();
return data;
}

Please advice. Thank you.
Herman<T>.Instance 9-Sep-14 7:26am    
Method createXMLPub requires String Object looking to your method
postData = createXMLPub(<your string="" object="" here="">);

error 2 a which line?
1. No overload for method 'createXMLPub' takes 0 arguments

Ans: This is your method
public static void createXMLPub(string data )

you are using the above method like this
postData = createXMLPub();

where is your parameter passing in above method
so it should be like this
postData = createXMLPub("yourdata");


2. An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'

Ans: In this method you are not passing data
public static void createXMLPub(string data )

and calling this method at
public static string WRequest(string URL, string method, string postData)

like
postData = createXMLPub();

here postData is a string type and the method createXMLPub is returning string value but method signature is like void so your createXMLPub() method should be like this
public static string createXMLPub(string data )
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900