Gaurav Pal

Gaurav Pal

  • NA
  • 306
  • 68.5k

Asp.net web service return json

Jul 27 2015 4:02 AM
I am using below code for my web service. I want return value in json. but this code return value in XML format only.
 
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public bool login(string uname, string pwd)
{
//SqlConnection con = new SqlConnection();
//SqlCommand cmd = new SqlCommand();
//con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
//SqlDataAdapter da = new SqlDataAdapter("select * from UserList where UserName ='" + uname + "' and password='" + pwd + "' ", con);
//DataSet ds = new DataSet();
//da.Fill(ds);
//return ds;
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.CommandText = "select * from UserList where UserName=@username and Password=@pwd";
cmd.Connection = con;
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = uname;
cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value = pwd;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
return true;
}
else
{
return false;
}
con.Close();
}
 

Answers (9)