Wednesday, May 23, 2012

How to Create a Webservice and How can we use it in .aspx Page?


What are Web Services?
Web services are small units of code
Web services are designed to handle a limited set of tasks
Web services use XML based communicating protocols(HTTP,XML,SOAP,WSDL,UDDI)
Web services are independent of operating systems
Web services are independent of programming languages
Web services connect people, systems and devices

Benefits of Web Services
Easier to communicate between applications
Easier to reuse existing services
Easier to distribute information to more consumers
Rapid development

Steps to Create a Web Service.

      Here I am going to show you demo of how to Retrive data using web services and display it on to the aspx Page.
  •  Create a Project called SampleWebSevice
  • Go to Solution Explorer right click on Project and Click on Add - > New Item.Under Web Option you find Web Service Select it and name it Service1.asmx and add it into the Project.
  • Now Create a Table Name Office with following Field.
         
  • Now Add the Method in Service1.asmx.cs Page
  • [WebMethod]

        public XmlElement GetUserDetails(string officename)
        {
            SqlConnection con = new SqlConnection(@"Data Source=kartikpatel\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
            con.Open();
            string s = "select * from Office where OfficeName like '%" + officename + "%'";
            SqlDataAdapter da = new SqlDataAdapter(s, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            XmlDataDocument xmldata = new XmlDataDocument(ds);
            XmlElement xmlElement = xmldata.DocumentElement;
            return xmlElement;
        }

  • Now Build solution and Host this service on your PC in iis.and name the virtual directory WebService  for this service.Now browse it and test this service.
  • Download WebService Sample from here......
    Download
  • Now Create a New Project Name SampleWebService  to use the Webservice that is created before.
  • Now Right Click On Project in solution explorer. and Click on Add Web Reference you will shown the below screen and then add Url in that which we configure in iis.(http://localhost/WebService/Service1.asmx).
         
  • Add Web Reference Name OfficeList and Click On Add Reference.
  • Now Add the aspx page by right click on project in solution explorer.
  • Now Add the GridView in Default.aspx Page.

 <table>
            <tr>
                <td>
                    <b>Enter OfficeName:</b>
                </td>
                <td>
                    <asp:TextBox ID="txtOfficeName" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
    </div>
    <div>
        <asp:GridView ID="gvUserDetails" runat="server" EmptyDataText="No Record Found">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

Now Add code in aspx.cs page.
using System.Xml;
using System.Data;

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserDetails("");
            }
        }
        protected void BindUserDetails(string userName)
        {
            OfficeList.Service1 objofficeDetails = new OfficeList.Service1();

            DataSet dsresult = new DataSet();
            XmlElement exelement = objofficeDetails.GetUserDetails(userName);
            if (exelement != null)
            {
                XmlNodeReader nodereader = new XmlNodeReader(exelement);
                dsresult.ReadXml(nodereader, XmlReadMode.Auto);
                gvUserDetails.DataSource = dsresult;
                gvUserDetails.DataBind();
            }
            else
            {
                gvUserDetails.DataSource = null;
                gvUserDetails.DataBind();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BindUserDetails(txtOfficeName.Text);
        }
  • Now run this Page.
  • Here i have use the XmlElement as the Return Type of Method in web service.Thats why i have to take the data from xmlelement to dataset in code.

Feel free if you have any confusion.

Download Code from here......
Download


1 comments:

sagar viradiya said...

it is very help full site for solution in .net!!!

Post a Comment