3 Replies - 3371 Views - Last Post: 18 February 2013 - 01:24 AM Rate Topic: -----

#1 rapo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 27-November 08

Keep information in DataTable

Posted 14 February 2013 - 03:08 PM

Hello,

I am a newbie in Asp.net technology I need someone's help. I have next situation:

A gridview, two dropdownlists and a button, all of them in a UpdatePanel. When the selection from the first ddl changes, a request is made in MS SQL with LINQ to load data in the second ddl. When the button is pressed another request in MS SQL is made to get information to load in gridview. The problem is: I can't find the manner to create the Datatable to store the data from database when I press that button. Everytime the datatable has the last value, on every click event a new instance of data table is made. How can I solve this issue.
This a part of my code:

 public partial class Blabla: BasePage
 {
  private DataTable _table = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
              _table = new DataTable("Table");
              _table .Columns.Add(new DataColumn("Col1",typeof(string)));
              _table .Columns.Add(new DataColumn("Col2", typeof (string)));
              _table .Columns.Add(new DataColumn("Col3", typeof (string)));

              gridI.DataSource = _table ;
              gridI.DataBind();
            }
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            var list = RequestDB();
            DataRow row_n = ReturnANewDataTableRow();

            row_n["Col1"] = (_table.Rows.Count + 1).ToString();
            row_n["Col2"] = list[0].Filed1.ToString();
            row_n["Col3"] = list[0].Field2.ToString();
            _table.Rows.Add(row_n);
            gridI.DataSource = _table;
            gridI.DataBind();

        }
 }





<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
   <table width="100%">
      <tr>
         <td style="width: 353px">
           <asp:DropDownList ID="ddlI" runat="server" Width="560px" AutoPostBack="True"
              onselectedindexchanged="ddlI_SelectedIndexChanged" >
           </asp:DropDownList>
           <br/>
         <asp:DropDownList ID="ddlD" runat="server" Width="560px" 
            onselectedindexchanged="ddlD_SelectedIndexChanged" >
         </asp:DropDownList>
          </td>
          </td>
          <td style="width: 154px">
            <asp:Button ID="btAdd" runat="server" onclick="btAdaugaAbatere_Click" 
               Text="Add" Width="100px" /> 
           </td>
           </tr>
           <tr>
              <td colspan="3">
                  <asp:Panel ID="panelC" runat="server">
                     <asp:GridView ID="gridI" runat="server" Width="100%">
                     </asp:GridView>
                  </asp:Panel>
               </td>
            </tr>
      </table>
     </td>
 </ContentTemplate>
</asp:UpdatePanel>



Is This A Good Question/Topic? 0
  • +

Replies To: Keep information in DataTable

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1176
  • View blog
  • Posts: 1,662
  • Joined: 24-August 11

Re: Keep information in DataTable

Posted 14 February 2013 - 03:52 PM

I'm not sure about this, but look into this.

You're using an UpdatePanel, so you're not ever posting back the full page. So if if (!Page.IsPostBack) continues to be true, and the Page_Load function evaluates again, _table = new DataTable("Table"); will evaluate again.

Again, I'm not sure if I'm right, but it's worth looking into.

This post has been edited by h4nnib4l: 14 February 2013 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Keep information in DataTable

Posted 15 February 2013 - 02:35 PM

The !IsPostBack is correct, a postback inside an UpdatePanel is still a postback.

However, during the postback your _table variable has no value. You have to remember that the web is a stateless environment. You either need to reload the _table variable every postback or store it in Session or ViewState.

You could replace your private field with a property like this
private DataTable MyDataTable
{
    get
    {
        object temp = Session["MyDataTable"];
        return temp == null ? null : temp as DataTable;
    }
    set
    {
        Session["MyDataTable"] = value;
    }
}



That should persist your DataTable across postbacks without having to reload it from the database.
Was This Post Helpful? 1
  • +
  • -

#4 rapo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 27-November 08

Re: Keep information in DataTable

Posted 18 February 2013 - 01:24 AM

Thank you for ideas. I've solved this problem by putting the DataTable on ViewState. This is proper solution in my opinion because the DataTable is small, maximum 10 rows on 4 columns.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1