0

I have created my own web services to consume. I added service reference. However at the store_getProducts , an error occured at XDocument xReturn = XDocument.Load(e.Result); it says the string was not recognized as a valid uri. parameter name inputuri. Can someone tell me what is the problem

SvcRefstore.ServiceSoapClient storeclient = new SvcRefstore.ServiceSoapClient();
       storeclient.getProductsAllCompleted += new EventHandler<SvcRefstore.getProductsAllCompletedEventArgs>(store_getProducts);
       storeclient.getProductsAllAsync();
       storeclient.setItemsarrayAsync(itemarray);

store_getProducts

 void store_getProducts(object sender, SvcRefstore.getProductsAllCompletedEventArgs e)
    {

        XDocument xReturn = XDocument.Load(e.Result);
         IEnumerable<Products> myQuote = from item in xReturn.Descendants("Products")
                                         select new Products
                                         {

                                             Name = Convert.ToString(item.Element("Name").Value),
                                             unitPrice = Convert.ToString(item.Element("unitPrice").Value),
                                         };
         Products thisQuote = myQuote.ElementAt(0);
         textBlock1.Text = thisQuote.Name.ToString();
    }

below is the getProducts method i created to consume

 public string getProductsAll()
{
    storeDBCon dbConn;
    dbConn = new storeDBCon();

    DataSet idataset = new DataSet();
    string products = "";

    SqlConnection db = dbConn.GetConnection();

    SqlCommand cmd;
    string sqlRetrieve = "SELECT * FROM Product WHERE Name = @Name";

    db.Open();
    try
    {
        for (int i = 0; i < itemsarray.Length; i ++ )

         //   foreach (String item in itemsarray)
            {
                cmd = new SqlCommand(sqlRetrieve, db);

                cmd.Parameters.AddWithValue("@Name", itemsarray[i]);

                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows == true)
                {
                    reader.Read();
                    //_userId = reader.GetInt32(0);
                    productID = reader.GetInt32(0);
                    pname = reader.GetString(1);
                    pDesc = reader.GetString(2);
                    unitPrice = reader.GetString(3);
                    imagefilename = reader.GetString(4);

                    products = productID + "," +pname + "," + pDesc + "," + unitPrice + "," + imagefilename + "";
                }
                else

                    products = "Null";

            }


    }
    catch (Exception ex)
    {
        errMsg = ex.Message;
    }
    finally
    {
        db.Close();

    }


    return products;

}

below is the setItemsarray method

public void setItemsarray(string[] _str)
    {
        itemsarray = _str;
    }
FredHomme
  • 153
  • 3
  • 3
  • 10

2 Answers2

1

There are several things wrong here, first of all, if you want to load a string into a XDocument, you need to call XDocument doc = XDocument.Parse(string) not XDocument.Load().

Second of all though, your getProductsAll() web service method is just building a comma delimited string. What makes you think you can load this into an XML document? You need to return a valid XML document from getProductsAll()

EkoostikMartin
  • 6,831
  • 2
  • 33
  • 62
0

As I explained in my previous answer, use XDocument.Load when you specify an url where to load content and XDocument.Parse to load xml content in a string.

In your case, you have to rewrite the code like this :

void store_getProducts(object sender, SvcRefstore.getProductsAllCompletedEventArgs e)
{

    XDocument xReturn = XDocument.Parse(e.Result); // Change is here
     IEnumerable<Products> myQuote = from item in xReturn.Descendants("Products")
                                     select new Products
                                     {

                                         Name = Convert.ToString(item.Element("Name").Value),
                                         unitPrice = Convert.ToString(item.Element("unitPrice").Value),
                                     };
     Products thisQuote = myQuote.ElementAt(0);
     textBlock1.Text = thisQuote.Name.ToString();
}

[edit]: after digging a bit, I can see that you server code does not return XML, but a simple string concatenation of values... You will have other errors later.

Community
  • 1
  • 1
Steve B
  • 36,818
  • 21
  • 101
  • 174
  • i change from parse to load in order to see the error i am getting. So what should i return in order to consume the webservice method? – FredHomme Jan 10 '13 at 16:17
  • or what should i do in order to consume the webservice method? – FredHomme Jan 10 '13 at 16:17
  • I think the simplest way is to create a custom serializable class that will hold your results, and return this kind of object on your webmethod. – Steve B Jan 10 '13 at 16:20