Hi AliYilmaz,
Its totally depends on the data format which is going to be returned and how its going to be handled whether in Json format or you want to give your own way for eg. Property Class Format,DataTable etc. as you can return your data in whichever format you want.
I have made a sample in which i have showed that how you can handle Json data and Property Class data like that you can implement your own way of returning data and handling it on the user end.
I have used NewtonJson DLL for converting Json Data to DataTable you can get NewtonJson DLL from below link and add reference to your project and use it.
NewtonJson
WebService.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
WebService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Xml;
/// <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
{
[WebMethod]
public UserDetail GetDataInClassFormat(string Email, string UserName)
{
UserDetail us = new UserDetail();
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Email = @Email AND UserName = @UserName", cn);
DataTable dt = new DataTable();
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@UserName", UserName);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
us.UserId = Convert.ToInt32(dt.Rows[0]["UserId"].ToString());
us.Name = dt.Rows[0]["Name"].ToString();
us.Email = dt.Rows[0]["Email"].ToString();
us.UserName = dt.Rows[0]["UserName"].ToString();
us.Password = dt.Rows[0]["Password"].ToString();
us.ImageName = dt.Rows[0]["ImageName"].ToString();
}
}
catch (Exception ex)
{
}
return us;
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetDataInJsonFormat(string Email, string UserName)
{
string resultJSON = "";
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
cn.Open();
SqlCommand cmd = new SqlCommand();
DataTable dt;
SqlDataReader reader;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Users WHERE Email = @Email AND UserName = @UserName";
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Connection = cn;
reader = cmd.ExecuteReader();
dt = new DataTable("results");
dt.Load(reader);
cn.Close();
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<String, Object>> tableRows = new List<Dictionary<string, object>>();
Dictionary<String, Object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col].ToString());
}
tableRows.Add(row);
}
return resultJSON = serializer.Serialize(tableRows).ToString();
}
catch (Exception ex)
{
return resultJSON = ex.Message.ToString();
}
}
public class UserDetail
{
int _userId;
public int UserId
{
get
{
return _userId;
}
set
{
_userId = value;
}
}
string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
string _email;
public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
string _userName;
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}
string _password;
public string Password
{
get
{
return _password;
}
set
{
_password = value;
}
}
string _imageName;
public string ImageName
{
get
{
return _imageName;
}
set
{
_imageName = value;
}
}
}
}
Default.aspx
<div>
<table>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server" />
</td>
</tr>
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnGetjSonData" OnClick="BindJsonData" Text="Bind Json Data" runat="server" />
</td>
<td>
<asp:Button ID="btnGetClassData" Text="Bind Class Data" OnClick="BindClassData" runat="server" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="gvUserDetails" runat="server" />
</div>
Default.aspx.cs
protected void BindJsonData(object sender, EventArgs e)
{
ServiceReference1.WebServiceSoapClient client = new ServiceReference1.WebServiceSoapClient();
string jsonData = client.GetDataInJsonFormat(txtEmail.Text.Trim(), txtUserName.Text.Trim());
JavaScriptSerializer js = new JavaScriptSerializer();
DataTable dt = JsonConvert.DeserializeObject<DataTable>(jsonData);
gvUserDetails.DataSource = dt;
gvUserDetails.DataBind();
}
protected void BindClassData(object sender, EventArgs e)
{
ServiceReference1.WebServiceSoapClient client = new ServiceReference1.WebServiceSoapClient();
ServiceReference1.UserDetail userDetail = client.GetDataInClassFormat(txtEmail.Text.Trim(), txtUserName.Text.Trim());
List<ServiceReference1.UserDetail> userDetails = new List<ServiceReference1.UserDetail>();
userDetails.Add(userDetail);
gvUserDetails.DataSource = userDetails;
gvUserDetails.DataBind();
}
Screenshot
