Solved

How to check if a column exists in a datarow?

Posted on 2004-08-11
10
9,528 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 57
C# guarantee sql connection close 6 39
C# bracket error 3 30
VB.NET 2008 Winforms Signing 13 16
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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