Click here to Skip to main content
15,916,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
dear all,

i am trying to display database value into html table using jquery ajax call method in asp.net

but its not displaying. plese help me

What I have tried:

function Bindtable() {
$(document).ready(function () {

var row = "";
$.ajax({
type: "POST",
url: "wsc_common.asmx/bindtable",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$.each(msg.d, function (index, obj) {
row += "
" + obj.pid + "" + obj.p_name + "" + obj.p_mobile + "" + obj.p_location + "
";

});

$("#tbDetails tbody").append(row);
}
});


});
}
Posted
Updated 17-Nov-17 7:14am
Comments
Karthik_Mahalingam 11-Jun-16 9:42am    
post your webmethod code
bindtable()
Rajiv.net40 11-Jun-16 9:46am    
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string bindtable()
{
dllgetinfo info = new dllgetinfo();
return info.showdata();
}

//=======
public string showdata()
{
string qry = "select *from usr";
DataTable dt = dllmain.ExecuteselectQry_returns_datatable(qry);
return getdatatabletojson(dt);


}

public static string getdatatabletojson(DataTable dt)
{
string str = string.Empty;
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<dictionary<string, object="">> rows = new List<dictionary<string, object="">>();
Dictionary<string, object=""> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object="">();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
str = serializer.Serialize(rows);
return str;

}
Karthik_Mahalingam 11-Jun-16 10:29am    
check the solution

try this

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script src="jquery.min.js"></script>
    <script>

        function BindTable()
        {
            
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/bindtable",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    var json = JSON.parse(msg.d);
                    $.each(json, function (index, obj) { 
                        var row = '<tr><td> ' + obj.pid + ' </td> <td> ' + obj.p_name + ' </td> <td>' + obj.p_mobile + '</td> <td>' + obj.p_location + '</td> </tr>'
                        $("#tbDetails tbody").append(row);
                    }); 
                }
            });

        }


        $(document).ready(function () {
            BindTable();
        });
    </script>


</head>
<body>
    
    <table id="tbDetails">
        <tr>
            <td>ID</td> <td>Name</td> <td>Mobile</td> <td>Location</td>
        </tr>
        <tbody>

        </tbody>
    </table>

</body>
</html>
 
Share this answer
 
Comments
Karthik_Mahalingam 11-Jun-16 10:28am    
I have hardcoded the Service code like this, FYI


[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string bindtable()
{

return this.showdata();
}

//=======
public string showdata()
{

DataTable dt = new DataTable();
dt.Columns.Add("pid");
dt.Columns.Add("p_name");
dt.Columns.Add("p_mobile");
dt.Columns.Add("p_location");
dt.Rows.Add(1, "karthik", 1234, "TamilNadu");
dt.Rows.Add(1, "kavya", 456, "TamilNadu");
return getdatatabletojson(dt);


}
Rajiv.net40 12-Jun-16 0:11am    
sir your code works fine but 1 thing is wrong with this. when i click getdata button it repeats complit table.if i click 2 time it repeats tables two times .....plz solve it
Karthik_Mahalingam 12-Jun-16 0:14am    
Ok wait
Karthik_Mahalingam 12-Jun-16 0:23am    
add this line
$("#tbDetails tbody").find('tr').remove(); ..
.-------------


function BindTable()
{
$("#tbDetails tbody").find('tr').remove();
var row = "";
$.ajax({
type: "POST",
url: "WebService1.asmx/bindtable",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var json = JSON.parse(msg.d);
$.each(json, function (index, obj) {
debugger;
var row = '<tr><td> ' + obj.pid + ' </td> <td> ' + obj.p_name + ' </td> <td>' + obj.p_mobile + '</td> <td>' + obj.p_location + '</td> </tr>'
$("#tbDetails tbody").append(row);
});


}
});

}
Rajiv.net40 12-Jun-16 0:28am    
this is not working sir
C#
class jamiu{
public string id{get; set;}
public string Name {get; set;}
}
public static List<jamiu> getdata(){
List<jamiu> jam=new  List<jamiu>();
SqlConnection con=new SqlConnection();
SqlCommand cmd=new SqlCommand();
cmd.connection=con;
cmd.CommandText="select *from Table";
 SqlDataReader dr=cmmd.ExecuteReader();
DataTable dt=new DataTable();
 for (Int32 i = 0; i < dt.Rows.Count; i++)
{dt.Load(dr);
jamiu jj=new jamiu();
jj.id=dt.Rows[1]["Id"].ToString();
}
 dr.Close();
        con.Close();
 


}

<script>
$(document).ready(function () {
var schli=document.getElementById(schlidd).innerText;
$.ajax({
type:"Post",
dataType: "json",
url: "Default.aspx/getbook",
data: "{'Id':'" + schli + "'}",
success: function (data) {
if (data.d.length > 0) {
$("#dd").html('');
for (var i = 0; i < data.d.length; i++) {
var List = ["<tr><td>" + data.d[i].Id + "</td><td>"];
$("#dd").append(List);
}
}
}

})
})
</script>
 
Share this answer
 
v4
Comments
saranraj98 12-Dec-17 23:56pm    
How to do this work in php?
first i have a php code with database. And i have some set of data in database.
how to fetch that data and convert that data to json .
then using jquery how to make the table using that json file?.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900