Abhilash JA Abhilash JA - 23 days ago 12
C# Question

c# - datatable if one field is null set another value

I have a Datatable.

using (DataSet ds = new DataSet())
{
da.Fill(ds);
dt = ds.Tables[0];
}


The
dt
, return list of data like this,

enter image description here

From this image, when UserId is DBnull then I want to set docCount equl to 0. How can I do that?

Answer

You could try something like this:

dt = ds.Tables[0];
foreach(DataRow row in dt.Rows)
{
   if(row["userId"] == DBNull.Value)
   {
       row["docCount"] = 0;
   }
}
Comments