I was told to build a rest web service using asp.net so does the code below represent the same
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace Jquery
{
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 string GetAgentDetails(string JsonAgentUsernameAndPassword)
{
string username = "", password = "";
Agent agentDetails = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Agent>(JsonAgentUsernameAndPassword);
//will get only one entry
foreach (var items in agentDetails.data)
{
username = items.username;
password = items.password;
}
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true"))
{
string cmdtxt = "select * from Tbl_Login where username=" + username + "and password =" + password;
using (SqlCommand cmd = new SqlCommand(cmdtxt, con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = 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]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
}
public class Agent
{
public List<AgentDetails> data { get; set; }
}
public class AgentDetails
{
public string username { get; set; }
public string password { get; set; }
}
}