0

I have a asp.net web service accessing value from database using a datatable and my javascript goes like this in eclipse where it is running in android simulator using phonegap but this code seems not to be working .pls help me out.

<script type="text/javascript">
     function GetAge() {
         jQuery.support.cors = true;
          $.mobile.allowCrossDomainPages = true;
           $.ajax({
          data: datas, 
             type: "POST",
            async: false,
            dataType: "json",
             contentType: "application/json; charset=utf-8",   
             url: "http://localhost:50113/Service1.asmx/mydbCon?wsdl",
             success: function (msg) {
                $('#divToBeWorkedOn').html(msg.text); 
             },
             error: function (e) {
                 $('#divToBeWorkedOn').html("unavailable");
             }
         });
     } 
  </script>  

and my service1.asmx goes like this

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DataTable mydbCon()
    {
        SqlConnection SqlCon = new SqlConnection("");
        SqlCon.Open();
        SqlCommand SqlComm = new SqlCommand();
        SqlComm.Connection = SqlCon;
        SqlComm.CommandType = CommandType.Text;
        SqlComm.CommandText = "select password from tbl_login where username='aby';";
        DataTable EmployeeDt = new DataTable("tbl_login");
        SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
        SqlDa.Fill(EmployeeDt);
        return EmployeeDt;
    }
7
  • is mydbCon the method name in the service1.asmx? can you show the content of service1.asmx? Commented Aug 23, 2012 at 9:34
  • i Have added my service1.asmx check it out Commented Aug 23, 2012 at 9:38
  • stackoverflow.com/a/2979938/169714 and why do you use ?wsdl? Commented Aug 23, 2012 at 9:45
  • i have deleted the >wsdl and checked also but not working Commented Aug 23, 2012 at 9:46
  • because you just return a datatable instead of json, please use json.codeplex.com Commented Aug 23, 2012 at 9:47

1 Answer 1

3

Add Json.Net to your solution with the package manager console or by the dialog

and then:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string mydbCon()
{
    SqlConnection SqlCon = new SqlConnection("");
    SqlCon.Open();
    SqlCommand SqlComm = new SqlCommand();
    SqlComm.Connection = SqlCon;
    SqlComm.CommandType = CommandType.Text;
    SqlComm.CommandText = "select password from tbl_login where username='aby';";
    DataTable EmployeeDt = new DataTable("tbl_login");
    SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
    SqlDa.Fill(EmployeeDt);
    return JsonConvert.SerializeObject(EmployeeDt, Formatting.Indented);
}

Here is the link of Json.Net on the nuget gallery: http://nuget.org/packages/Newtonsoft.Json

Sign up to request clarification or add additional context in comments.

3 Comments

hey thanks for the code but i cant insdtall json.net in visual studio express 2010 since I dont have the option library package manager .Let me ask my admin to install VS ultimatum and try this out any way thanks a lot
or download the dll, put it in your bin folder and reference it
or open your solution in visual web developer express stackoverflow.com/questions/4566908/… do you have nuget installed? visualstudiogallery.msdn.microsoft.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.