Consume Web Service in ASP.Net web application using C# and VB.Net

Last Reply on Jun 14, 2017 03:16 AM By AnandM

Posted on Jun 14, 2017 03:08 AM



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 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 '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 = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 

    public string HelloWorld() {
        return "Hello World";

    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))


                System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                    UserName = reader.GetString(0);
                    Password = reader.GetString(1);



        JavaScriptSerializer jss = new JavaScriptSerializer();

        json = jss.Serialize(sqlStatment);
        return json;


You are viewing reply posted by: AnandM on Jun 14, 2017 03:16 AM.
Posted on Jun 14, 2017 03:16 AM
nauna says:
json = jss.Serialize(sqlStatment);

 you are serializing only sql statement and returning the same so thats why you are getting sql statement so refer below articles and you will get better idea of implementation of webservice

  1. Call (Consume) Web Service (ASMX) using jQuery AJAX in ASP.Net
  2. Consume (Call) JSON Web Service in ASP.Net using C# and VB.Net
  3. Call (Consume) Web Service (ASMX) in ASP.Net web application using C# and VB.Net