How to store the values in datatable between postbacks

Rate It (1)

Last post 05-14-2008 4:52 AM by Rizwan328. 1 replies.

Sort Posts:

  • How to store the values in datatable between postbacks

    05-14-2008, 2:45 AM
    • Loading...
    • Sharonrao123
    • Joined on 02-07-2007, 3:06 AM
    • Sydney, Australia
    • Posts 238

    Hello All,

    I have to store values selected by the user in a datatable and display in datagrid. My question is how i can store value the value selected by the user without loosing the previous value stored in datatable.

    Any suggestions are appreciated. Thanks in advance, Shilpa.

    Here is the code i have written but dosent work as the old value is replaced with new one after postback

    protected void ddlList_SelectedIndexChanged(object sender, EventArgs e)

    {

    Txt.Text = ddlList.SelectedValue;

    DataTable dt = new DataTable();

    DataColumn dc = new DataColumn();

    dc.DataType = Type.GetType("System.Int32");dc.ColumnName = "property_option_idx";

    dt.Columns.Add(dc);

    DataRow dr = dt.NewRow();

    // row["id"] = Guid.NewGuid().ToString();

    dr["property_option_idx"] = ddlList.SelectedValue;

    dt.Rows.Add(dr);

    Session[
    "dt"] = dt;GridView1.DataSource = ((DataTable)Session["dt"]).DefaultView;

    GridView1.DataBind();

    // this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;

    }

  • Re: How to store the values in datatable between postbacks

    05-14-2008, 4:52 AM
    Answer
    • Loading...
    • Rizwan328
    • Joined on 05-25-2006, 6:26 AM
    • Dubai, UAE
    • Posts 249

    Sharonrao123:

    DataTable dt = new DataTable();

     

     

    you are creating net DataTable object each time........you should check from Session["dt"] if it's not null.....and then cast this object to Datatable and simply add rows to existing table.....not create new DataTable each time

    If this post helps you, please mark it as Answer.



    Cheers,
    Muhammad Rizwan Javed.
    Software Engineer
    Aim 168 Digital,Dubai, UAE.



Page 1 of 1 (2 items)