Thursday, February 4, 2010

How to create webservice ?

1) create file with .asmx file

2) write your logic in .cs file for example

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;

///
/// Summary description for WebService_360
///

[WebService(Namespace = "http://aatish.com/WebService_360", Name = "GetCustomers")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService_360 : System.Web.Services.WebService {

public WebService_360 () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public DataSet GetCustomers()
{
SqlConnection conn;
SqlDataAdapter myDataAdapter;
DataSet myDataSet;
string cmdString = ("select * from student_info");
conn = new SqlConnection("Server=myserver;database=allexample;Integrated Security=SSPI;");
myDataAdapter = new SqlDataAdapter(cmdString, conn);
myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "student_info");
return myDataSet;
}
}

3) Add ref to web service through

add web references

4) call web service

protected void Button1_Click(object sender, EventArgs e)
{
try
{
myservices.customer mycustomer = new myservices.customer();
GridView1.DataSource = mycustomer.getcustomer();
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}

No comments:

Post a Comment