Step 1: Create one website project in the visual studio
Step 2: Create one folder with name “WebServices” in the project.
Step 3: Right click on the folder and click on add new item, then select “ web services” file from the list, as shown in image below
Step 4: After creating file in folder, Webservices.asmx file will be created in the folder and a default folder with name “App_Code” will be added the project and new file with the name “Webservices.cs” file will be added to it automatically.
Step 5: Then create three class file in App_Code folder with name :
a.WebService_Class.cs
We will create all the function of database operation here. We will discuss this in detail below.
b.Webservice_Main.cs
In this class, we will store the generic list of web services data. We will discuss this in detail below.
c.WebServices_StoreLocalVariable.cs
In this class we will store all the local variables, so that we can speed up web services. We will discuss this in detail below.
WebService_Class.cs
You can add this code in this file
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using commonlib.Common;
/// <summary>
/// Summary description for WebService_Class
/// </summary>
namespace EuroCity
{
public class GetAllCityClass
{
#region "Fields"
private int iCityID;
private string sCityName;
DBManager DM = new DBManager(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
#endregion
#region "Properties"
public int ICityID
{
get { return iCityID; }
set { iCityID = value; }
}
public string SCityName
{
get { return sCityName; }
set { sCityName = value; }
}
#endregion
#region "Function"
public DataSet GetAllCity()
{
DataSet DSCity = new DataSet();
string SQL = "SP_GetAllCity";
try
{
DSCity = DM.ExecuteDataSet(CommandType.StoredProcedure, SQL);
}
catch (Exception ex)
{
ex = null;
}
finally
{
if (DSCity != null)
{
DSCity.Dispose();
}
}
return DSCity;
}
#endregion
}
}
Webservice_Main.cs
You can add this below code in this file.
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using EuroCity;
/// <summary>
/// Summary description for WebServices_Main
/// </summary>
namespace EurocityCards_Classes
{
public class WebServices_GetAllCity
{
public string ErrorMessage = "";
public bool IsError = false;
public List<GetAllCityClass> CityList = new List<GetAllCityClass>();
}
}
WebServices_StoreLocalVariable.cs
You can add this below code in this file
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for WS_EuroClass
/// </summary>
public class GetAllCityClass
{
#region Data Member
public string iCityID, sCityName;
#endregion
}
WebService.cs
Add Below code in the WebService.cs, that generated automatically in the App_Code file.
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using EurocityCards_Classes;
using System.Collections.Generic;
using System.Configuration;
using EuroCity;
/// <summary>
/// Summary description for WS_EurocityCards
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public WebServices_GetAllCity GetAllCity()
{
EuroCity.GetAllCityClass objcity = new EuroCity.GetAllCityClass();
DataSet dscity = new DataSet();
WebServices_GetAllCity WS_city = new EurocityCards_Classes.WebServices_GetAllCity();
dscity = objcity.GetAllCity();
objcity = null;
if (dscity != null && dscity.Tables[0].Rows.Count > 0)
{
WS_city.IsError = false;
List<GetAllCityClass> stateList = new List<GetAllCityClass>();
foreach (DataRow dtRow in dscity.Tables[0].Rows)
{
GetAllCityClass objcityName = new GetAllCityClass();
objcityName.iCityID = dtRow["catid"].ToString();
objcityName.sCityName = dtRow["Catname"].ToString();
stateList.Add(objcityName);
}
WS_city.CityList = stateList;
}
else
{
WS_city.ErrorMessage = "No record Found";
WS_city.IsError = true;
}
return WS_city;
}
}
Add Below code in the web.config file for the connection to the database
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=YOURSQLSERVERNAME;Initial Catalog=YOURDATABASENAME;user id=YOURDATABASEUSERNAME;password=YOURDATABASEPASSWORD;" providerName="System.Data.SqlClient;Max Pool Size=7500;Connect Timeout=500;pooling=true"/>
</connectionStrings>
To make database connection you need to create SP for that, please find code below
Create one Database in your SQL Server, and create one table name “SiteCity”
-- For City Details
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('SP_GetAllCity'))
DROP PROCEDURE SP_GetAllCity
GO
CREATE PROCEDURE [SP_GetAllCity]
AS
BEGIN
SELECT CityID,CityName
FROM SiteCity
ORDER BY CityName ASC
END
GO
After Creating the project, just build your application and run in brower.
Follow the screen to get your output.
Web Services First View
You will see the web service name here, see below in the image,
After clicking to the web services name, you will see "Invoke button"
This is the button used to run the web services.See in the screen below
Then finally you will see the below screen as output, what you were in need of, just call this and make you application successfull.
Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com
No comments :
Post a Comment