Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if DataRow exists by column name in c#? [duplicate]

Tags:

c#

null

datarow

I want to do something like this:

   private User PopulateUsersList(DataRow row)         {             Users user = new Users();             user.Id = int.Parse(row["US_ID"].ToString());             if (row["US_OTHERFRIEND"] != null)             {                 user.OtherFriend = row["US_OTHERFRIEND"].ToString();             }             return user;         } 

However, I get an error saying US_OTHERFRIEND does not belong to the table. I want to simply check if it is not null, then set the value.

Isn't there a way to do this?

like image 378
waqasahmed Avatar asked Apr 01 '10 20:04

waqasahmed


People also ask

How do I check if a DataRow column exists?

You can use the DataColumnCollection of Your datatable to check if the column is in the collection.

What is a DataRow?

A DataRow represent a row of data in data table. You add data to the data table using DataRow object. A DataRowCollection object represents a collection of data rows of a data table.


1 Answers

You should try

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

I don't believe that row has a columns property itself.

like image 167
Kibbee Avatar answered Sep 21 '22 05:09

Kibbee