Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting no error codes just clicking Button1 and nothing happens.

I was an having error with the "length" in data.d so I added "var data1 = data.d" that did not corrected that.

Any suggestions?

Default.aspx
C#
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#<%= Button1.ClientID %>").click(function () {
            $.ajax({
                type: "POST",
                url: "Default.aspx/gvAjax",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{}",
                async: true,
                cache: false,
                success: function (data) {
                    $("#<%= GridView1.ClientID %>").empty();
                    var data1 = data.d;
                    for (var i = 0; i < data1.length; i++) {

                        $("#<%= GridView1.ClientID %>").append("<tr><td>" + data1[i].Id + "</td><td>" + data1[i].col1 + "</td><td>" + data1[i].col2 + "</td><td>" + data1[i].col3 + "</td></tr>");
                    }
                },
                error: function (x, e) {
                    alert("Error: " + x.responseText);
                }
            });
        });
    });

Default.aspx.cs
C#
[System.Web.Services.WebMethod]
public static string gvAjax(int id)
{
    List<Table1> gvlist = new List<Table1>();
    var json1 = "";
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string connStr = ConfigurationManager.ConnectionStrings["connContext"].ConnectionString;
    string cmdStr = "SELECT * FROM [Table1];";
    using (SqlConnection conn = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
        {
            conn.Open();
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(ds);
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {

                    Table1 tb1 = new Table1();
                    tb1.Id = Convert.ToInt32(dr["Id"]);
                    tb1.col1 = dr["col1"].ToString();
                    tb1.col2 = dr["col2"].ToString();
                    tb1.col3 = dr["col3"].ToString();
                    gvlist.Add(tb1);
                }
            }
        }
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        json1 = serializer.Serialize(gvlist);
        return json1;
    }
}
Posted
Updated 19-Feb-15 5:34am
v2

Check if data contains some records.
If it is null and you try and access d, you will get an error.
 
Share this answer
 
Comments
teledexterus 19-Feb-15 11:53am    
connContext or conndb.mdf contains data in Table1 for all the columns listed.
Replace you success block with :
XML
success: function (data) {
                        $("#GridView1").empty();
                        var data1 = data.d;
//incase you want some headers
$("#GridView1").append("<tr><th>Id</th><th>Col1</th><th>Col2</th><th>Col3</th></tr>")
                        for (var i = 0; i < data1.length; i++) {

                            $("#GridView1").append("<tr><td>" + data1[i].Id + "</td><td>" + data1[i].col1 + "</td><td>" + data1[i].col2 + "</td><td>" + data1[i].col3 + "</td></tr>");
                        }
                    }


Hope this helps!!
 
Share this answer
 

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