connect jqgrid to databasehttp://forums.asp.net/t/1624546.aspx/1?connect+jqgrid+to+databaseMon, 22 Nov 2010 11:22:06 -050016245464173888http://forums.asp.net/p/1624546/4173888.aspx/1?connect+jqgrid+to+databaseconnect jqgrid to database <p>&nbsp;hi</p> <p>I have a jqgrid and I want to connect to database in c# (Webforms) and I write below code,but it doesn t show any data in my jqgrid</p> <p>would you please check my code and tell me what should I do?</p> <p>&lt;!------Gridpage.aspx-----------!&gt;</p> <pre class="prettyprint">&lt;head runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;link rel=&quot;Stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;Themes/jquery-ui-1.8.2.custom.css&quot; /&gt; &lt;link rel=&quot;Stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;Themes/ui.jqgrid.css&quot; /&gt; &lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt; &lt;script type=&quot;text/javascript&quot; src=&quot;js/grid.locale-en.js&quot;&gt;&lt;/script&gt; &lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.jqGrid.min.js&quot;&gt;&lt;/script&gt; &lt;script type=&quot;text/javascript&quot; &gt; jQuery(document).ready(function () { jQuery(&quot;#Grid1&quot;).jqGrid({ url: 'data.aspx', datatype: 'json', mtype: 'GET', colNames: ['BookID', 'BookName'], colModel: [{ name: 'BookID', index: 'BookID', width: 200 }, { name: 'BookName', index: 'BookName', width: 300}], pager: jQuery('#pager'), sortname: 'id', viewrecoreds: true, imgpath: 'Themes/images' }).navGrid(pager,{edit:true,add:true,del:true,refresh:true,search:true}); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;table id=&quot;Grid1&quot; class=&quot;scroll&quot; align=&quot;center&quot; width=&quot;100%&quot;&gt;&lt;/table&gt; &lt;div id=&quot;pager&quot; class=&quot;scroll&quot; style=&quot;text-align:center;&quot;&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt;</pre><pre class="prettyprint"> public DataTable GetDataTable(string sidx, string sord, int page, int pageSize) { int startIndex = (page - 1) * pageSize; int endIndex = page * pageSize; string sql = @"WITH PAGED_CUSTOMERS AS { SELECT BookID, BookName ROW_NUMBER() OVER (ORDER BY " + sidx + @" " + sord + @") AS RowNumber FROM BOOKS }"; DataTable dt = new DataTable(); SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=.;Initial Catalog=library;Integrated Security=True"].ConnectionString); SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlcon); var rows = adapter.Fill(dt); Console.Write("rows"); return dt; }</pre><BR></PRE> <p><br> &nbsp;</p> 2010-11-18T06:38:55-05:004173938http://forums.asp.net/p/1624546/4173938.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>ofcourse it wont . in grid.base.js &nbsp;check out addJSONData and see how result should be sent from server</p> <p>To give you some idea modified your code . go through this</p> <p><br> </p> <p></p> <pre class="prettyprint">public struct s_GridResult { public int page; // Current page of data that is being viewed/requested public int total; // Total number of pages avaialble to view in the entire list public int records; // Number of records available in the rows[] array public s_RowData[] rows; // Rows of data } public struct s_RowData { public int id; // ItemID value for the row public string[] cell; // Array of strings that hold the field values for the given row } /// public s_GridResult GetDataTable(string sidx, string sord, int page, int pageSize) { int startIndex = (page - 1) * pageSize; int endIndex = page * pageSize; string sql = @&quot;WITH PAGED_CUSTOMERS AS { SELECT BookID, BookName ROW_NUMBER() OVER (ORDER BY &quot; &#43; sidx &#43; @&quot; &quot; &#43; sord &#43; @&quot;) AS RowNumber FROM BOOKS }&quot;; DataTable dt = new DataTable(); SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;Data Source=.;Initial Catalog=library;Integrated Security=True&quot;].ConnectionString); SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlcon); var rows = adapter.Fill(dt); s_GridResult result = new s_GridResult(); List&lt;s_RowData&gt; rowsadded = new List&lt;s_RowData&gt;(); // // here for each row in dt create new row object of s_RowData and add the row in a list like // s_RowData newrow = new s_RowData(); // newrow.id = item.ID; // newrow.cell = new string[fields.Length]; //total number of columns // newrow.cell[0] = &quot;Content for first column&quot;; ..etc // rowsadded.Add(newrow); // result.page = page; result.total = dt.rows.count ... result.record = rowsadded.count return result; }</pre> <p><br> <br> </p> <p></p> <p><br> </p> 2010-11-18T07:08:51-05:004177089http://forums.asp.net/p/1624546/4177089.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>thanks for your&nbsp;consideration</p> <p>but it still doesnt work.I want to create jqgrid but my problem is that I dont know how to connect my jqgrid to my database.</p> <p>in my database I have a&nbsp; table(Books) which has 9 fields (BookId,BookName,MenuID,RegDate,Author,Publisher,Price,Description,Status)</p> <p>I want to show&nbsp;all this fields in jqgrid.what should I do?&nbsp;</p> <p>in all codes that&nbsp; I found the page that connect to database is written in php or mvc but&nbsp; I want&nbsp;to write in c#&nbsp;(webform).</p> <p>would you please help me more&nbsp;</p> <p>&nbsp;</p> 2010-11-20T07:09:39-05:004177102http://forums.asp.net/p/1624546/4177102.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>Did u try and modified &nbsp;what i wrote &nbsp;, because that does cover what you asking for .. what you can do is , show me the updated code and i can try help to complete it .</p> 2010-11-20T07:20:52-05:004177107http://forums.asp.net/p/1624546/4177107.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>also i noticed &nbsp; &nbsp;your &nbsp;jquery code is wrong a bit &nbsp;, pager &nbsp;should be id and not jquery element also the url . &nbsp;try this</p> <pre class="prettyprint">jQuery(&quot;#Grid1&quot;).jqGrid({ url: 'data.aspx/GetDataTable', datatype: 'json', mtype: 'GET', colNames: ['BookID', 'BookName'], colModel: [{ name: 'BookID', index: 'BookID', width: 200 }, { name: 'BookName', index: 'BookName', width: 300}], pager: '#pager', sortname: 'id', viewrecoreds: true, imgpath: 'Themes/images' }).navGrid(pager,{edit:true,add:true,del:true,refresh:true,search:true});</pre> <p><br> <br> </p> <p><br> </p> 2010-11-20T07:25:36-05:004177117http://forums.asp.net/p/1624546/4177117.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <pre class="prettyprint">public struct s_GridResult { public int page; public int total; public int record; public s_RowData[] rows; } public struct s_RowData { public int id; public string[] cell; } public s_GridResult GetDataTable(string sidx, string sord, int page, int pagesize) { int startindex = (page - 1) * pagesize; int endindex = page * pagesize; string sql = @&quot; WITH PAGED_CUSTOMERS AS { SELECT BookID,BookName ROW_NUMBER() OVER(ORDER BY &quot; &#43; sidx &#43; @&quot; &quot; &#43; sord &#43; @&quot;) AS ROWNUMBER FROM BOOKS}&quot;; DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;Data Source=.;Initial Catalog=library;Integrated Security=True&quot;].ConnectionString); SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); var rows = adapter.Fill(dt); s_GridResult result = new s_GridResult(); List&lt;s_RowData&gt; rowsadded = new List&lt;s_RowData&gt;(); //I dont know what should I write here result.page = page; result.total = dt.Rows.Count; result.record = rowsadded.Count; return result; }</pre> <p><br> &nbsp;</p> 2010-11-20T07:37:22-05:004177167http://forums.asp.net/p/1624546/4177167.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>update getdatatable &nbsp;to this &nbsp; . This doesnt take care of paging right now , but we can go to that on next level.</p> <pre class="prettyprint">[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public s_GridResult GetDataTable(string sidx, string sord, int page, int pagesize) { int startindex = (page - 1) * pagesize; int endindex = page * pagesize; string sql = @&quot; WITH PAGED_CUSTOMERS AS { SELECT BookID,BookName ROW_NUMBER() OVER(ORDER BY &quot; &#43; sidx &#43; @&quot; &quot; &#43; sord &#43; @&quot;) AS ROWNUMBER FROM BOOKS}&quot;; DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;Data Source=.;Initial Catalog=library;Integrated Security=True&quot;].ConnectionString); SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); var rows = adapter.Fill(dt); s_GridResult result = new s_GridResult(); List&lt;s_RowData&gt; rowsadded = new List&lt;s_RowData&gt;(); int idx = 1; foreach (DataRow row in dt.Rows) { s_RowData newrow = new s_RowData(); newrow.id = idx&#43;&#43;; newrow.cell = new string[2]; //total number of columns newrow.cell[0] = row[0].ToString(); newrow.cell[1] = row[1].ToString(); rowsadded.Add(newrow); } result.rows = rowsadded.ToArray(); result.page = page; result.total = dt.Rows.Count; result.record = rowsadded.Count; return result; }</pre> <p><br> <br> </p> <p></p> 2010-11-20T08:34:18-05:004177330http://forums.asp.net/p/1624546/4177330.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>I write the exact code you told me but it still doesnt work. grid is created but my data does'nt&nbsp; load in it and it`s empty.</p> <p>would you please tell me what to do?&nbsp;&nbsp;</p> 2010-11-20T12:03:30-05:004177340http://forums.asp.net/p/1624546/4177340.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>can u open in firefox and see what is &nbsp;the response result ?&nbsp;</p> <p>use <a href="https://addons.mozilla.org/en-US/firefox/addon/1843/">https://addons.mozilla.org/en-US/firefox/addon/1843/</a>&nbsp;&nbsp;for checking out what gets posted and what is the response</p> 2010-11-20T12:29:19-05:004177394http://forums.asp.net/p/1624546/4177394.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>Ok &nbsp;i understand your problem now :) &nbsp;.... loads of stuff to do make jqgrid &nbsp;to work with webmethod&nbsp;</p> <p>firstly download JSON plugin from this link&nbsp;</p> <p><a href="http://code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.min.js">http://code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.min.js</a></p> <p>then your &nbsp;jquery code should look like this</p> <p><br> </p> <pre class="prettyprint">// change these references as per your own solution &lt;script src=&quot;Scripts/jquery-1.4.1.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; &lt;script src=&quot;Scripts/jquery.json-2.2.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; &lt;script src=&quot;Scripts/jquery-ui-1.8.5.custom.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; &lt;script src=&quot;src/i18n/grid.locale-en.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; &lt;script src=&quot;src/jquery.jqGrid.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; &lt;script&gt; $(document.ready(){ // Update default settings //required to make it work for JSON data handling // this just overrides ajax settings and datatype $.extend($.jgrid.defaults, {datatype: 'json'}, {ajaxGridOptions: { contentType: &quot;application/json&quot; , success: function(data, textStatus) { if (textStatus == &quot;success&quot;) { var thegrid = $(&quot;#Grid1&quot;)[0]; thegrid.addJSONData(data.d); thegrid.grid.hDiv.loading = false; switch (thegrid.p.loadui) { case &quot;disable&quot;: break; case &quot;enable&quot;: $(&quot;#load_&quot; &#43; thegrid.p.id).hide(); break; case &quot;block&quot;: $(&quot;#lui_&quot; &#43; thegrid.p.id).hide(); $(&quot;#load_&quot; &#43; thegrid.p.id).hide(); break; } } } } }); jQuery(&quot;#Grid1&quot;).jqGrid({ url: 'WebForm3.aspx/GetDataTable', dataType: &quot;json&quot;, mtype: 'POST', colNames: ['BookID', 'BookName'], colModel: [{ name: 'BookID', index: 'BookID', width: 200 }, { name: 'BookName', index: 'BookName', width: 300}], pager: '#pager', sortname: 'id', viewrecoreds: true, imgpath: 'Themes/images', serializeGridData: function(data) { return $.toJSON(data); /// done to override default serialization } }).navGrid(&quot;#pager&quot;, { edit: true, add: true, del: true, refresh: true, search: true }); }); &lt;/script&gt;</pre><p>Now &nbsp;update the webmethod &nbsp;definition to this</p><p><br></p><pre class="prettyprint">[WebMethod] public static s_GridResult GetDataTable(string _search ,string nd ,int rows ,int page ,string sidx , string sord ) { int startindex = (page - 1) * pagesize; int endindex = page * pagesize; string sql = @" WITH PAGED_CUSTOMERS AS { SELECT BookID,BookName ROW_NUMBER() OVER(ORDER BY " + sidx + @" " + sord + @") AS ROWNUMBER FROM BOOKS}"; DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=.;Initial Catalog=library;Integrated Security=True"].ConnectionString); SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); var rows = adapter.Fill(dt); s_GridResult result = new s_GridResult(); List&lt;s_RowData&gt; rowsadded = new List&lt;s_RowData&gt;(); int idx = 1; foreach (DataRow row in dt.Rows) { s_RowData newrow = new s_RowData(); newrow.id = idx++; newrow.cell = new string[2]; //total number of columns newrow.cell[0] = row[0].ToString(); newrow.cell[1] = row[1].ToString(); rowsadded.Add(newrow); } result.rows = rowsadded.ToArray(); result.page = page; result.total = dt.Rows.Count; result.record = rowsadded.Count; return result; }</pre> <p><br> <br> </p> <p>Hope this works for u now :) let me know if you get stuck up on something</p> 2010-11-20T13:50:42-05:004177403http://forums.asp.net/p/1624546/4177403.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>But do understand this is just a quick fix , to give you some idea how to do it . But if you want a long term and permanent &nbsp;solution &nbsp;,</p> <p>then you should customize &nbsp;grid.base.js .&nbsp;</p> 2010-11-20T14:03:12-05:004177458http://forums.asp.net/p/1624546/4177458.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>I checked it with firebug and it seems that&nbsp;data.aspx/GetDataTable in html page it&nbsp;is not&nbsp; invoked.</p> <p>what should i do?</p> <p><font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"></font></font></font></p> <p>&nbsp;</p> <p></p> <p>&nbsp;</p> 2010-11-20T15:06:10-05:004177465http://forums.asp.net/p/1624546/4177465.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>did u try the updated code i posted?</p> 2010-11-20T15:11:46-05:004177471http://forums.asp.net/p/1624546/4177471.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>no I cant download&nbsp; this jquery.json from above link ,would you do me a favor and send&nbsp;this file to my email adress please?</p> <p>my email address is <a href="mailto:bahareh992sh@yahoo.com">bahareh992sh@yahoo.com</a>&nbsp; .I would appreciate it.thank you</p> 2010-11-20T15:18:40-05:004177486http://forums.asp.net/p/1624546/4177486.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>done , let me know if you still get probs with this&nbsp;</p> 2010-11-20T15:36:50-05:004177515http://forums.asp.net/p/1624546/4177515.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>your email was empty! Did you send jquery.json.min.js?&nbsp;</p> 2010-11-20T16:14:30-05:004177609http://forums.asp.net/p/1624546/4177609.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>done again</p> 2010-11-20T18:30:35-05:004178123http://forums.asp.net/p/1624546/4178123.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>thanks alot for your email </p> <p>when I run my grid I faced with this error:</p> <p><strong>Microsoft JScript runtime error: Object doesn't support this property or method&nbsp;</strong></p> <p>and I really dont know what to do now?!!</p> 2010-11-21T15:04:33-05:004178133http://forums.asp.net/p/1624546/4178133.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>check references for javascript files in your page &nbsp;, they may be not correct.&nbsp;</p> 2010-11-21T15:25:54-05:004178608http://forums.asp.net/p/1624546/4178608.aspx/1?Re+connect+jqgrid+to+databaseRe: connect jqgrid to database <p>when I run my grid I faced with this error :<span><strong>document.ready is not a function </strong></span></p> <p><span>this error is shown in Firebug and my code is exactly similar as your code.!</span></p> 2010-11-22T05:45:12-05:00