views:

98

answers:

3

I have this datatable

public partial class class1
{
  private DataTable dt;
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           dt.Columns.Add("Col1", System.Type.GetType("System.String"));
           dt.Columns.Add("Col2", System.Type.GetType("System.String"));
           bind();
        }
    }
}

private void bind()
{
    //database call
    //loop 
    dt.Rows.Add(col1_value.ToString(), col2_value.ToString(), col3.ToString());
    // populate the dropdown list with the database entries
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DataRow[] datarow;
   variable1=  DropDownList1.SelectedValue;
   datarow = dt.Select( dt.Columns["col1"] + DropDownList1.SelectedValue);
   variable2 = datarow.GetValue(1).ToString();
}

When the selectedindexchange event is called, it displays an error in the line datarow = dt.Select( dt.Columns["col1"] + DropDownList1.SelectedValue); , saying the column does not exist. All the rows and columns from the datatable are lost from the datatable when the selectedIndexChanged is executed. What am I missing here?

I want to avoid storing the datatable in session or viewstate. Also, the question is why does the datatable becomes empty. Is there anyway to avoid repopulating the datatable everything there is a post back? If I have a class variable of string that doesnt become empty after a postback or does it.

+1  A: 

You are adding the columns programmatically in your Page_Load event. The SelectedIndexChanged event causes a postback, and your logic in the Page_Load only adds the columns when the request is NOT a postback. This means that the columns are not added back on each postback, which they must be if you want to add them programmatically.

Dave Swersky
But, If I put the bind() function outside the !IsPostback, it will also repopulate the dropdownlist and reset the selected index.
Hna0002
That is why adding columns programmatically is tricky and no fun ;) What you have to do is populate the list only when !Postback but re-add the columns on every postback.
Dave Swersky
Kewl! Will do that. I just find it inefficient. :) Thank you!
Hna0002
It is inefficient :) Programmatic setup of controls in ASP.NET is to be avoided whenever possible.
Dave Swersky
Any suggestions on how to go about it in my situation?
Hna0002
You shouldn't have to add columns to your datatable explicitly. An ADO.NET call can automatically populate a datatable with the necessary columns.
Dave Swersky
Thanks will make not of it.
Hna0002
+3  A: 

Remove the if (!IsPostBack) .... You have to populate the datatable dt everytime you postback since you aren't storing it anywhere in viewstate or session....

klabranche
Is there a way to avoid re populating the dt without putting it in viewstate or session?
Hna0002
The nature of the web is stateless. Viewstate, session, etc.... are techniques used to overcome this.... Or making calls back and forth from the DB as needed, AND/OR caching results and using the cache....
klabranche
+1  A: 

It may be because of your condition (if (!IsPostBack)), indexchanged event causes postback, so columns are never added to the datatable. You should get rid of this condition or populate your datatable in another place (maby even your eventhandler for indexchanged event).

Jarek