Web Services in ASP.Net C# With SQL Server

Sameer Gaikwad
6 min readFeb 16, 2022

--

Follow me Sameer Gaikwad then follow the link below! 👉 💯day challenge 📩 Reach out to me on Twitter or Linkedin, or Blogspot if you want to discuss this further. :)

Github link download

check out my blog post

REST API Design Rulebook

Web Services are components on an internet server that a client application can call by making HTTP requests across the online. ASP.NET enables you to make custom Web Services or to use built-in application services and call these services from any client application.

Web Services communicate using the subsequent standard web protocols and data formats:

  • HTTP
  • XML
  • SOAP

The following are some terms we frequently use with Web Services.

Understanding Web Services: XML, WSDL, SOAP, and UDDI (Independent Technology Guides)

SOAP: Simple Object Access Protocol. SOAP is much by which method calls are translated into XML format and sent via HTTP.

WSDL: Web Service Description Language. WSDL contains every detail regarding the utilization of Web Services and method and properties provided by Web Service and URLs from which those methods are often accessed and data types used.

UDDI: Universal Description, Discovery, and Integration (UDDI) may be a directory service where businesses can register and look for Web Services.

Discovery or.Disco Files: The DISCO file typically points to a WSDL source that successively points to the actual Web Service when one searches for the web Services

Now we’ll see the way to create and consume Web Services in ASP.Net C#. The following is my data table in Design Mode from which I will be able to fetch data using the online Service:

check out my blog post

Image 1.

The following is the script of my data table:

  1. CREATE TABLE [dbo].[Employee](
  2. [Emp_ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Designation] [varchar](50) NULL,
  5. [City] [varchar](50) NULL,
  6. [State] [varchar](50) NULL,
  7. [Country] [varchar](50) NULL,
  8. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  9. (
  10. [Emp_ID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO

The following is the data in my table:

Image 2.

Now open Visual Studio then select File -> New Web Site-> ASP.NET Web Service.

Image 3.

Now open Service.cs inside the App_Code folder to write your WebMethod as in the following:

Image 4.

The following is the App_Code/Service.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. using System.Xml;
  9. [WebService(Namespace = “http://tempuri.org/")]
  10. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  12. // [System.Web.Script.Services.ScriptService]
  13. public class Service : System.Web.Services.WebService
  14. {
  15. public Service()
  16. {
  17. //Uncomment the following line if using designed components
  18. //InitializeComponent();
  19. }
  20. [WebMethod]
  21. public XmlElement GetEmployeeSearchResult(string EMP_Name)
  22. {
  23. using (SqlConnection con = new SqlConnection(@”Data Source=DESKTOP-318KFSS\SQLEXPRESS; Initial Catalog=sameer; Integrated Security=True;”))
  24. {
  25. using (SqlCommand cmd = new SqlCommand(“SELECT * FROM EMPLOYEE WHERE Name like @EMP_Name+’%’”, con))
  26. {
  27. con.Open();
  28. cmd.Parameters.AddWithValue(“@EMP_Name”, EMP_Name);
  29. cmd.ExecuteNonQuery();
  30. SqlDataAdapter da = new SqlDataAdapter(cmd);
  31. DataSet ds = new DataSet();
  32. da.Fill(ds);
  33. con.Close();
  34. XmlDataDocument xmldata = new XmlDataDocument(ds);
  35. XmlElement xmlElement = xmldata.DocumentElement;
  36. return xmlElement;
  37. }
  38. }
  39. }
  40. }

Now to check your Web Service. So run your application:

Image 5.

Now click on your Method name:

Image 6.

Image 7.

Now to consume this Web Service. To create a new ASP.NET Web Forms application.

Now open Visual Studio then select File -> New -> Project-> ASP.NET Web Forms Application.

Image 8.

Now right-click on your application in the Solution Explorer then select Add Web Reference.

Image 9.

Type your Web Service URL and click GO. Then click on the Add Reference button:

check out my blog post

Image 10.

The following is the aspx:

  1. <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
  2. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns=”http://www.w3.org/1999/xhtml">
  4. <head runat=”server”>
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id=”form1" runat=”server”>
  9. <div>
  10. <table style=”border: solid 15px #d4ebef; width: 100%; vertical-align: central;”>
  11. <tr>
  12. <td style=”padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: yellow;
  13. text-align: center; font-family: Verdana; font-size: 20pt; color: red;”>
  14. Web Services In ASP.NET C#
  15. </td>
  16. </tr>
  17. <tr>
  18. <td style=”background-color: yellow; text-align: center; font-family: Verdana; font-size: 14pt;
  19. color: red;”>
  20. <b>Enter Employee Name:</b>
  21. <asp:TextBox ID=”txtEmpName” runat=”server”></asp:TextBox>
  22. <asp:Button ID=”btnSearchEmployee” runat=”server” Text=”Search Employee” OnClick=”btnSearchEmployee_Click” />
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>
  27. <table style=”width: 80%; text-align: center; vertical-align: central;”>
  28. <tr>
  29. <td style=”text-align: left;”>
  30. <asp:GridView ID=”GridViewEmployee” runat=”server” AutoGenerateColumns=”False” Width=”100%”
  31. BackColor=”White” BorderColor=”#3366CC” BorderStyle=”None” BorderWidth=”1px”
  32. CellPadding=”4">
  33. <Columns>
  34. <asp:BoundField DataField=”Name” HeaderText=”Employee Name” HeaderStyle-HorizontalAlign=”Left”>
  35. <HeaderStyle HorizontalAlign=”Left”></HeaderStyle>
  36. </asp:BoundField>
  37. <asp:BoundField DataField=”Designation” HeaderText=”Designation” HeaderStyle-HorizontalAlign=”Left”>
  38. <HeaderStyle HorizontalAlign=”Left”></HeaderStyle>
  39. </asp:BoundField>
  40. <asp:BoundField DataField=”City” HeaderText=”City” HeaderStyle-HorizontalAlign=”Left”>
  41. <HeaderStyle HorizontalAlign=”Left”></HeaderStyle>
  42. </asp:BoundField>
  43. <asp:BoundField DataField=”State” HeaderText=”State” HeaderStyle-HorizontalAlign=”Left”>
  44. <HeaderStyle HorizontalAlign=”Left”></HeaderStyle>
  45. </asp:BoundField>
  46. <asp:BoundField DataField=”Country” HeaderText=”Country” HeaderStyle-HorizontalAlign=”Left”>
  47. <HeaderStyle HorizontalAlign=”Left”></HeaderStyle>
  48. </asp:BoundField>
  49. </Columns>
  50. <FooterStyle BackColor=”#99CCCC” ForeColor=”#003399" />
  51. <HeaderStyle BackColor=”#ff5722" Font-Bold=”True” ForeColor=”#CCCCFF” />
  52. <PagerStyle ForeColor=”#003399" HorizontalAlign=”Left” BackColor=”#99CCCC” />
  53. <RowStyle BackColor=”White” ForeColor=”#003399" />
  54. <SelectedRowStyle BackColor=”#009999" Font-Bold=”True” ForeColor=”#CCFF99" />
  55. </asp:GridView>
  56. </td>
  57. </tr>
  58. </table>
  59. </td>
  60. </tr>
  61. </table>
  62. </div>
  63. </form>
  64. </body>
  65. </html>

The following is the aspx.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Xml;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack)
  14. {
  15. BindEmployee(“”);
  16. }
  17. }
  18. protected void BindEmployee(string Emp_Name)
  19. {
  20. Test.Service obj = new Test.Service();
  21. DataSet ds = new DataSet();
  22. XmlElement exElement = obj.GetEmployeeSearchResult(Emp_Name);
  23. if (exElement != null)
  24. {
  25. XmlNodeReader nodeReader = new XmlNodeReader(exElement);
  26. ds.ReadXml(nodeReader, XmlReadMode.Auto);
  27. GridViewEmployee.DataSource = ds;
  28. GridViewEmployee.DataBind();
  29. }
  30. else
  31. {
  32. GridViewEmployee.DataSource = null;
  33. GridViewEmployee.DataBind();
  34. }
  35. }
  36. protected void btnSearchEmployee_Click(object sender, EventArgs e)
  37. {
  38. BindEmployee(txtEmpName.Text);
  39. }
  40. }

Now run your application:

Image 11.

Now enter a name and click on Search.

Image 12.

Image 13.

Image 14.

#sameergaikwadbymesameergaikwad #sameergaikwadcrudoperation #sameergaikwadmvccrudoperation #sameergaikwadjoinsinsqlserver #sameergaikwadsqlstoredprocedure #sameergaikwadsqltolinq

--

--

Sameer Gaikwad

#mesameergaikwad Sameer Gaikwad on mesameergaikwad MVC web service crud operation join SQL to LINQ Stored Procedure WEB API Entity Framework