DataTable refill problem

G

Guest

I am using a DataGrid with a dataset and the
OracleDataAdapter.Fill(dataset,"Tablename") method. All is fine: a table is
created and mapped to the dataset and data is shown in the datagrid.

When I clear the table, dispose it and remove it from the dataset, and then
reload the same table with the fill statement, the datagrid shows (NULL)
although the debug output shows that there is a table mapped to the dataset
with populated rows. Although when I try to remove it again by index from the
DataSet, I get a NullPointerException - but the Table is present!

What am I missing? If anyone can verify this strange behaviour?

Test-Code is like this:

namespace TestDsLoadClear
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
/// DeleteButton
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
dataSet1.Tables[0].Clear();
dataSet1.Tables[0].Dispose();
dataSet1.Tables.RemoveAt(0);

button2.Enabled = false;
button1.Enabled = true;
}

/// <summary>
/// LoadButton
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
oracleDataAdapter1.Fill(dataSet1, "NewTable");
button1.Enabled = false;
button2.Enabled = true;
}
}
}
 
P

Paul Clement

¤ I am using a DataGrid with a dataset and the
¤ OracleDataAdapter.Fill(dataset,"Tablename") method. All is fine: a table is
¤ created and mapped to the dataset and data is shown in the datagrid.
¤
¤ When I clear the table, dispose it and remove it from the dataset, and then
¤ reload the same table with the fill statement, the datagrid shows (NULL)
¤ although the debug output shows that there is a table mapped to the dataset
¤ with populated rows. Although when I try to remove it again by index from the
¤ DataSet, I get a NullPointerException - but the Table is present!
¤
¤ What am I missing? If anyone can verify this strange behaviour?
¤
¤ Test-Code is like this:
¤
¤ namespace TestDsLoadClear
¤ {
¤ public partial class Form1 : Form
¤ {
¤ public Form1()
¤ {
¤ InitializeComponent();
¤ }
¤
¤ /// <summary>
¤ /// DeleteButton
¤ /// </summary>
¤ /// <param name="sender"></param>
¤ /// <param name="e"></param>
¤ private void button2_Click(object sender, EventArgs e)
¤ {
¤ dataSet1.Tables[0].Clear();
¤ dataSet1.Tables[0].Dispose();
¤ dataSet1.Tables.RemoveAt(0);
¤
¤ button2.Enabled = false;
¤ button1.Enabled = true;
¤ }
¤
¤ /// <summary>
¤ /// LoadButton
¤ /// </summary>
¤ /// <param name="sender"></param>
¤ /// <param name="e"></param>
¤ private void button1_Click(object sender, EventArgs e)
¤ {
¤ oracleDataAdapter1.Fill(dataSet1, "NewTable");
¤ button1.Enabled = false;
¤ button2.Enabled = true;
¤ }
¤ }
¤ }

I don't believe the DataGrid will display any data until you set the DataSource property to the new
DataTable.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

The datasource property of the datagrid is set in the designer.

I expanded the form with a third button for switching the Datasource:


private void button3_Click(object sender, EventArgs e)
{
if (dataGrid1.DataSource == dataSet1)
{
dataGrid1.DataSource = dataSet1.Tables["NewTable"];

}
else
dataGrid1.DataSource = dataSet1;
}

If the datasource references directly to the datatable, data is shown. If it
references to the dataset, the tree is shown with "NewTable" but NewTable has
0 (1 row with NULL values) rows.

Why is this?? Plz help!!!
 
P

Paul Clement

¤ The datasource property of the datagrid is set in the designer.
¤
¤ I expanded the form with a third button for switching the Datasource:
¤
¤
¤ private void button3_Click(object sender, EventArgs e)
¤ {
¤ if (dataGrid1.DataSource == dataSet1)
¤ {
¤ dataGrid1.DataSource = dataSet1.Tables["NewTable"];
¤
¤ }
¤ else
¤ dataGrid1.DataSource = dataSet1;
¤ }
¤
¤ If the datasource references directly to the datatable, data is shown. If it
¤ references to the dataset, the tree is shown with "NewTable" but NewTable has
¤ 0 (1 row with NULL values) rows.
¤
¤ Why is this?? Plz help!!!
¤

Setting the DataSource property to a DataSet is actually setting it to all DataTables in the
DataSet. That is why you get a TreeView display. It's allowing you to select amongst all the
DataTables in your DataSet.

If you want to see data immediately then you need to set the DataSource to a specific DataTable.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

Paul,

this is not the question. The problem is that if I click on the tree to
select the "newTable", "newTable" has 0 Rows (only after removing and
re-loading). If you bind to "newTable" directly, you see rows. Don't believe
it? Try it!!
 
P

Paul Clement

¤ Paul,
¤
¤ this is not the question. The problem is that if I click on the tree to
¤ select the "newTable", "newTable" has 0 Rows (only after removing and
¤ re-loading). If you bind to "newTable" directly, you see rows. Don't believe
¤ it? Try it!!
¤

I can't reproduce this problem. I am seeing the data I expect.

Did you check the underlying DataTable in the DataSet to make certain that is contains the data you
are expecting?

Which version of Visual Studio are you using?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

I am using VS2005 and C#, but I had the same problem in VS2003. Did you use
the OracleDataAdaptor? And did you Clear and Dispose? Maybe I should test the
same with SQL or OLEDB.


Before I do this:
dataSet1.Tables[0].Clear();
dataSet1.Tables[0].Dispose();
dataSet1.Tables.RemoveAt(0);
dataGrid1.DataSource = dataSet1;
oracleDataAdapter1.Fill(dataSet1, "newTable");

I see data when DataSource is set to dataSet1 or "newTable". Of course I
debugged the sample but even after executing the 5 lines above debug is:
dataSet1.Tables+Non-Public members+List+[0]+Rows+Count=95
and
dataSet1.Tables+Non-Public members+List+[0]+Rows+TableName="newTable"

but no Rows are shown when datasource is set to dataset1.

Very strange.
 
P

Paul Clement

¤ I am using VS2005 and C#, but I had the same problem in VS2003. Did you use
¤ the OracleDataAdaptor? And did you Clear and Dispose? Maybe I should test the
¤ same with SQL or OLEDB.
¤
¤
¤ Before I do this:
¤ dataSet1.Tables[0].Clear();
¤ dataSet1.Tables[0].Dispose();
¤ dataSet1.Tables.RemoveAt(0);
¤ dataGrid1.DataSource = dataSet1;
¤ oracleDataAdapter1.Fill(dataSet1, "newTable");
¤
¤ I see data when DataSource is set to dataSet1 or "newTable". Of course I
¤ debugged the sample but even after executing the 5 lines above debug is:
¤ dataSet1.Tables+Non-Public members+List+[0]+Rows+Count=95
¤ and
¤ dataSet1.Tables+Non-Public members+List+[0]+Rows+TableName="newTable"
¤
¤ but no Rows are shown when datasource is set to dataset1.
¤
¤ Very strange.

I didn't use the Oracle provider but I will give that a try.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
P

Paul Clement

¤ I am using VS2005 and C#, but I had the same problem in VS2003. Did you use
¤ the OracleDataAdaptor? And did you Clear and Dispose? Maybe I should test the
¤ same with SQL or OLEDB.
¤

I was able to repro this issue under C#. I'm taking a closer look to see if there is a reason for
this behavior.


Paul
~~~~
Microsoft MVP (Visual Basic)
 

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

Top