hi,
I made this sample webservice which takes username and return the details in json response. it has 2 problems
1. Instead of showing result set it is showing sql statement in the result, what change should be done it return so it shows result instead of sql statement.
2. i deploy it here http://phelade.com/WebService.asmx which shows 2 function getusername when i click getusername it show all details but showing result
i want to change it like that i can pass parameter in url and it shows result in json like http://phelade.com/WebService.asmx?username= 'xyz' when i click enter it shows result.
what should i change in code to achieve that?
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
/// <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]
public string getusername(string username)
{
var json = "";
string UserName;
string Password;
string sqlStatment = "select Username, PasswordHash from aspnetusers where username ='" + username + "' ";
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlStatment, con))
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
UserName = reader.GetString(0);
Password = reader.GetString(1);
}
reader.Close();
cmd.Connection.Close();
}
}
JavaScriptSerializer jss = new JavaScriptSerializer();
json = jss.Serialize(sqlStatment);
return json;
}
}