?
Solved

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

Posted on 2006-05-19
5
Medium Priority
?
206 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:Dynotoe
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Expert1701
ID: 16722941
Would you give the following a try?

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

Author Comment

by:Dynotoe
ID: 16725061

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
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 2000 total points
ID: 16725227
Okay, let's try the following:

 SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers WHERE CustomerID = 'XX';", cn); //replace XX with a valid ID

And if that works, try:

 SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers WHERE CustomerID = '" + ID.Replace("'", "''") + "';", cn);
0
 

Author Comment

by:Dynotoe
ID: 16725506
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??...

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21852752.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
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16725591
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...
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question