DataGrid and ListBox problem...

N

Nischal

Hello All,

I am trying build a web form with a listbox and datagrid on it.

1. I have a dataset into which I load an xml file using ReadXml()
method.

private DataSet LoadDataSet(string FileName)
{
DataSet ds = new DataSet();
ds.ReadXml(FileName);
return ds;
}

2. I then populate all the tablenames into the listbox

private void LoadTableList(DataSet ds)
{
this.lstTables.Items.Clear();
foreach(DataTable dt in ds.Tables)
{
this.lstTables.Items.Add(dt.TableName);
}
this.lstTables.SelectedIndex = 0;
}

3. In the SelectedIndexChanged event of the listbox I am trying to
load the rows for the particular table into the datagrid

private void LoadDataGrid(string tbName)
{
this.dg.DataSource = this.ds.Tables[tbName].DefaultView;
this.dg.DataBind();
}

4. Here is how I am calling the above functions:

DataSet dsMain = new DataSet();
dsMain = LoadDataSet(Server.MapPath("Test.Config"));
LoadTableList(dsWebConfig);
private void lstTables_SelectedIndexChanged(object sender,
System.EventArgs e) { LoadDataGrid(this.lstTables.SelectedItem.Text);
}

PROBLEM: The SelectedIndexChanged event is throwing me an exception
saying the dataset is empty, when infact I populated the dataset. I
did set the AutoPostBack = true. And I am using if(!IsPostBack) in my
page_load event.

Can someone help me where I am going wrong.

Appreciate your time.

Thanks
Nischal
 
I

Imtiaz Hussain

May be this is what is happening.
You change the value in the listbox and this causes a postback, in the
pageload method you are checking for !IsPostBack and if its a postback you
do not populate the dataset and that might be what the error states.

Hope this helps,
Imtiaz Hussain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top