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;
    }
Dennis
  • 297
  • 6
  • 26

1 Answers1

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

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
  • 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 – Dennis Aug 23 '12 at 10:23
  • or download the dll, put it in your bin folder and reference it – JP Hellemons Aug 23 '12 at 10:24
  • or open your solution in visual web developer express http://stackoverflow.com/questions/4566908/how-can-i-use-nuget-with-visual-c-sharp-express do you have nuget installed? http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c – JP Hellemons Aug 23 '12 at 10:25