473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Checking for columns in a DataRow

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,
--
Randy
Aug 14 '06 #1
3 7257

"randy1200" <ra*******@newsgroups.nospamwrote in message
news:45**********************************@microsof t.com...
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();

Aug 14 '06 #2
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...
}

randy1200 wrote:
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,
--
Randy
Aug 14 '06 #3
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

randy1200 wrote:
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,
--
Randy
Aug 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: scorpion53061 | last post by:
Very urgent and I am very close but need a little help to get me over the edge........ I need to write these columns to a html file with each row containing these columns (seperated by breaks)....
3
by: rcolby | last post by:
Hi all, I'm experiecing a problem with dates being returned from SQL 2000 Database. I retrieve the last updated field (Date) from a sql 2000 and then compare the returned date with another...
4
by: Sam | last post by:
Hi, I'm adding columns to a datatable as followed. The values are from textboxes or comboboxes. The first column is properly field but then all the subsequent columns just contain "", whereas the...
4
by: Jeff | last post by:
I am stuck on trying to generate two columns headers for a datagrid on form load. I can use a datatable as the datasource and get the results I want, but I want to set different column widths and...
2
by: Philip Wagenaar | last post by:
I have a dataset that I queried from an excel sheet. I want to loop through the rows, then loop through the columns and write them to a file. But I got stuck pretty quickly: So far: For Each...
2
by: RSH | last post by:
Hi, Iam struggling with an application where I am trying to transfer a datarow from one sql server to another instance of sql server. The schmeas may be slightly different and I am getting an...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
2
by: Jacksm | last post by:
How can I align an asp:table columns with gridview columns (the widths)? I have tried setting table.column(0).width = gridview.column(0).width at page_load but it doesn't work. Thanks in advance
17
by: vish | last post by:
Hi all, I know this might look strange at first look, but i am looping thru a dataset created using to different tables from database.These two tables have some fields with the same names. ...
2
by: Scott | last post by:
I am trying to sort a datagrid which contains 5 checkbox columns. All other columns sort properly except these 5 checkbox columns which dont sort at all. I am using ASP.NET 1.1 and this code is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.