Jump to content
Xtreme .Net Talk

Not failing if I cannot find a column in my dataset [CS/C# 2003]


Recommended Posts

Posted

Currently I use the following code to retrieve information from an XML file ... it works perfectly fine as long as the [type] (column name) exists in the DataRowView resulting from my .ReadXML.

 

DataSet dsxmlfile = new DataSet();
string sLabel = "";

dsxmlfile.ReadXml(filename);
if (dsxmlfile.Tables["Component"] != null ) 
{
DataView dsview = dsxmlfile.Tables["Component"].DefaultView;

// Check to see if the TYPE exists before trying to access it
// ?????? //
				
foreach (DataRowView a in dsview)
{			
	sLabel = a[type].ToString();	// THIS IS WHERE THE EXCEPTION OCCURS where [type]="FirstV"
}
	
dsxmlfile.Dispose();
return sLabel;
}				
dsxmlfile.Dispose();

 

The problem is - if I am looking for a [type] that does NOT exist I get the following exception:

Exception: FirstV is neither a DataColumn nor a DataRelation for table ComponentVersion.

Exception: at System.Data.DataRowView.get_Item(String property)

 

Therefore, I need a way to CHECK first if the COLUMN [type] exists and if not RETURN NULL and not cause an exception...

Is there a way to do this? Maybe using the XML toolset instead of Dataset/DataRowViews?

 

Any help, hints, or ideas would be greatly appreciated.

Thanks,

Posted

You should check the DataTable columns to see if it exists. You can go through your dsview variable to access the table and then the column collection to check.

           if (dsview.Table.Columns.Contains(type))
           {
               // do stuff
           }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...