Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Checking for columns in a DataRow

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
randy1200 - 14 Aug 2006 14:14 GMT
I have a DataSet/DataTable read in from an XML file. I can loop through the
DataRows in the DataTable, and do actions like this:

string theName = dr["Name"].ToString();

My fear is that somebody is going to come along and slightly change the xml
file, perhaps changing "Name" to "Names". The rather brittle line above will
throw an exception.

I'd really like to do something like the following:

if(dr["Name"].Exists)
  string theName = dr["Name"].ToString();

As far as I can tell, the .Exists doesn't exist in this context in C#.

Any suggestions on how to approach this, other than just catching the
exception?

Thanks,
Signature

Randy

dllhell - 14 Aug 2006 14:21 GMT
> I'd really like to do something like the following:
>
> if(dr["Name"].Exists)
>   string theName = dr["Name"].ToString();

try this:
string theName = dr[index_of_column].ToString();
Bala - 14 Aug 2006 14:24 GMT
Hi,

Can't you just do:

Object oDataRowValue = dr["Name"];
if (oDataRowValue != null)
{
 string theName = dr["Name"].ToString();
}
else
{
 // Whatever you planned to do on error...
}

> I have a DataSet/DataTable read in from an XML file. I can loop through the
> DataRows in the DataTable, and do actions like this:
[quoted text clipped - 16 lines]
>
> Thanks,
Rader - 14 Aug 2006 14:39 GMT
use method "Contains(string)" of DataColumnCollection to check if the
column exists:
if(dt.Columns.Contains(colName)
 string theName = dr["colName"].ToString();

another choice is selecting with col index

> I have a DataSet/DataTable read in from an XML file. I can loop through the
> DataRows in the DataTable, and do actions like this:
[quoted text clipped - 16 lines]
>
> Thanks,

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.