Create And Read Operations Using ASP.NET WCF Services

Agenda

In this example, we are having a student table, which contains the details of a student and the type of course, which he opted for during admission. We will insert the student details and retrieve them through our client Application, created in ASP.NET, which consumes our WCF Service.

Pre-requisite

For a basic knowledge of WCF Services, I recommend you read my previous article, which will make it easy for you to understand this example.

Step 1: Create the database tables and stored procedures.

Create a database in SQL Server, name it 'DEMODB' and execute the SQL commands, given below, to generate the tables, test data and stored procedures, which we will use in WCF Service.

  1. CREATE TABLE [dbo].[STUDENTS] ([Student_Id] INT IDENTITY(1, 1) NOT NULL, [Name] NVARCHAR(MAXNOT NULL, [Gender] NVARCHAR(10) NOT NULL, [Contact_No] NVARCHAR(12) NOT NULL, [Fees] INT NOT NULL, [DateOfJoin] DATETIME NULL, [CourseType] INT NULL, [AnnualFee] INT NULL, [MonthlyFee] INT NULLPRIMARY KEY CLUSTERED ([Student_Id] ASC));  
  2.    
  3. SET IDENTITY_INSERT [dbo].[STUDENTS] ON  
  4.    
  5. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  6. VALUES (1, N'John', N'Male', N'2310542310', 250000, N'2016-08-01 09:21:16', 1, NULL, 20000)  
  7.    
  8. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  9. VALUES (2, N'James', N'Male', N'2310542310', 250000, N'2016-08-01 09:24:25', 1, NULL, 20000)  
  10.    
  11. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  12. VALUES (3, N'Sana', N'Female', N'3302002102', 300000, N'2016-07-22 09:24:25', 2, 300000, NULL)  
  13.    
  14. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  15. VALUES (4, N'Albert', N'Male', N'5542103365', 350000, N'2016-07-12 09:24:25', 1, NULL, 30000)  
  16.    
  17. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  18. VALUES (5, N'Steve', N'Male', N'8865987455', 450000, N'2016-07-02 09:24:25', 2, 450000, NULL)  
  19.    
  20. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  21. VALUES (6, N'Stark', N'Male', N'9988656623', 200000, N'2016-08-11 09:24:25', 1, NULL, 20000)  
  22.    
  23. INSERT INTO [dbo].[STUDENTS] ([Student_Id], [Name], [Gender], [Contact_No], [Fees], [DateOfJoin], [CourseType], [AnnualFee], [MonthlyFee])  
  24. VALUES (7, N'James', N'Male', N'9899063210', 600000, N'2016-09-01 00:00:00', 2, 600000, NULL)  
  25.    
  26. SET IDENTITY_INSERT [dbo].[STUDENTS] OFF  
  27.    
  28. CREATE PROCEDURE uspGetStudentDetails @StudentId INT  
  29. AS  
  30. BEGIN  
  31.     SELECT *  
  32.     FROM dbo.Students  
  33.     WHERE Student_id = @StudentId  
  34. END  
  35.    
  36. CREATE PROCEDURE [dbo].[uspInsertStudentDetails] (@Name NVARCHAR(max), @Gender NVARCHAR(10), @Contact_No NVARCHAR(12), @Fees INT, @DateOfJoin DATETIME, @CourseType INT, @AnnualFee INT = NULL, @MonthlyFee INT = NULL)  
  37. AS  
  38. BEGIN  
  39.     INSERT INTO [dbo].[STUDENTS]  
  40.     VALUES (@Name, @Gender, @Contact_No, @Fees, @DateOfJoin, @CourseType, @AnnualFee, @MonthlyFee)  
  41. END  

Explanation

  • We have created a table having Student details and inserted some sample data in it.
  • uspGetStudentDetails will be used in the Service to retrieve the details of the students on the basis of his StudentId.
  • uspInsertStudentDetails will be used to insert the student details and student ID is the identity column. Ths, you don't have to pass student ID, while saving the details.

Step 2: Create a new project in Visual Studio of type Class Library.

We will create our WCF Service in this project. The steps are given below to create the new Class Library Project in Visual Studio.

new Project

Step 3: Remove any class files, which are created by default in this project and Add a new class file, named 'student.cs', as shown below:

Add

Add

Step 4: Add the code, given below, in this class file:

  1. using System;  
  2. using System.Runtime.Serialization;  
  3.    
  4. namespace studentService  
  5. {  
  6.     [KnownType(typeof(ShortTerm)), KnownType(typeof(LongTerm)), DataContract]  
  7.     public class Student  
  8.     {  
  9.         [DataMember(Order = 1)]  
  10.         public int StudentId { getset; }  
  11.    
  12.         [DataMember(Order = 2)]  
  13.         public string Name { getset; }  
  14.    
  15.         [DataMember(Order = 3)]  
  16.         public string Gender { getset; }  
  17.    
  18.         [DataMember(Order = 4)]  
  19.         public string ContactNo { getset; }  
  20.    
  21.         [DataMember(Order = 5)]  
  22.         public int Fees { getset; }  
  23.    
  24.         [DataMember(Order = 6)]  
  25.         public DateTime DateOfJoin { getset; }  
  26.    
  27.         [DataMember(Order = 7)]  
  28.         public CourseType Type { getset; }  
  29.     }  
  30.    
  31.     [DataContract]  
  32.     public enum CourseType  
  33.     {  
  34.         [EnumMember] ShortTerm = 1,  
  35.    
  36.         [EnumMember] LongTerm = 2  
  37.     }  
  38. }  
Explanation
  • We have created a class, named Student, having 7 DataMembers ordered in a serialized format. These DataMembers corresponds to our database table columns.
  • The Student Class is decorated with a KnownType attribute, which will tell the class, which is using the concept of an Inheritance.
  • An Enum is created, which has two values, which corresponds to the type of courses that the student opted for. These enum values are also decorated with EnumMember attributes, which tells the Service to serialize them as well.

Step 5: Add two more class files, named 'LongTerm.cs' and 'ShortTerm.cs' and add the code, given below, to them:

  1. namespace studentService  
  2. {  
  3.     public class LongTerm : Student  
  4.     {  
  5.         public int AnnualFee { getset; }  
  6.     }  
  7. }  
  1. namespace studentService  
  2. {  
  3.     public class ShortTerm : Student  
  4.     {  
  5.         public int MonthlyFee { getset; }  
  6.     }  
  7. }  
Explanation

The above two classes are inheriting the base class Student, which we created in Step 4. If the student opted for LongTerm CourseType, AnnualFee will be applicable for him, else for ShortTem CourseType, MonthlyFee will be applicable.

Step 6: Now, add a WCF Service Library to this project, as shown below. After adding this, the two files will be added to this project. One is the Service file and another is the interface.

Add

Add

After adding the above, your project will look like, as shown below:

project

Step 7: Add the code, given below, to the Interface file:

  1. using System.ServiceModel;  
  2.    
  3. namespace studentService  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IStudentService  
  7.     {  
  8.         [OperationContract]  
  9.         Student GetStudentDetails(int studentId);  
  10.    
  11.         [OperationContract]  
  12.         void InsertStudentDetails(Student student);  
  13.     }  
  14. }   

Explanation

There are two operation contracts. One will fetch the data on the basis of StudentId and other will insert the data in the database.

Step 8: Now, we will implement these two interface Methods in the Service file. Replace the code, given below: 

  1. namespace studentService  
  2. {  
  3.     using System;  
  4.     using System.Configuration;  
  5.     using System.Data;  
  6.     using System.Data.SqlClient;  
  7.    
  8.     public class StudentService : IStudentService  
  9.     {  
  10.         private readonly string _cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  11.    
  12.         public Student GetStudentDetails(int studentId)  
  13.         {  
  14.             try  
  15.             {  
  16.                 var student = new Student();  
  17.                 using (var con = new SqlConnection(_cs))  
  18.                 {  
  19.                     var cmd = new SqlCommand("uspGetStudentDetails", con) {CommandType = CommandType.StoredProcedure};  
  20.                     cmd.Parameters.AddWithValue("@StudentId", studentId);  
  21.                     con.Open();  
  22.                     var dr = cmd.ExecuteReader();  
  23.                     while (dr.Read())  
  24.                     {  
  25.                         if ((CourseType) dr["CourseType"] == CourseType.LongTerm)  
  26.                         {  
  27.                             student = new LongTerm  
  28.                             {  
  29.                                 StudentId = Convert.ToInt32(dr["STUDENT_ID"]),  
  30.                                 Name = dr[1].ToString(),  
  31.                                 Gender = dr[2].ToString(),  
  32.                                 ContactNo = dr[3].ToString(),  
  33.                                 Fees = Convert.ToInt32(dr[4]),  
  34.                                 DateOfJoin = Convert.ToDateTime(dr[5]),  
  35.                                 Type = CourseType.LongTerm,  
  36.                                 AnnualFee = Convert.ToInt32(dr["AnnualFee"])  
  37.                             };  
  38.                         }  
  39.                         else  
  40.                         {  
  41.                             student = new ShortTerm()  
  42.                             {  
  43.                                 StudentId = Convert.ToInt32(dr["STUDENT_ID"]),  
  44.                                 Name = dr[1].ToString(),  
  45.                                 Gender = dr[2].ToString(),  
  46.                                 ContactNo = dr[3].ToString(),  
  47.                                 Fees = Convert.ToInt32(dr[4]),  
  48.                                 DateOfJoin = Convert.ToDateTime(dr[5]),  
  49.                                 Type = CourseType.ShortTerm,  
  50.                                 MonthlyFee = Convert.ToInt32(dr["MonthlyFee"])  
  51.                             };  
  52.                         }  
  53.                     }  
  54.    
  55.                     return student;  
  56.                 }  
  57.             }  
  58.             catch (Exception exception)  
  59.             {  
  60.                 throw exception.InnerException;  
  61.             }  
  62.         }  
  63.    
  64.         public void InsertStudentDetails(Student student)  
  65.         {  
  66.             try  
  67.             {  
  68.                 using (var con = new SqlConnection(_cs))  
  69.                 {  
  70.                     var cmd = new SqlCommand("uspInsertStudentDetails", con)  
  71.                     {  
  72.                         CommandType = CommandType.StoredProcedure  
  73.                     };  
  74.                     cmd.Parameters.AddWithValue("@Name", student.Name);  
  75.                     cmd.Parameters.AddWithValue("@Gender", student.Gender);  
  76.                     cmd.Parameters.AddWithValue("@Contact_No", student.ContactNo);  
  77.                     cmd.Parameters.AddWithValue("@Fees", student.Fees);  
  78.                     cmd.Parameters.AddWithValue("@DateOfJoin", student.DateOfJoin);  
  79.                     cmd.Parameters.AddWithValue("@CourseType", student.Type);  
  80.                     if (student.GetType() == typeof(LongTerm))  
  81.                     {  
  82.                         cmd.Parameters.AddWithValue("@AnnualFee", ((LongTerm) student).AnnualFee);  
  83.                     }  
  84.                     else  
  85.                     {  
  86.                         cmd.Parameters.AddWithValue("@MonthlyFee", ((ShortTerm) student).MonthlyFee);  
  87.                     }  
  88.    
  89.                     con.Open();  
  90.                     cmd.ExecuteNonQuery();  
  91.                 }  
  92.             }  
  93.             catch (Exception exception)  
  94.             {  
  95.                 throw exception.InnerException;  
  96.             }  
  97.         }  
  98.     }  
  99. }   

Explanation

  • At first, we are creating the Connection string to the database.
  • In GetStudentDetails Method, we implement the stored procedure and pass StudentId as the parameter. If the course type in the database is of type LongTerm, Service will return AnnualFee field, else MonthlyFee.
  • In InsertStudentDetails method, we implement the SP and pass all the details as the parameters. Also, if the coursetype is LongTerm, AnnualFee will be sent to the database, else MonthlyFee.

Now, we are done with the Service implementation.

Now, lets host the Service, using a Console Application.

Step 9: Add a new project to the same solution of type Console Application, as shown below:

Add

Add

Step 10: Add the reference to the WCF Service project and System.ServiceModel assembly is shown below:

Add

Add

Add

Step 11: Add a config file to add the endpoints for the client Application and database connectionStrings.

Add

Add

Step 12: Add the code, given below, to Program.cs file.

  1. using System;  
  2. using System.ServiceModel;  
  3.    
  4. namespace studentServiceHost  
  5. {  
  6.     public class Program  
  7.     {  
  8.         public static void Main(string[] args)  
  9.         {  
  10.             using (var host = new ServiceHost(typeof(studentService.StudentService)))  
  11.             {  
  12.                 host.Open();  
  13.                 Console.WriteLine($"Host Successfully started at : {DateTime.Now}");  
  14.                 Console.ReadLine();  
  15.             }  
  16.         }  
  17.     }  
  18. }  

The code, given above starts the host in the Console Application.

Step 13: Add the following code to App.config file.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <configuration>  
  4.   <connectionStrings>  
  5.     <add name="DBCS"  
  6.          connectionString="data source=.;Integrated Security=SSPI;database=DEMODB"  
  7.          providerName="System.Data.SqlClient" />  
  8.   </connectionStrings>  
  9.    
  10.   <system.serviceModel>  
  11.     <behaviors>  
  12.       <serviceBehaviors>  
  13.         <behavior name="">  
  14.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
  15.         </behavior>  
  16.       </serviceBehaviors>  
  17.     </behaviors>  
  18.     <services>  
  19.       <service name="studentService.StudentService">  
  20.         <endpoint address="" binding="basicHttpBinding" contract="studentService.IStudentService">  
  21.           <identity>  
  22.             <dns value="localhost" />  
  23.           </identity>  
  24.         </endpoint>  
  25.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
  26.         <host>  
  27.           <baseAddresses>  
  28.             <add baseAddress="http://localhost:8733/studentService/StudentService/" />  
  29.           </baseAddresses>  
  30.         </host>  
  31.       </service>  
  32.     </services>  
  33.   </system.serviceModel>  
  34. </configuration>  
The configuration, given above, is already explained in detail in my previous article about WCF Services.

Step 14: Now, build the project, go to the root directory of the project and move to bin folder, as shown below and run the EXE file.

Add

Step 15: Now, lets create the client Application, which will consume this WCF Service host. Follow the steps, given below:

Add

Add

Add

Now, add a Webform to it.

Webform

Step 16: Add a reference to the Service host, as shown below. Make sure that Host is running in Console Application. This will generate proxy classes to the Service.

Add

Add

Step 17: Add the bootstrap reference from NuGet package library to create our Webform.

Add

Add

Step 18: Now, replace the code of Webform1.aspx with the code, given below:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="studentApp.WebForm1" %>  
  2.    
  3. <!DOCTYPE html>  
  4.    
  5. <html lang="en">  
  6. <head runat="server">  
  7.     <title>Student App</title>  
  8.     <link href="Content/bootstrap.css" rel="stylesheet"/>  
  9. </head>  
  10. <body>  
  11. <form id="form1" runat="server">  
  12.     <div class="container">  
  13.         <div class="jumbotron">  
  14.             <div class="form-group" role="form">  
  15.                 <div class="form-group">  
  16.                     <h1>Student Details</h1>  
  17.                 </div>  
  18.                 <div class="form-group">  
  19.                     <label>Id</label>  
  20.                     <asp:TextBox ID="txtId" runat="server" CssClass="form-control"></asp:TextBox>  
  21.                 </div>  
  22.                 <div class="form-group">  
  23.                     <label>Name</label>  
  24.                     <asp:TextBox ID="txtName" runat="server" CssClass="form-control"></asp:TextBox>  
  25.                 </div>  
  26.                 <div class="form-group">  
  27.                     <label>Gender</label>  
  28.                     <asp:TextBox ID="txtGender" runat="server" CssClass="form-control"></asp:TextBox>  
  29.                 </div>  
  30.                 <div class="form-group">  
  31.                     <label>Contact No</label>  
  32.                     <asp:TextBox ID="txtContact" runat="server" CssClass="form-control"></asp:TextBox>  
  33.                 </div>  
  34.                 <div class="form-group">  
  35.                     <label>Fees</label>  
  36.                     <asp:TextBox ID="txtFees" runat="server" CssClass="form-control"></asp:TextBox>  
  37.                 </div>  
  38.                 <div class="form-group">  
  39.                     <label>Date of Admission</label>  
  40.                     <asp:TextBox ID="txtDOJ" runat="server" CssClass="form-control"></asp:TextBox>  
  41.                 </div>  
  42.                 <div class="form-group">  
  43.                     <label>Course Type</label>  
  44.                     <asp:DropDownList ID="ddlCourseType" runat="server" AutoPostBack="True" CssClass="form-control" OnSelectedIndexChanged="ddlCourseType_SelectedIndexChanged">  
  45.                         <asp:ListItem Text="Select Course Type" Value="-1">  
  46.                         </asp:ListItem>  
  47.                         <asp:ListItem Text="Short Term" Value="1">  
  48.                         </asp:ListItem>  
  49.                         <asp:ListItem Text="Long Term" Value="2">  
  50.                         </asp:ListItem>  
  51.                     </asp:DropDownList>  
  52.                 </div>  
  53.                 <div class="form-group" visible="false" runat="server" id="divAnnualFee">  
  54.                     <label>Annual Fee</label>  
  55.                     <asp:TextBox ID="txtAnnualFee" runat="server" CssClass="form-control"></asp:TextBox>  
  56.                 </div>  
  57.                 <div class="form-group" visible="false" runat="server" id="divMonthlyFee">  
  58.                     <label>Monthly Fee</label>  
  59.                     <asp:TextBox ID="txtMonthlyFee" runat="server" CssClass="form-control"></asp:TextBox>  
  60.                 </div>  
  61.                 <div class="form-group">  
  62.                     <asp:Button ID="btnAdd" runat="server" Text="Save" CssClass="btn btn-primary" OnClick="btnAdd_Click"/>  
  63.                     <asp:Button ID="btnGet" runat="server" Text="Retreive" CssClass="btn btn-info" OnClick="btnGet_Click"/>  
  64.                 </div>  
  65.                 <div runat="server" id="divNotify" visible="False">  
  66.                 </div>  
  67.             </div>  
  68.         </div>  
  69.     </div>  
  70.    
  71. </form>  
  72. </body>  
  73. </html>  
 Step 19: Now, replace the code of Webform1.aspx.cs file.
  1. using System;  
  2. using System.Web.UI;  
  3. using studentApp.StudentService;  
  4.    
  5. namespace studentApp  
  6. {  
  7.     public partial class WebForm1 : Page  
  8.     {  
  9.         protected void Page_Load(object sender, EventArgs e)  
  10.         {  
  11.         }  
  12.    
  13.         protected void btnAdd_Click(object sender, EventArgs e)  
  14.         {  
  15.             var client = new StudentServiceClient();  
  16.    
  17.             switch ((CourseType) Convert.ToInt32(ddlCourseType.SelectedValue))  
  18.             {  
  19.                 case CourseType.LongTerm:  
  20.                 {  
  21.                     var student = new LongTerm()  
  22.                     {  
  23.                         Name = txtName.Text,  
  24.                         Gender = txtGender.Text,  
  25.                         ContactNo = txtContact.Text,  
  26.                         Fees = Convert.ToInt32(txtFees.Text),  
  27.                         DateOfJoin = Convert.ToDateTime(txtDOJ.Text),  
  28.                         AnnualFee = Convert.ToInt32(txtAnnualFee.Text),  
  29.                         Type = CourseType.LongTerm  
  30.                     };  
  31.                     client.InsertStudentDetails(student);  
  32.                     divNotify.Visible = true;  
  33.                     divNotify.Attributes.Add("class""alert alert-success visible");  
  34.                     divNotify.InnerText = "Hooray! Data Saved Successfully";  
  35.                     txtName.Text = "";  
  36.                     txtGender.Text = "";  
  37.                     txtContact.Text = "";  
  38.                     txtFees.Text = "";  
  39.                     txtDOJ.Text = "";  
  40.                     txtMonthlyFee.Text = "";  
  41.                     txtAnnualFee.Text = "";  
  42.                 }  
  43.    
  44.                     break;  
  45.                 case CourseType.ShortTerm:  
  46.                 {  
  47.                     var student = new ShortTerm()  
  48.                     {  
  49.                         Name = txtName.Text,  
  50.                         Gender = txtGender.Text,  
  51.                         ContactNo = txtContact.Text,  
  52.                         Fees = Convert.ToInt32(txtFees.Text),  
  53.                         DateOfJoin = Convert.ToDateTime(txtDOJ.Text),  
  54.                         MonthlyFee = Convert.ToInt32(txtMonthlyFee.Text),  
  55.                         Type = CourseType.ShortTerm  
  56.                     };  
  57.                     client.InsertStudentDetails(student);  
  58.                     divNotify.Visible = true;  
  59.                     divNotify.Attributes.Add("class""alert alert-success visible");  
  60.                     divNotify.InnerText = "Hooray! Data Saved Successfully";  
  61.                     txtName.Text = "";  
  62.                     txtGender.Text = "";  
  63.                     txtContact.Text = "";  
  64.                     txtFees.Text = "";  
  65.                     txtDOJ.Text = "";  
  66.                     txtMonthlyFee.Text = "";  
  67.                     txtAnnualFee.Text = "";  
  68.                 }  
  69.    
  70.                     break;  
  71.                 default:  
  72.                     divNotify.Visible = true;  
  73.                     divNotify.Attributes.Add("class""alert alert-danger visible");  
  74.                     divNotify.InnerText = "Please Select Course Type";  
  75.                     break;  
  76.             }  
  77.         }  
  78.    
  79.         protected void btnGet_Click(object sender, EventArgs e)  
  80.         {  
  81.             var client = new StudentServiceClient();  
  82.             var student = client.GetStudentDetails(Convert.ToInt32(txtId.Text));  
  83.    
  84.             if (student.Type == CourseType.LongTerm)  
  85.             {  
  86.                 txtAnnualFee.Text = ((LongTerm) student).AnnualFee.ToString();  
  87.                 divAnnualFee.Visible = true;  
  88.                 divMonthlyFee.Visible = false;  
  89.             }  
  90.             else  
  91.             {  
  92.                 txtMonthlyFee.Text = ((ShortTerm) student).MonthlyFee.ToString();  
  93.                 divMonthlyFee.Visible = true;  
  94.                 divAnnualFee.Visible = false;  
  95.             }  
  96.    
  97.             ddlCourseType.SelectedValue = ((int) student.Type).ToString();  
  98.             txtName.Text = student.Name;  
  99.             txtGender.Text = student.Gender;  
  100.             txtContact.Text = student.ContactNo;  
  101.             txtFees.Text = student.Fees.ToString();  
  102.             txtDOJ.Text = student.DateOfJoin.ToShortDateString();  
  103.             divNotify.Visible = true;  
  104.             divNotify.Attributes.Add("class""alert alert-success visible");  
  105.             divNotify.InnerText = "Hooray! Data Retreived Successfully";  
  106.         }  
  107.    
  108.         protected void ddlCourseType_SelectedIndexChanged(object sender, EventArgs e)  
  109.         {  
  110.             switch (ddlCourseType.SelectedValue)  
  111.             {  
  112.                 case "-1":  
  113.                     divAnnualFee.Visible = false;  
  114.                     divMonthlyFee.Visible = false;  
  115.                     break;  
  116.                 case "1":  
  117.                     divMonthlyFee.Visible = true;  
  118.                     divAnnualFee.Visible = false;  
  119.                     break;  
  120.                 default:  
  121.                     divMonthlyFee.Visible = false;  
  122.                     divAnnualFee.Visible = true;  
  123.                     break;  
  124.             }  
  125.         }  
  126.     }  
  127. }  
Explanation
  • In the above code, we are referencing studentService proxy class generated by Service reference.
  • Now, we have all the Members and attributes of the Service.
  • This code simply invokes the interface methods and displays the data in HTML controls.

Step 20: Now, we can also trace the SOAP messages, being exchanged by the service between the client and database. Follow the steps, given below, to enable the tracing.

Add

Add

The steps, listed above, will add Logging files to the project directory and the path is clearly shown in point 3 and 4 above.

Step 21: Now, press Ctrl + F5 and make sure that the host is running. The output will be given below:

output
Step 22: Now, lets check the Trace logs.

logs

Below is the Request sent to the database to save the data.

data

Below, is the request sent to the Server for processing.

data

Below is the response back from the Service. All are in XML format.

data

Up Next
    Ebook Download
    View all
    Learn
    View all