|
So there could be one of two situations for you to create a new web service. Follow
the one match your situation. Either you are creating it as independent new project
or you are creating it in an existing Project.
(a) Create a web service in as a new Project
- Create a new
Project using Visual Studio or Visual Web Developer and Name it “DatabaseWebservice”
Create a new web service project

- If you dock the Solution Explorer
you will be able to see two files. i.e. “Service.asmx”
in the Root Directory and “Service.cs” in the “App_code”
Directory.

- The code which the environment
spit out will look like this:
using
System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class
Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello
World";
}
}
(b) Create a web service in an existing Project
- Create the web service in an
existing Project and name it “DatabaseWebservice”
Create a Web Service in an existing
Project
- You will have
“DatabaseWebservice.asmx” in the root Directory and
“DatabaseWebservice.cs” in the “App_code”
- The code which
the environment spit out would look like this:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
///<summary>
/// Summary description
for DatabaseWebservice
///</summary>
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class
DatabaseWebservice : System.Web.Services.WebService {
public DatabaseWebservice () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello
World";
}
}
(c) Add a web method to the Service
So you noticed that the only difference
is how the Environment will name your service.
Now add a new method to the above
code.
[WebMethod]
public DataSet
GetLatestCsharp()
{
using (SqlConnection
connection = new
SqlConnection(
ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString))
{
string Query = @"SELECT
TOP 5 * FROM [Articles]
WHERE ([Branch] = @Branch) ORDER BY [DateAdded] DESC";
SqlCommand command =
new SqlCommand(Query,
connection);
command.CommandType = CommandType.Text;
command.Parameters.Add(
"@Branch",
SqlDbType.NVarChar).Value = "CS";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable myTable =
new DataTable("myTable");
myTable.Columns.Add("Article Title",
typeof(string));
myTable.Columns.Add("Authour",
typeof(string));
myTable.Columns.Add("Link",
typeof(string));
while (reader.Read())
{
myTable.Rows.Add(
new object[]
reader["Title"].ToString(),
reader["AuthourName"].ToString(),
"www.dotnet-friends.com/"+
reader["Link"].ToString()});
}
myTable.AcceptChanges();
DataSet ds =
new DataSet();
ds.Tables.Add(myTable);
ds.AcceptChanges();
return ds;
}
}
You see that we are using
ConfigurationManager
and SQL Data Objects. So don’t forget to include the related following references
to your service header:
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
Now the Complete code will look
like this:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class
Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello
World";
}
///<summary>
/// Get Latest 5 Articles/Tutorial
in C-Sharp catagory
///</summary>
///<returns></returns>
[WebMethod(Description =
"Get latest Five Articles and Tutorials of C#, It returns Untyped Dataset")]
public DataSet
GetLatestCsharp()
{
using (SqlConnection
connection = new
SqlConnection(
ConfigurationManager.ConnectionStrings["MyConnStr
"].ConnectionString))
{
string Query = @"SELECT
TOP 5 * FROM [Articles] WHERE
([Branch]
= @Branch) ORDER BY [DateAdded] DESC";
SqlCommand command =
new SqlCommand(Query,
connection);
command.CommandType = CommandType.Text;
command.Parameters.Add(
"@Branch",
SqlDbType.NVarChar).Value = "CS";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable myTable =
new DataTable("myTable");
myTable.Columns.Add("Title",
typeof(string));
myTable.Columns.Add("Authour",
typeof(string));
myTable.Columns.Add("Link",
typeof(string));
while (reader.Read())
{
myTable.Rows.Add(
new object[]
reader["Title"].ToString(),
reader["AuthourName"].ToString(),
"http://www.dotnet-friends.com/"+ reader["Link"].ToString()});
}
myTable.AcceptChanges();
DataSet ds =
new DataSet();
ds.Tables.Add(myTable);
ds.AcceptChanges();
return ds;
}
}
}
So how are you going to access
the service now? For this purpose You have to create a web service client.
In the following Tutorial you will get to know that how can we create a Webservice
Client:
>> Creating a Web Service Client in .NET 3.0 using C#
Download the code:
Download Project: DatabaseService.zip
|