Solved

How to check if a column exists in a datarow?

Posted on 2004-08-11
10
9,671 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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