Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
hi,

i am getting error Cannot set Column 'EffectiveDate' to be null. Please use DBNull instead.
my code is

C#
dt.Columns.Add("EffectiveDate", typeof(DateTime));

   DateTime?   effectivedate=null;


  if (lastRow["Effective_x0020_Date"] != DBNull.Value)
  {

     effectivedate = Convert.ToDateTime(lastRow["Effective_x0020_Date"]);
  }


DataRow dr = dt.NewRow();
     dr["EffectiveDate"] = effectivedate;
dt.Rows.Add(dr);


i followed many things

http://codeverge.com/asp.net.presentation-controls/how-to-return-dbnull.value-inst/487162[^]

but same error i am getting
Posted
Updated 18-Jan-15 19:25pm
v3

1 solution

COuld it simply be that lastRow["Effective_x0020_Date"] is DBNull? Then you would have the initial value (null) in effectivedate;

When assigning the data row value, try using null coalescing operator
C#
dr["EffectiveDate"] = effectivedate ?? DBNull.Value;
 
Share this answer
 
Comments
Merlin Tintin 2-Jul-19 7:25am    
I think this is better (other you will get an error) :

dr["EffectiveDate"] = effectivedate ?? (object)DBNull.Value;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900