You must Sign In to post a response.
  • Category: Webservices

    Asp.net web service return json

    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();
    }
  • #760976
    hi
    you can try this code


    [WebMethod]
    public EmployeeInformations GetEmployessXML()
    {
    EmployeeInformations emps= new EmployeeInformations {
    new EmployeeInformations()
    {
    EmployeeId=1,
    EmployeeName="Bret Lee",
    EmployeeSalary=2000
    },
    new EmployeeInformations()
    {
    EmployeeId=2,
    EmployeeName="Mc-Grah",
    EmployeeSalary=1000
    }
    };
    return emps;
    }


    I have attached Images below

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #760977
    not in XML i want Json formated value

  • #760982
    Change the return type into string

    See the following sample

    public string login(string uname, string pwd)
    {
    string zCustomerIdL = "Test"; HttpContext.Current.Response.ContentType = "application/json";
    return zCustomerIdL;
    }

    By Nathan
    Direction is important than speed

  • #760986
    no change

  • #760987
    Hello Gaurav,

    Refer the below example of code :

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetDataInJSON()
    {
    Software[] sw = new Software[] {
    new Software()
    {
    Id=10100,
    Name="Visual Studio",
    Price=25000
    },
    new Software()
    {
    Id=10101,
    Name="MS Office",
    Price=22000
    }
    };
    return new JavaScriptSerializer().Serialize(sw);
    }

    Hope this will help you to get your data in JSon.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #760989
    Hi
    Try this code


    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetEmployessJSON()
    {
    EmployeeInformations[] emps = new EmployeeInformations[] {
    new EmployeeInformations()
    {
    EmployeeId=1,
    EmployeeName="Sachin",
    EmployeeSalary=100
    },
    new EmployeeInformations()
    {
    EmployeeId=102,
    EmployeeName="Koli",
    EmployeeSalary=101
    }
    };
    return new JavaScriptSerializer().Serialize(emps);
    }
    public class EmployeeInformations
    {
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public int EmployeeSalary { get; set; }
    }


    I have attached Image given below.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #760994
    following result shown

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
    <string xmlns="http://tempuri.org/">
    [{"EmployeeId":1,"EmployeeName":"Sachin","EmployeeSalary":100},{"EmployeeId":102,"EmployeeName":"Koli","EmployeeSalary":101}]
    </string>

  • #760995
    Hi
    Gaurav

    This is wrong?

    Can you explain more . what is your output can you show them.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #760996
    i am receiving about output

  • #761000
    Web service is SOAP. You can try handler (.ashx).
    By Nathan
    Direction is important than speed

  • #761001
    how?

  • #761004
    public void ProcessRequest(HttpContext context)
    {

    context.Response.ContentType = "application/json";
    context.Response.Write("Json String");
    }

    By Nathan
    Direction is important than speed


  • Sign In to post your comments