WebService with MethodoverLoading

App_Code/Servece1.cs

using System;
using
System.Web;
using
System.Web.Services;
using
System.Web.Services.Protocols;
using
System.Xml;
using
System.Configuration;
using
System.Data;
using
System.Data.OleDb;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]

public
class Service : System.Web.Services.WebService
{
    ConnClass conn = new ConnClass(); // connction class
    public Service () {
       
    }

    [WebMethod]
    public XmlElement GetUserDetails(string userName)
    {
        string con = conn.GetConnection(); // connection method
        OleDbConnection connect = new OleDbConnection(con);
        string qry = "select * from AuthorDetails where AuthName like '"+userName+"%'";
        OleDbCommand cmd = new OleDbCommand(qry, connect);
        connect.Open();
        OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        XmlDataDocument xmldata = new XmlDataDocument(ds);
        XmlElement xmlElement = xmldata.DocumentElement;
        return xmlElement;
    }
    [WebMethod(MessageName = "You Have Greeting")]

    public XmlElement GetUserDetails(string AutherName, string BookName

    {
        string con = conn.GetConnection();
        OleDbConnection connect = new OleDbConnection(con);
        string qry = "select * from AuthorDetails where AuthNam\
e like'"
+ AutherName + "%' and BookTitle like '" + BookName + "%'";
        OleDbCommand cmd = new OleDbCommand(qry, connect);
        OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        oda.Fill(ds);
        XmlDataDocument xdoc = new XmlDataDocument(ds);
        XmlElement XmlElem = xdoc.DocumentElement;
        return XmlElem;
    }   

}

forOnePara.JPG

Calling the Web Service into my UI Application

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
    <title>Untitled Page</title>

</
head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click1" />
    </div>
        <asp:GridView ID="gvUserDetails" runat="server">
        </asp:GridView>
    </form>

</
body>
</
html>

Default.aspx.cs


using
System;
using
System.Data;
using
System.Configuration;

sing
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.OleDb;
using
System.Xml;
public
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {                      
        }
    }
    protected void btnSubmit_Click1(object sender, EventArgs e)
    {       
        BindUserDetails();
    }

    private void BindUserDetails()
    {
        string UName = txtUserName.Text; //Textbox to read the values
        localhost.Service objService = new localhost.Service();
        //webservice aliase name with object
        DataSet ds = new DataSet();
        XmlElement elem = objService.GetUserDetails(UName);
        if (elem != null)
        {
            XmlNodeReader nodereader = new XmlNodeReader(elem);
            ds.ReadXml(nodereader, XmlReadMode.Auto);
            gvUserDetails.DataSource = ds;
            gvUserDetails.DataBind();
        }
        else
        {
            gvUserDetails.DataSource = null;
            gvUserDetails.DataBind();
        }
    }
}


UIforTwoPara.JPG

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
    <title>Untitled Page</title>

</
head>
<
body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Author Name"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br />
        <asp:Label ID="Label2" runat="server" Text="Book Name"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
         <div>  
    </div>
    </form>

</
body>
</
html>

Default2.aspx.cs

using System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Xml;
using
System.Data.OleDb;
public
partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        findAuthBookDeta();
    }

    private void findAuthBookDeta()
    {
        string AName = TextBox1.Text; //read the value from textbox for I/P
        string BName = TextBox2.Text;
        localhost.Service objService = new localhost.Service();
        DataSet ds = new DataSet();
        XmlElement elem = objService.GetUserDetails(AName, BName); //call the web service method
        if (elem != null)
        {
            XmlNodeReader nodereader = new XmlNodeReader(elem);
            ds.ReadXml(nodereader, XmlReadMode.Auto);
            gvUserDetails.DataSource = ds;
            gvUserDetails.DataBind();
        }

       else
        {
                gvUserDetails.DataSource = null;
             gvUserDetails.DataBind();

        }

    }

}
OutPutforTwoPara

Next Recommended Reading Creating Simple WebService in ASP.NET