Web Service is an entity which is used to provide various functionality over the internet irrespective of programming language, OS and platform.
ASP.NET web service is easy to create and is accessible over HTTP (SOAP).
Following example shows how to use web service in ASP.NET
1. To create Web service in VS 2013, create an empty project and then add web service item.
2. Web class test uses getUser web method to implement desired functionality.
Objective of getUser() method is to get User details from database for a particular city.
[WebMethod]
public DataSet getUser(string strCity)
{
//get City Users
string strConnect = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectKBTestDB"].ConnectionString;
SqlConnection con = new SqlConnection(strConnect);
con.Open();
SqlCommand cmd = new SqlCommand("SP_City_User", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlParameter City=new SqlParameter();
City.ParameterName=("@City");
City.Value=strCity;
cmd.Parameters.Add(City);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
return ds;
}
Run and execute web service to see the result.
Note : asmx file created as a result of this webservice as http://localhost:62798/test.asmx which would look like
3. In order to utilize this web service method, create a sample website project.
Add Service Reference (Solution Explorer --> Add --> Service Reference )
4. Copy the URL of web service created just now
http://localhost:62798/test.asmx and paste it to the reference as shown in figure below
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
ServiceReference1.testSoapClient tc = new ServiceReference1.testSoapClient();
DataSet ds=tc.getUser(ddlCity.SelectedItem.Text);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
ASP.NET web service is easy to create and is accessible over HTTP (SOAP).
Following example shows how to use web service in ASP.NET
1. To create Web service in VS 2013, create an empty project and then add web service item.
2. Web class test uses getUser web method to implement desired functionality.
Objective of getUser() method is to get User details from database for a particular city.
[WebMethod]
public DataSet getUser(string strCity)
{
//get City Users
string strConnect = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectKBTestDB"].ConnectionString;
SqlConnection con = new SqlConnection(strConnect);
con.Open();
SqlCommand cmd = new SqlCommand("SP_City_User", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlParameter City=new SqlParameter();
City.ParameterName=("@City");
City.Value=strCity;
cmd.Parameters.Add(City);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
return ds;
}
Run and execute web service to see the result.
Note : asmx file created as a result of this webservice as http://localhost:62798/test.asmx which would look like
3. In order to utilize this web service method, create a sample website project.
Add Service Reference (Solution Explorer --> Add --> Service Reference )
4. Copy the URL of web service created just now
http://localhost:62798/test.asmx and paste it to the reference as shown in figure below
5. So now we have to consume this web service , web method in our web application.
When we click OK button (Figure above), .NET framework would generate a series of classes and methods in order to use webmethods. Code to use the same is as follows.
On our web page, we have a dropdown list which shows list of available cities. On selecting a particular city (SelectedIndexChanged event) our task is to show webservice result in gridview.
6. As we run web service, we get result as follows.




No comments:
Post a Comment