Building a Web Service with Visual Studio.Net involves
creating a new Web Services project and adding methods and properties to a Web
Services class form (i.e. an .asmx file). This section builds a Web Service
with Visual Studio.Net. Followings are the steps in short to create an .asmx
file.
1.
Start Visual Studio .NET
2.
Select New Project
3.
Select Visual C# Project as Project Type and Asp.Net Web Service as
Template
A solution will be generated. In solution explorer there
will be an asmx file.
The following code we should write in .asmx file as
specified in Listing 4.
Listing 4
[WebMethod]
public string Add(int a, int b)
{
return Convert.ToString(a + b);
}
[WebMethod]
public DataSet Populate(string con, string sql)
{
DataSet DS=new DataSet();
SqlConnection conn=new SqlConnection(con);
SqlDataAdapter DA=new SqlDataAdapter(sql, conn);
DA.Fill(DS);
return DS;
}
In the above code there are two methods in Web Service. These
methods are known as the WebMethods. The Add WebMethod returns the sum of two
integer value as string. Populate web method takes two string type arguments,
one for connection string and the other for sql query, and returns a DataSet
object.