?
Solved

How to check if a column exists in a datarow?

Posted on 2004-08-11
10
Medium Priority
?
9,759 Views
Last Modified: 2010-09-02
Hi,

I'm performing some tasks on a DataRow - retrieving values etc, but I want to be able to check that a row exists, for example

name = row["name"].ToString()

but how can I check that row["name"] actually exists?

There's no 'Contains' method I can use?

Thanks

Wint.
0
Comment
Question by:WinterMuteUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Expert Comment

by:rohanpandya
ID: 11771669
Hi

 Use following

    objDataset.Tables[0].Columns["name"] != null
0
 
LVL 4

Expert Comment

by:rohanpandya
ID: 11771675
put about code in if condition or any where it's work fine

 Best Luck
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11771686
if (!row.IsNull ("name"))
   name = row["name"].ToString();
else
   name = "Name does not exist";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11771707
Doesn't IsNull say whether the value of the column is actually null, not whether the column exists....?

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11771733
Sorry, I didn't get your question correctly. You wrote: "but how can I check that row["name"] actually exists" - I though you are trying to get if the value in the column exists.

So, to check the column:

if (table.Columns["name"] != null)
    // Do whatever
0
 
LVL 4

Expert Comment

by:rohanpandya
ID: 11771785
it check for the values of that column is null means it exists or not? It's work try it
0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11771812
Sorry - my fault for not explaining/wording properly....
I don't have the table at this point:

public static string GetNameFromDataRow(DataRow row)
{
     return row["name"].ToString();
}

All I have is the DataRow, and I would like to do something like:

public static string GetNameFromDataRow(DataRow row)
{
     if(row.Contains("name"))
          return row["name"].ToString();
     else return null;
}

0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11771823
If the value of the column is null, thats still a valid result in my system, so checking for null doesn't help in that case!
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 2000 total points
ID: 11771825
public static string GetNameFromDataRow(DataRow row)
{
     if(row.Table.Columns["name"] != null)
          return row["name"].ToString();
     else return null;
}
0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11771954
Equally I suppose I could use:

if(row.Table.Columns.Contains("name"))...

Thanks a lot mate, I just didn't see that!

Wint

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question