Shaitan00 Posted November 20, 2006 Posted November 20, 2006 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, Quote
Killian35 Posted November 20, 2006 Posted November 20, 2006 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 } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.