Link to home
Start Free TrialLog in
Avatar of Dynotoe
Dynotoe

asked on

C# - WebService. I am having issue extracting data by plugging a value into a web service help page textbox in the Browser.

Hi everyone,

I built a project as part of a book learning VS2003.net.  The chapter is concerned with creating a web Service.  There are 3 methods that work on my virtual local host except for the one that takes a parameter via a textbox on the web service help page and the invoke button for it. Here are 2 methods.  The first one works fine and poulates the XML from the query which take no partamters and fills the returned browser page.  The second method which is very similar doesn't return anything.  I'm clueless.  Here are the 2 methods...


[WebMethod]
            public  DataSet GetCustomers()
            {

                  // Get Connection
                  SqlConnection cn = new SqlConnection
                        (@"database=northwind;server=.\NetSDK;Integrated Security=SSPI");

                  // Get DataAdapter
                  SqlDataAdapter da = new SqlDataAdapter("SELECT * from Customers", cn);
                  
                  // Create a DataSet
                  DataSet ds = new DataSet();

                  // Fill DataSet
                  da.Fill(ds, "Customers");

                  // Return DatSet
                  return ds;
            }


AND this one which doesn't work...

// This Does NOT WORK!!!!
            [WebMethod]
            public DataSet GetCustomersByID( string ID )
            {
                  // Get Connection
                  SqlConnection cn = new SqlConnection
                        (@"database=northwind;server=.\NetSDK;Integrated Security=SSPI");
                  
                  // Get DataAdapter
                  SqlDataAdapter da = new SqlDataAdapter("SELECT * from Customers where CustomerID = @CustomerID", cn);
                  
                  da.SelectCommand.Parameters.Add("@CustomersID", SqlDbType.VarChar, 30).Value = ID;
                  

                  // Create new DataSet
                  DataSet ds = new DataSet();

                  // Fill DataSet
                  da.Fill(ds, "Customers");

                  // Return DataSet
                  return ds;
            }

I have this in my web config file...
   <!--  GLOBALIZATION
          This section sets the globalization settings of the application.
    -->
    <globalization
            requestEncoding="utf-8"
            responseEncoding="utf-8"
   />
      <webServices>
            <protocols>
                  <add name="HttpSoap1.2"/>
                  <add name="HttpSoap"/>
                  <add name="HttpPost"/>
                  <add name="HttpGet"/>
                  <add name="HttpPostLocalhost"/>
                  <add name="Documentation"/>
            </protocols>
      </webServices>
      
 </system.web>

It appears like the string is being passed into the method.

Let me please express my gratitude for you help in advance.

Best regards,

Dynotoe
Avatar of Expert1701
Expert1701

Would you give the following a try?

  da.SelectCommand.Parameters.Add("@CustomersID", System.Data.SqlDbType.VarChar, 30);
  da.SelectCommand.Parameters["@CustomersID"].Value = ID;
Avatar of Dynotoe

ASKER


Hi Expert1701,

Well no luck.  However I think it's this line...

SqlDataAdapter da = new SqlDataAdapter("SELECT * from Customers where CustomerID = @CustomerID", cn);

becasue when I comment it out and place the following line in it's place I end up getting all the Customers returned in XML even though I type in ALFKI into the textbox befoe hitting "Invoke"...

// Get DataAdapter
                  //SqlDataAdapter da = new SqlDataAdapter("SELECT * from Customers where CustomerID = @CustomerID", cn);
                  SqlDataAdapter da = new SqlDataAdapter("SELECT * from Customers", cn);

What do you think?


Best,

Dynotoe
ASKER CERTIFIED SOLUTION
Avatar of Expert1701
Expert1701

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dynotoe

ASKER

Hi Ex,

That's great!!!  It works now.  Now my example projects which consume the XML can be written.  :)  What was the problem?  Would you like to try your hand at this question of mine which has been open for a while and nobody seems to want to answer??...

https://www.experts-exchange.com/questions/21852752/C-How-do-I-get-a-XML-schema-from-a-feed-I-get-over-the-internet-which-currently-populates-a-non-typed-table-in-my-code.html

I hate when I do problems in a book and they don't work! Makes learning very difficult lol :)

Thanks once again.  Very much appreciated.


Best,

Dynotoe
If you want to learn the proper use of web services in a software development environment then STAY AWAY from using datasets.  Also look into contract first development...