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

Last Reply one year ago By AnandM

Posted one year ago

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;
    }
    
}

 

You are viewing reply posted by: AnandM one year ago.
Posted one year ago
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