Categories : - How to create Ajax enable wcf web service in asp.net
Introduction : -
.ASP.NET makes it easy to create web services but they usually return XML. Like many web developers I now prefer JSON. This article and sample code will show you how to get your web service to return data in the JSON format.
Description : -
For fetch the data from MySQL database using web service in asp.net json format step as shown below.
Step :- 1 Create a new web site
Step : -2 Write click on website and add new web service
Step : -3 Write a code on WebService.cs file
as shown below
For Connect to my sql use the Connector .MySql Connector Download Link is Click
Run your service and seen output like this
Click on GetAlRecords
Introduction : -
.ASP.NET makes it easy to create web services but they usually return XML. Like many web developers I now prefer JSON. This article and sample code will show you how to get your web service to return data in the JSON format.
Description : -
For fetch the data from MySQL database using web service in asp.net json format step as shown below.
Step :- 1 Create a new web site
Step : -2 Write click on website and add new web service
Step : -3 Write a code on WebService.cs file
as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using MySql.Data.MySqlClient;
using System.Data;
using System.Web.Script.Serialization;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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 : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod(Description = "Gets All Records.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetAllRecords() {
MySqlConnection objConnection = new MySqlConnection("server=localhost;User Id=root;database=databaseName;Pooling=false;Connect Timeout=10;Allow User Variables=True;");
MySqlCommand objCommand = new MySqlCommand("SELECT * FROM info_tab ORDER BY id", objConnection);
DataSet objDataSet = new DataSet();
MySqlDataAdapter objDataAdapter = new MySqlDataAdapter(objCommand);
objDataAdapter.Fill(objDataSet, "reading");
objConnection.Close();
// Create a multidimensional jagged array
string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
int i = 0;
foreach (DataRow rs in objDataSet.Tables[0].Rows)
{
JaggedArray[i] = new string[] { rs["user_id"].ToString(), rs["First_Name"].ToString(), rs["Last_Name"].ToString() };
i = i + 1;
}
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using MySql.Data.MySqlClient;
using System.Data;
using System.Web.Script.Serialization;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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 : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod(Description = "Gets All Records.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetAllRecords() {
MySqlConnection objConnection = new MySqlConnection("server=localhost;User Id=root;database=databaseName;Pooling=false;Connect Timeout=10;Allow User Variables=True;");
MySqlCommand objCommand = new MySqlCommand("SELECT * FROM info_tab ORDER BY id", objConnection);
DataSet objDataSet = new DataSet();
MySqlDataAdapter objDataAdapter = new MySqlDataAdapter(objCommand);
objDataAdapter.Fill(objDataSet, "reading");
objConnection.Close();
// Create a multidimensional jagged array
string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
int i = 0;
foreach (DataRow rs in objDataSet.Tables[0].Rows)
{
JaggedArray[i] = new string[] { rs["user_id"].ToString(), rs["First_Name"].ToString(), rs["Last_Name"].ToString() };
i = i + 1;
}
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON;
}
}
For Connect to my sql use the Connector .MySql Connector Download Link is Click
Run your service and seen output like this
Click on GetAlRecords