Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi implemented jquery grid using json and dictionaries.
I am getting error with Request.InputStream. i am always getting null value.
If i pass static inputs method "GetData" it showing empty grid.

   public class EmpDetails
   {
       public int id;
       public string[] cell;
   }
   public class JsonEmployeeData
   {
       public int page;
       public int total;
       public int records;
       public EmpDetails[] rows;
   }
    public partial class NewImpUsingJson : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
            if (Request.QueryString["method"] == "GetData")
           {
               Request.InputStream.Position = 0;
               StreamReader ipStRdr = new StreamReader(Request.InputStream);
               string json = ipStRdr.ReadToEnd();
               JavaScriptSerializer jser = new JavaScriptSerializer();
Dictionary<string, string> dict = jser.Deserialize<Dictionary<string, string>>(json);
                    //NewImpUsingJson.GetData(1,10,1,"ASC");
               NewImpUsingJson.GetData(int.Parse(dict["page"].ToString()), int.Parse(dict["rows"].ToString()), int.Parse(dict["_search"].ToString()), dict["sord"].ToString());
               Response.End();
           }
                   }

       [WebMethod]
       [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
       public static JsonEmployeeData GetData(int page, int rows, int _search, string sord)
       {
          //int startindex = (page - 1) * pagesize;
           //int endindex = page * pagesize;
           string queryString = "SELECT Row_Number() over (order by EmployeeId ASC) as rowID, EmployeeId,EmployeeName,ContactNumber FROM (Select ROW_NUMBER() OVER (ORDER BY EmployeeId ASC) AS ROWID," +
           "EmployeeId,EmployeeName,ContactNumber FROM EmployeeJtable) AS a";                    SqlConnection connection = new SqlConnection(@"Data Source=HYD-NPULIVARTHY;Initial Catalog=test2;Integrated Security=True");
               connection.Open();
               DataTable dt = new DataTable();
               SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
               var tRows = adapter.Fill(dt);
               JsonEmployeeData result = new JsonEmployeeData();
               List<EmpDetails> rowsadded = new List<EmpDetails>();
               int index=1;
               foreach (DataRow dtRow in dt.Rows)
               {
                   EmpDetails newrow = new EmpDetails();
                   newrow.id = index++;
                   newrow.cell = new string[3];
                   newrow.cell[0] = dtRow[1].ToString();
                   newrow.cell[1] = dtRow[2].ToString();
                   newrow.cell[2] = dtRow[3].ToString();
                   rowsadded.Add(newrow);
               }
               result.rows = rowsadded.ToArray();
               result.page = page;
               result.total = ((rowsadded.Count + rows - 1) / tRows);
               result.records = rowsadded.Count;
               return result;
       }
   }


and my script code is

<script type="text/javascript">
       $(document).ready(function () {
       var sPage="NewImpUsingJson.aspx"
      jQuery("#Grid1").jqGrid({
          url: sPage + '?method=GetData',
          datatype: 'json',
          mtype: 'GET',
          colNames: ['id','EmployeeId', 'EmployeeName', 'ContactNumber'],
          colModel: [
                   { name: 'id', index: 'id', width: 60, search: false },
                  { name: 'EmployeeId', index: 'EmployeeId', width: 90, align: 'center', sorttype: 'EmployeeId'},
                  { name: 'EmployeeName', index: 'EmployeeName', width: 105, align: 'right' },
                  { name: 'ContactNumber', index: 'ContactNumber', width: 95, align: 'left' }
                  ],
          pager: '#pager',
          sortname: 'EmployeeId',
          viewrecoreds: true,
           serializeGridData: function(data)
             {
             // return  $.toJSON(data);   /// done to override default serialization
                 return JSON.stringify(data);
             },
             jsonReader: { repeatitems: false, root: "rows", page: "page", total: "total", records: "records" }
         });
         jQuery("#Grid1").jqGrid('navGrid', '#Pager', { edit: false, add: false, del: false }, null, null, true, { multipleSearch: true });
         var height = $(window).height();
      });


  </script>




please any one help to implement displaying data in JQGrid using SQL Server 2008 data.
Posted

1 solution

First you need to concentrate on your SqlConnection to see whether you get data from database or not,if it is yes then you should recheck your query.
See a simple but working examples here:
http://www.trirand.net/examples/grid/loading_data/datatable/[^]
http://www.trirand.net/examples/grid/loading_data/million_sql/default.aspx[^]
 
Share this answer
 
v3
Comments
Naga Sindhura 29-Aug-13 11:06am    
through trirand it's working. However i have to implement with the freeware.
I am getting data from DataBase. properbinding is not hapening ,it is displaying empty data.
Iam getting problem with Request.InputStream. showing null values
ridoy 29-Aug-13 11:15am    
does your database has values or they are null?
Naga Sindhura 29-Aug-13 21:46pm    
Hi ridoy i am getting data from my database, however i don't have null values in my database.

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