Tuesday, 23 July 2013

How to store datatable in viewstate:

How to store datatable in viewstate:

Description:

In this example i will get data table from database and store it in viewstate in page load event and then in button click event i will get data table from viewstate and populates it in gridview. This is the example how to store large volumes of data in viewstate.

     Take form which contains a label,a button and gridview like below
  1. <form id="form1" runat="server">  
  2.    <div>  
  3.      
  4.        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"   
  5.            GridLines="None"   
  6.            style="z-index: 1; left: 279px; top: 121px; position: absolute; height: 133px; width: 187px">  
  7.            <alternatingrowstyle BackColor="White" />  
  8.            <editrowstyle BackColor="#7C6F57" />  
  9.            <footerstyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  10.            <headerstyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  11.            <pagerstyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
  12.            <rowstyle BackColor="#E3EAEB" />  
  13.            <selectedrowstyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
  14.            <sortedascendingcellstyle BackColor="#F8FAFA" />  
  15.            <sortedascendingheaderstyle BackColor="#246B61" />  
  16.            <sorteddescendingcellstyle BackColor="#D4DFE1" />  
  17.            <sorteddescendingheaderstyle BackColor="#15524A" />  
  18.        </asp:GridView>  
  19.        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
  20.            style="z-index: 1; left: 284px; top: 80px; position: absolute"   
  21.            Text="LoadData" />  
  22.        <asp:Label ID="Label1" runat="server" BackColor="#CCCCCC" ForeColor="#FF0066"   
  23.            style="z-index: 1; left: 325px; top: 54px; position: absolute; width: 354px"></asp:Label>  
  24.      
  25.    </div>  
  26. lt;/form>  

In the page load event and button click event write below code
  1. protected void Page_Load(object sender, EventArgs e)  
  2.    {  
  3.        SqlConnection con = new SqlConnection("database=cs9db;user id=sa;password=9290812112;data source=(local);");  
  4.        SqlDataAdapter da = new SqlDataAdapter("Select * from emp where deptno="+30, con);  
  5.        DataTable dt = new DataTable();  
  6.        da.Fill(dt);  
  7.        ViewState["EmpTable"] = dt;// storing datatable in viewstate  
  8.        Label1.Text = "Data Table stored in viewstate";  
  9.    }  
  10.    protected void Button1_Click(object sender, EventArgs e)  
  11.    {  
  12.        GridView1.DataSource = (DataTable)ViewState["EmpTable"];// retrieving datatable from viewstate  
  13.        GridView1.DataBind();  
  14.        Label1.Text = "View state datatable is displayed in gridview";  
  15.    }  


In the page load event i am getting datatable from emp table and storing it in viewstate. in button click event i am getting viewstate emptable and binding that table to gridview. In this way you can store large amount of data in viewstate but it will increase the page size.



No comments:

Post a Comment