c# - Binding RadGrid To DataTable ASP.NET -


what trying before saving records database want show records in gridview when user fill text box , click on button record shown on gridview serial number 1 , 2 , 3 ... far have approach . first records added when adding second record datatable give null reference exception . right approach ? or there easy way . using telerik radgrid asp.net .

 public datatable dt;  protected void page_load(object sender, eventargs e)     {         if (!ispostback)         {             dt = new datatable();             dt.columns.add("sn#");             dt.columns.add("type");             dt.columns.add("accounttitle");             dt.columns.add("description");             dt.columns.add("costcenter");             dt.columns.add("debit");             dt.columns.add("credit");             sno.text = "1";         }      }    protected void radgridview2_needdatasource(object sender, gridneeddatasourceeventargs e)     {             dt = addrow(dt);  // call method create row              viewstate["dt"] = dt;             dt = (datatable)viewstate["dt"];             radgridview2.datasource = dt;           }      private datatable addrow(datatable dt)     { // method create row              datarow dr = dt.newrow();             dr[0] = sno.text;             dr[1] = type.text;             dr[2] = htitle.text;             dr[3] = disc.text;             dr[4] = job.selecteditem.text;             dr[5] = drr.text;             dr[6] = crr.text;             dt.rows.add(dr);          return dt;     }          protected void b1_click(object sender, eventargs e)     {          datatable dt = (datatable)viewstate["dt"];                viewstate["dt"] = addrow(dt);         radgridview2.rebind();         session["cttt"] = convert.toint32(sno.text)+1;         sno.text = session["cttt"].tostring();      } 

i have copy code , change bit. due dt declared twice. believe error datatable null

.aspx

<asp:scriptmanager id="sm" runat="server"></asp:scriptmanager>     <div>         <telerik:radgrid id="rg" runat="server" onneeddatasource="radgrid1_needdatasource"></telerik:radgrid>         <br />         <asp:textbox id="sno" runat="server"></asp:textbox><br />         <asp:textbox id="type" runat="server" text="a"></asp:textbox><br />         <asp:textbox id="htitle" runat="server" text="b"></asp:textbox><br />         <asp:textbox id="disc" runat="server" text="c"></asp:textbox><br />         <asp:dropdownlist id="job" runat="server"><asp:listitem>yeah</asp:listitem></asp:dropdownlist><br />         <asp:textbox id="drr" runat="server" text="d"></asp:textbox><br />         <asp:textbox id="crr" runat="server" text="e"></asp:textbox><br />         <br />         <br />         <asp:button id="b1" runat="server" text="click" onclick="b1_click" />      </div> </asp:scriptmanager> 

.cs

private datatable dt;  protected void page_load(object sender, eventargs e) {     if (!ispostback)     {         sno.text = "1";     } }  private datatable createdatatable() {     dt = new datatable();     dt.columns.add("sn#");     dt.columns.add("type");     dt.columns.add("accounttitle");     dt.columns.add("description");     dt.columns.add("costcenter");     dt.columns.add("debit");     dt.columns.add("credit");      return dt; }  private datatable addrow(datatable dt1) { // method create row      datarow dr = dt1.newrow();     dr[0] = sno.text;     dr[1] = type.text;     dr[2] = htitle.text;     dr[3] = disc.text;     dr[4] = job.selecteditem.text;     dr[5] = drr.text;     dr[6] = crr.text;     dt1.rows.add(dr);      return dt1; }    protected void radgrid1_needdatasource(object sender, telerik.web.ui.gridneeddatasourceeventargs e) {     rg.datasource = viewstate["dt"] datatable;     }  protected void b1_click(object sender, eventargs e) {     datatable dt1 = viewstate["dt"] != null ? viewstate["dt"] datatable : createdatatable();     viewstate["dt"] = addrow(dt1);     rg.rebind();     session["cttt"] = convert.toint32(sno.text) + 1;     sno.text = session["cttt"].tostring(); } 

result
enter image description here


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -