c# - Error when paging in GridView -


i have gridview 2 columns. first shows date , second, year of date in first column. far works when change page, web fails message: "object reference not set instance of object."

this code:

test.aspx

<%@ page language="c#" autoeventwireup="true" codefile="test.aspx.cs" inherits="qq_site_test" %>  <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title>test</title> </head> <body>     <form id="form1" runat="server">     <div>         <asp:gridview id="gridview1" runat="server" allowpaging="true" pagesize="5"              allowsorting="true" autogeneratecolumns="false"              enablemodelvalidation="true" onrowcreated="gridview1_rowcreated"              onpageindexchanged="gridview1_pageindexchanged"              onpageindexchanging="gridview1_pageindexchanging">             <columns>                 <asp:commandfield showselectbutton="true" />                 <asp:commandfield showdeletebutton="true" />                 <asp:boundfield datafield="creationdate" headertext="creationdate" sortexpression="creationdate" />                 <asp:templatefield headertext="year">                     <itemtemplate>                         <asp:textbox id="textbox1" runat="server" readonly="true"></asp:textbox>                     </itemtemplate>                 </asp:templatefield>                             </columns>         </asp:gridview>     </div>     </form> </body> </html> 

test.aspx.cs

using system; using system.collections.generic; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.odbc; using system.data;  public partial class qq_site_test : system.web.ui.page {     static dataset ds;      protected void page_load(object sender, eventargs e)     {         if (!page.ispostback)         {             string connectionstring = "dsn=kitchenmaster.es.qq-site;";             string sqlquery = "select * tblsystems id < 100";              ds = new dataset();              odbcconnection connection = new odbcconnection(connectionstring);             odbccommand command = new odbccommand(sqlquery, connection);             odbcdataadapter adapter = new odbcdataadapter(command);              try             {                 adapter.fill(ds);                 gridview1.datasource = ds;                 gridview1.databind();             }             catch (system.exception ex)             {                 response.write(ex.message);             }                         {                 if (connection.state == connectionstate.open)                     connection.close();             }         }     }      protected void gridview1_rowcreated(object sender, gridviewroweventargs e)     {         if (e.row.rowtype == datacontrolrowtype.datarow)         {             try             {                 datetime dt = (datetime)databinder.eval(e.row.dataitem, "creationdate");                 e.row.cells[getcolumnbyid("year")].text = dt.year.tostring();             }             catch (system.exception ex)             {                 response.write(ex.message);                          }         }     }      protected int getcolumnbyid(string columnname)     {         foreach (datacontrolfield column in gridview1.columns)         {             if (column.headertext == columnname)                 return gridview1.columns.indexof(column);         }          return -1;     }      protected void gridview1_pageindexchanged(object sender, eventargs e)     {         gridview1.selectedindex = -1;     }      protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)     {         try         {             if (e.newpageindex != -1)             {                 gridview1.pageindex = e.newpageindex;                 gridview1.datasource = ds;                 gridview1.databind();             }         }         catch (system.exception ex)         {             response.write(ex.message);         }     } } 

can me?

this line of code

gridview1.datasource = ds; 

is getting error.

when gridview1_pageindexchanging event called, have not yet set value ds. part in code in page_load event, , when it's not postback.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -