Dawood Abbas

Dawood Abbas

  • NA
  • 264
  • 98.2k

How to get Array values from webmethod to jquery for display

Aug 26 2015 8:44 AM
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.

Hide Copy Code
<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.

Hide Expand Copy Code
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?

Answers (6)