Solved

How to check if a column exists in a datarow?

Posted on 2004-08-11
10
9,475 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now