Powered by Blogger.

Sunday, March 2, 2014

How to send data through SoapHeader in webservices in Asp.net?

In this post I will explain how to send user data through SoapHeader in webservices.
Also you can check out:

- Get nth highest lowest salary in SQL Server 2008

- Delete all stored procedures at once in SQL Server database

- Increase column size in sql server 2008

Steps to do:
1.  Create a class named securityInfo which contains user credentials that inherits SoapHeader class.
2.  Send securityInfo object in the SoapHeader attribute of the webmethod.
3.  Set MustUnderstand property to true to insists that SoapHeader must understood the data.
4.  Use DidUnderstand property to check whether XML webservice method properly processed the SoapHeader.
5.  Create a client application, add web refference to the webservice.
WebService code:
using System.Data;
using System.Data.SqlClient;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
    public class securityInfo : SoapHeader
        public string username;
        public string password;
     public securityInfo obj1 = new securityInfo();
    [WebMethod, SoapHeader("obj1")]
    public string validate()
        string s1, s2, result;
        result = "n";
        obj1.MustUnderstand = true;
        SqlConnection con = new SqlConnection("uid=sa;pwd=systems;  Database=abi");
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select * from Info",con);
        da.Fill(ds, "userInfo");
        for (int i = 0; i <= ds.Tables["userInfo"].Rows.Count; i++)
            s1 = Convert.ToString(ds.Tables["userInfo"].Rows[i][0]);
            s2 = Convert.ToString(ds.Tables["userInfo"].Rows[i][1]);
            if (s1 == obj1.username.ToString() && s2 == obj1.password.ToString())
                result = "y";
        if (result == "y")
            return "valid login";
            obj1.DidUnderstand = true;
            return "invalid login";
Client Application Source page:
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
         <asp:Button ID=" btnSubmit" runat="server"    onclick="btnSubmit_Click" Text="Submit" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
Client Application Code behind page:
protected void btnSubmit_Click(object sender, EventArgs e)
        localhost.Service obj1 = new localhost.Service();
        localhost.securityInfo obj2 = new localhost.securityInfo();
        obj2.username = txtUserName.Text;
        obj2.password = txtPwd.Text;
        obj1.securityInfoValue = obj2;
        string res = obj1.validate();
        if (res == "valid login")
            Label1.Text = "valid user";
            Label1.Text = "Invalid user";