<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>
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(); }