Solved

How to check if a column exists in a datarow?

Posted on 2004-08-11
10
9,598 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
Industry Leaders: 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!

 
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

Independent Software Vendors: 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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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