access datagrid properties

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

I have a data access component with a function that returns a dataset.
I want to access the DataTable[0].Rows.Count property of this dataset,
but I get a "Object reference not set to an instance of an object."
error when I try to access this property from a Page_Load.

The data component function that returns the DataSet:

public DataSet GetProducts(int CategoryID)
{
//clipped for clarity ...

DataSet ds = new DataSet();
myDataAdapter.Fill(ds);

myDataAdapter.Dispose();
myConnection.Close();

return ds;

}

Now on a codebehind file I have this in the Page_Load:

private void Page_Load(object sender, System.EventArgs e)
{

//clipped for clarity

test.Components.ProductsDB products = new
test.Components.ProductsDB();

DataSet myDS = new DataSet();
myDS = products.GetProducts(ProductTypeID);
ProductTypeGrid.DataSource = myDS;


// here is problem -- i am trying to set the number of total rows to a
// label

lblTotalProducts.Text = myDS.Tables[0].Rows.Count.ToString();
ProductTypeGrid.DataBind();

}

??

Thanks,

Jack
 
Hi,

Ensure the dataset has at least one table before accessing the items of the
Tables collection.
 
Hi,

It does. I can use the contents of the dataset in other ways, (for
instance, by databinding it to a grid -- the dataset is NOT empty).

Any other ideas?

thanks
 
I was getting the same error in C#. I fixed it by using the Invoke
command. I am updating a DataTable which is a datasource of a
DataGrid. I was just directly calling the procedure that updated the
DataTable. This was causing an exception outside my code, like you
are getting. It has something to do with accessing a form component
from your thread that doesn't "own" the component.

Here is what fixed it for me:

Define the procedure that does the updating like this:
public void UpdGrid_handler(object sender, EventArgs evArgs)

Whenever you need to update the grid, use a line like this:
grid.Invoke(new EventHandler(UpdGrid_handler));

Hope this helps,
Mick
 

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

Back
Top