views:

52

answers:

3

While executing the following statement to bind my database to my datagridview, I get this error:

Cannot find table 0.

How can I determine if Tables[0] exists prior to executing this statement? :

myDataView = myDataSet.Tables[0].DefaultView;

I am using SQL Server 2005 Express.

+4  A: 

Try the following

if (myDataSet.Tables.Count > 0 ) { 
  ...
}
JaredPar
@ck, nope. Baring reflection, the Tables property can never be null for a `DataSet`. It's a read only property over tableCollection which is a readonly field set in every .ctor of `DataSet`
JaredPar
@JaredPar its best to @ @ the comment rather then @ your own comment. Otherwise no one knows who you are talking to.
JonH
@JohN, I replied to a comment by @ck left on this post. He has since deleted that comment which makes mine look funny
JaredPar
Sorry, mine was wrong as you commented on my answer, hence I removed it. I think we got there in the end though...
ck
A: 

Try this:

if (myDataSet.Tables != null && myDataSet.Tables.Count > 0)
{
   // do stuff
}
ck
Baring reflection, the Tables property can never be null for a `DataSet`. It's a read only property over tableCollection which is a readonly field set in every .ctor of `DataSet`
JaredPar
@JaredPar - thanks for the heads up
ck
+1  A: 

Does the DataSet have more than one table? If not, then you should be able to just check if the DataSet itself is null. This is generally the syntax that I'll do...

DataSet ds = BLL.GetMyDataSet();

if (ds != null && ds.Tables[0].Rows.Count > 0) {
  // TODO
}
Jagd