Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display the data from database on some time interval so I used Timer control, but on every tick fire the div (chat box) minimizing, so I want to avoid this minimizing on every post back I used Jquery to webmethode concept like below.

to call C# array type webmethod.

XML
<script type="text/javascript">
        $(document).ready(function () {
            $("#tblCustomers tbody tr").remove();
            $.ajax({
                type: "POST",
                url: "GetDataByJquery.aspx/GetMessages",
                data: '{roomId: "' + $("[id$=lblRoomId]").html() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        var rows = "<tr>"
                    + "<td class='customertd'>" + item.Username + "</td>"
                    + "<td class='customertd'>" + item.Sex + "</td>"
                    + "<td class='customertd'>" + item.Text + "</td>"
                    + "<td class='customertd'>" + item.TimeStamp + "</td>"
                    + "<td class='customertd'>" + item.UserID + "</td>"
                    + "</tr>";
                        $('#tblCustomers tbody').append(rows);
                    }))
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        });
    </script>


Got data from sqlserver and reterning in array.

public static Messages[] GetMessages(string roomId)
   {
       List<Messages> messages = new List<Messages>();
       string strConnString = ConfigurationManager.ConnectionStrings["LinqChatConnectionString"].ConnectionString;

       using (SqlConnection con = new SqlConnection(strConnString))
       {
           using (SqlDataAdapter sda = new SqlDataAdapter())
           {
               string query = "[Get_Messages]";
               SqlCommand cmd = new SqlCommand(query);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@roomId", roomId);
               cmd.Connection = con;
               sda.SelectCommand = cmd;
               con.Open();
               SqlDataReader reader = cmd.ExecuteReader();

               while (reader.Read())
               {
                   Messages message = new Messages();
                   message.Username = reader.GetString(0);
                   message.Sex = reader.GetString(1);
                   message.Text = reader.GetString(2);
                   message.TimeStamp = reader.GetDateTime(3);
                   message.UserID = reader.GetInt32(4);
                   messages.Add(message);
               }
           }
       }
       return messages.ToArray();
   }


but I can't display the data..so how to display it?
Posted
Comments
Schatak 26-Aug-15 8:58am    
are you able to get data from database to Reader?
Sergey Alexandrovich Kryukov 26-Aug-15 10:31am    
The way you want to display it. What's the problem?
—SA
Homero Rivera 26-Aug-15 21:46pm    
Comment your code in the response function and see what you're getting with alert(data); You seem to be expecting an array of data, but your aspx doesn't seem to use Response.ContentType to declare you'll be replying with an array.

You can send arrays to your response in the form of json, XML, or simple text with your whole HTML ready for pasting.
Dawood507 27-Aug-15 1:00am    
@Schatak .yes i am able to getting all rows data from server.
Dawood507 27-Aug-15 1:02am    
@Sergey Alexandrovich Kryukov .. I dnt know thats why i ask here.

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