Home Page
Looking for a better way to learn C# and ASP.NET? Take a look at the source code for a working program and see how the pieces all fit together.
- - -
Click here to read more and take a free video tour. This is a great deal, act now and learn tricks you can use to improve your own programs.

Category: Advanced ASP.NET

Click here to download the web service project source code.
Click here to download the web service client project source code.

In this lesson I am going to teach you how to update an ASP.NET C# application that uses a web service whenever there are changes to the web service application.  In the previous lessons of this category, I had created an ASP.NET web service that retrieves data from a database table and returns the list in a web service method named getCustomers().  I had also created an ASP.NET client web application that calls and uses the web service to display a list of data on a web form.

There may be situations where you want or need to make modifications to the back-end web service that your web application is calling.  In fact, when you are developing an ASP.NET C# application that has a web service tier, you may be making frequent updates to the web service back-end during the development process.

Examples of updates to the back-end web service that I are applicable to this lesson are:

  • Changing the web service method signature (e.g. adding new parameters to a web service method or changing the return type of a web service method)
  • Adding a new web service method
  • Removing an existing web service method
  • Renaming an existing web service method

All of these types of updates mentioned above can require that a client application become aware of the changes.  In fact, if you change something like a web service method signature and you don’t update (or refresh) an ASP.NET web service client application to reflect the changes, you could end up with both compile time and runtime errors.  I will show you examples of these types of errors in this lesson so that you can fix the problems if they arise during your own web service and client application development process.

There are also situations where you DON’T have to update (or refresh) an ASP.NET web service client application if the back-end web service changes.  Here is an example:

  • You changed the logic in a web service method, but the method signature did NOT change.  In other words the method still returns the same type and expects the same parameters.

Now let’s get to the actual lesson.  I am going to describe what I did in a step by step fashion.  I had a back-end web service named Lab18_csharpuniversity_webservice that contained a class named CustomerData, with a method named getCustomers():

[WebMethod]
public string getCustomers()
{
 SqlDataAdapter dbadapter = null;
 DataSet returnDS = new DataSet();

 using (SqlConnection dbconn = new SqlConnection(
     "Server=localhost;"
     + "Database=csharpuniversity;"
     + "User ID=sa;"
     + "Password=Sqlserverpass$123;"
     + "Trusted_Connection=False;"))
 {
     SqlCommand cmd = new SqlCommand();
     string sqlQuery = "select * from customer";

     cmd.CommandText = sqlQuery;
     cmd.Connection = dbconn;
     dbadapter = new SqlDataAdapter(cmd);

     dbadapter.Fill(returnDS);
 }

 return returnDS.GetXml();
}

getCustomers() queries the database and returns a list of customers in an XML string.  Let’s say that I had a requirement to make this method a little bit more dynamic and give it the capability of performing a search based on the customer’s last name.  Due to this new requirement, I added a string parameter named pLastName to the method.  Then I added the database search code inside the method by using an SqlParameter object.  I added a query parameter named @last_name and then put that in the SqlCommand parameters collection so that the query will use it.  Here is what the updated version of the method looked like:

[WebMethod]
public string getCustomers(string pLastName)
{
        SqlDataAdapter dbadapter = null;
        DataSet returnDS = new DataSet();

        using (SqlConnection dbconn = new SqlConnection(
            "Server=localhost;"
            + "Database=csharpuniversity;"
            + "User ID=sa;"
            + "Password=Sqlserverpass$123;"
            + "Trusted_Connection=False;"))
        {
            SqlCommand cmd = new SqlCommand();
            SqlParameter param;
            string sqlQuery = "select * from customer ";

            if (pLastName.Length > 0)
            {
                sqlQuery += "where last_name = @last_name";

                param = cmd.CreateParameter();
                param.DbType = System.Data.DbType.String;
                param.ParameterName = "@last_name";
                param.Value = pLastName;
                cmd.Parameters.Add(param);
            }

            cmd.CommandText = sqlQuery;
            cmd.Connection = dbconn;
            dbadapter = new SqlDataAdapter(cmd);

            dbadapter.Fill(returnDS);
        }

        return returnDS.GetXml();
}

Now that this method was updated, I was able to test my web service with Visual Web Developer and make sure that it works.  I tested by executing the web service method both with and without a last name input argument value.  If a last name is not supplied, the method was supposed to return all of the customers in the database.  Click here to watch a video that shows what web service changes I made and shows how I tested the web service after the code changed.  Notice that when I supplied a last name value, only the records that match that last name were returned by the web service.

Since this update changed the method signature of getCustomers() in the back-end web service, it qualifies as one of the reasons that you would have to update (or refresh) an ASP.NET client web application.  Remember the bullets that I had at the beginning of this lesson.  Let’s explore what happens if you don’t update the files in the ASP.NET client web application.

Before starting this lesson I had previously created an ASP.NET website project named Lab18_csharpuniversity_webserviceclient.  That project contained a web reference to the old version of the getCustomers() method.  Remember that the old version of the method did NOT contain any input arguments so the ASP.NET client web application had the following code to call the web service:

CustomerWebService.CustomerData customerdata =
            new CustomerWebService.CustomerData();
        string dataXML = customerdata.getCustomers();

When I tried to run this code after the back-end web service was changed, I received the following error:
System.Web.Services.Protocols.SoapException: Server was unable to process request.
System.NullReferenceException: Object reference not set to an instance of an object
 at CustomerData.getCustomers(String pLastName)

Click here to watch a video that shows exactly what happened when I ran the client project and the SoapException error coming up.

Next, in the Lab18_csharpuniversity_webserviceclient project I decided to change the web form GridView_SQLServer.aspx and add a TextBox so that the user can type in an optional last name as search criteria.  I updated the click event code for the Show Customers button as follows:

CustomerWebService.CustomerData customerdata =
            new CustomerWebService.CustomerData();
        string dataXML = customerdata.getCustomers(txtLastName.Text);

Notice that I am passing the value from the txtLastName text box as an argument to the getCustomers web service method.  When I tried to run this code, I received the following error from the compiler:
No overload for method ‘getCustomers’ takes ‘1′ arguments

Now that error message wouldn’t even let me compile the application.  Click here to watch a video that shows how I added the new TextBox and tried to call the new version of the web service method.  The reason that the compiler was throwing the error message is because whenever you Add a Web Reference to your ASP.NET application in Visual Web Developer or Visual Studio, the IDE actually hits the web service, obtains a definition for all of the web service methods, and stores this definition in some files inside your project.  You should see a folder that gets created in your project called App_WebReferences.  In that folder, there are some IDE generated files that contain the web service definition.

Since the web service changed in the back-end project Lab18_csharpuniversity_webservice, I would then have to update the definition of that service in the client project files of Lab18_csharpuniversity_webserviceclient.  The proper way to do this is by right clicking on the App_WebReferences folder in your client ASP.NET project and choosing the option to “Update Web/Service References”.  This will cause the IDE to go and grab the latest version of the back-end web service definition file.  Click here to see a video that shows how I refreshed my client ASP.NET application with the most up to date web service definition and tested my new search web form

When you right click on the App_WebReferences folder and update the web references, you are essentially updating the definition for all of the web services that your ASP.NET application is using.  This means that if you had references to several different web services, it will go to each and every one of them to get an updated set of definitions.  This may not be necessary in some situations where you just want to update ONE web service definition (i.e. the one that you know has changed).

If you only want to update a particular web service definition, you can expand the App_WebReferences folder and right click on the individual service class you need to refresh, then choose “Update Web Reference”.  Click here to watch a video that shows how I updated an individual web service class reference for my CustomerData web service class.

So what kind of web services are you calling in your ASP.NET application?

Did you like this lesson? Enter your Email


You get notified anytime a new article is posted and you can unsubscribe at anytime.

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment