WebService

                                                            WebService Application




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
string conStr = ConfigurationManager.ConnectionStrings["testCon"].ConnectionString;
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter sqlda;
DataTable dt;
DataSet ds;



[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public System.Data.DataTable sample()
{
using (con = new SqlConnection(conStr))
{
con.Open();
string selQry = "select * from testAjax";
using (cmd = new SqlCommand(selQry, con))
{
sqlda = new SqlDataAdapter(cmd);
dt = new DataTable();
ds = new DataSet();
sqlda.Fill(ds,"testAjax");
}
}
return ds.Tables["testAjax"];
}


}
}





http://localhost:49457/WebServ1/Service.asmx


















Default.aspx

<div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" >
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gender">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
   
    </div>

Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid();
        }
    }

    private void FillGrid()
    {
        DataTable dt = new DataTable();
        SampleWebService.Service sws = new SampleWebService.Service();


        GridView1.DataSource = sws.SampleFillGrid();
        GridView1.DataBind();

    }
}


Comments