tags:

views:

720

answers:

3

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?

+1  A: 

You can use

try {
   user.OtherFriend = row["US_OTHERFRIEND"].ToString();
}
catch (Exception ex)
{
   // do something if you want 
}
Bryan
+1: Thanks. Simple enough :)
waqasahmed
+1  A: 
if (row.Columns.Contains("US_OTHERFRIEND"))
Big Endian
@Big Endian, DataRow doesn't have Columns property.
šljaker
this doesn't work... DataRow row does not have Columns property.
waqasahmed
+2  A: 

You should try

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

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

Kibbee
+1: Thanks. This is perfect!
waqasahmed