Click here to Skip to main content
15,886,860 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Dears
am create a get data Webmethod in Asp.net but i don't know how to get whole Datatable row value and return to ajax and generate HTML in Ajax Success message
This is my Webmethod Code
C#
public static string Getdata(string Id)
       {
           string SQL = "select * from testtable where UserId='" + Id + "'";
           DataTable DATA = new DataTable();
           SqlDataAdapter DTA = new SqlDataAdapter(SQL, cnnx);
           DTA.Fill(DATA);
           if (DATA.Rows.Count > 0)
           {
 

 

 

           }
           return "";

What we do in if block to get whole rows value with for each loop and return to ajax
JavaScript
<script>
        $(document).ready(function () {
            $("#Btnclick").click(function () {
                $.ajax({
                    type: "POST",
                    url: "WebForm1.aspx/Getdata",
                    data: "{'Id':'" + $("#textid").val() + "'}",
                    datatype: "JSON",
                    contentType: "application/json; charset=ut-8",
                    success:function(data)
                    {
                        alert(data.d);
                       
                    },
                    error: function () {
                        alert("error");
                    }
                   
 
                });
 
            });
 

 
        });
    </script>

Please tell me any solution

What I have tried:

Return multiple rows data in ADO.NET to ajax and generate HTML
Posted
Updated 13-Apr-16 2:39am
Comments
ZurdoDev 13-Apr-16 7:55am    
One way I've done it before is to use NewtsonSoft's json.net because it can serialize a DataTable into json in one line of code. Very easy to use.
CHill60 13-Apr-16 8:26am    
OP is trying to respond to you
Arslan saif 13-Apr-16 7:57am    
Please suggest me some topic for read and solve my problem
CHill60 13-Apr-16 8:26am    
You've been told before that if you want a poster to see your response you must use the "Reply" link next to their post.
Unfortunately because you have a virtually identical post How to return multipule rows data in ADO.NET[^] this looks like a repost. You should make it clear that it is only the generate HTML part that you require an answer to (as you accepted the solutions on the other post).
ZurdoDev 13-Apr-16 8:27am    
I did. Look into using NewtonSoft's json.net.

1 solution

Use this method to convert a datatable to html string

C#
public string GenerateHtmlFromDataTable(DataTable dt )
        {
            //Building an HTML string.
            StringBuilder html = new StringBuilder();

            //Table start.
            html.Append("<table border="1">");

            //Building the Header row.
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<th>");
                html.Append(column.ColumnName);
                html.Append("</th>");
            }
            html.Append("</tr>");

            //Building the Data rows.
            foreach (DataRow row in dt.Rows)
            {
                html.Append("<tr>");
                foreach (DataColumn column in dt.Columns)
                {
                    html.Append("<td>");
                    html.Append(row[column.ColumnName]);
                    html.Append("</td>");
                }
                html.Append("</tr>");
            }

            //Table end.
            html.Append("</table>");

            return html.ToString();


        }


return the html string from your method like
C#
public static string Getdata(string Id)
       {
           string SQL = "select * from testtable where UserId='" + Id + "'";
           DataTable DATA = new DataTable();
           SqlDataAdapter DTA = new SqlDataAdapter(SQL, cnnx);
           DTA.Fill(DATA);
           if (DATA.Rows.Count > 0)
           {
 
               return GenerateHtmlFromDataTable(DATA);
 
           }
           return "";
}


Use script like this

HTML
<script>
        $(document).ready(function () {
            $("#Btnclick").click(function () {
                $.ajax({
                    type: "POST",
                    url: "WebForm1.aspx/Getdata",
                    data: "{'Id':'" + $("#textid").val() + "'}",
                    datatype: "JSON",
                    contentType: "application/json; charset=ut-8",
                    success:function(data)
                    {
                        alert(data.d);
                        $("#divtable").html(data.d)
                       
                    },
                    error: function () {
                        alert("error");
                    }
                   
 
                });
 
            });
 
 
 
        });
    </script>



Hope this help...
 
Share this answer
 
Comments
Richard Deeming 13-Apr-16 9:57am    
You have copied the SQL Injection[^] vulnerability from the question.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

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