hariomthapa.in hariomthapa.in - 4 months ago 10
jQuery Question

how to get double object using json or jquery?

I tried out to get double DataTable from webservice by json or jquery ajax like

The WebService Method :

[WebMethod]
public DataSet FareAccpted_(string custId)
{
DataSet ds = new DataSet();
List<fareAccptedList> details = new List<fareAccptedList>();

using (SqlConnection con = new SqlConnection("..."))

{
using (SqlCommand cmd = new SqlCommand("proc_FareAcceptedC", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@customerId", SqlDbType.Int).Value = custId;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
}
return ds;
}


The Js page :

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "../lpService.asmx/FareAccpted_",
data: JSON.stringify({ custId: custId_ }),
dataType: "json",
success: function (data) {
alert('Length : '+data.d.length)

},
error: function (result) {
alert("FareAcceptedC" + "Error");
}
});


DataTables stores in DataSet.

Answer

Firstly, your $.ajax call is wrong, specifically the way you are passing through the custId_ parameter. Also the dataType is not used in the POST call and the contentType can be excluded as well.

Change your $.ajax call like this and you will now be able to hit a breakpoint in the [WebMethod]:

$.ajax({
    type: "POST",
    url: "../lpService.asmx/FareAccpted_",
    data: { "custId": custId_ },
    success: function (data) {
        alert('Length : ' + data.d.length)
    },
    error: function (result) {
        alert("FareAcceptedC" + "Error");
    }
});

Secondly, look at the way you're instantiating the SqlConnection object:

using (SqlConnection con = new SqlConnection("..."))

"..." is not a valid connection string.You need store the connection string to your SQL database in the Web.config file like this:

  <connectionStrings>
    <add name="connectionString" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=UsersDatabase;Data Source=MyServerName"/>
  </connectionStrings>

And access it from C# code like this:

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{

}
Comments