Setting value for datarow if its null [modified]
-
i have datatable in which 2 row,1st column value is null that column is datatype is DateTime. wwhen i tried to add data to database..its gives me error as Object cannot be cast from DBNull to other types. i have done following.. foreach (DataRow dtRow in dtTrackingData.Rows) { SqlHelper.ExecuteNonQuery ( myConnectionString , "SP_NAME" , iWeekNo , Convert.ToInt32(dtRow["InvCoID"]) , Convert.ToDateTime(dtRow["LatestReportDateConsensus"]) , Convert.ToDateTime(dtRow["LatestReportDateVirtua"]) ,dtFromDate ,dtToDate ,CreatedBy ); } i tried to add if (dtRow["LatestReportDateConsensus"] == System.DBNull.Value) dtRow["LatestReportDateConsensus"] =null; still its not work..:( how i will add value to datarow if its null
modified on Wednesday, June 3, 2009 1:22 AM
-
i have datatable in which 2 row,1st column value is null that column is datatype is DateTime. wwhen i tried to add data to database..its gives me error as Object cannot be cast from DBNull to other types. i have done following.. foreach (DataRow dtRow in dtTrackingData.Rows) { SqlHelper.ExecuteNonQuery ( myConnectionString , "SP_NAME" , iWeekNo , Convert.ToInt32(dtRow["InvCoID"]) , Convert.ToDateTime(dtRow["LatestReportDateConsensus"]) , Convert.ToDateTime(dtRow["LatestReportDateVirtua"]) ,dtFromDate ,dtToDate ,CreatedBy ); } i tried to add if (dtRow["LatestReportDateConsensus"] == System.DBNull.Value) dtRow["LatestReportDateConsensus"] =null; still its not work..:( how i will add value to datarow if its null
modified on Wednesday, June 3, 2009 1:22 AM
try using the following code : foreach (DataRow dtRow in dtTrackingData.Rows) { SqlHelper.ExecuteNonQuery ( myConnectionString , "SP_NAME" , iWeekNo , Convert.ToInt32(dtRow["InvCoID"]) , IIF(IsDBNull(dtRow["LatestReportDateConsensus"]), "null" , dtRow["LatestReportDateConsensus"] ) , Convert.ToDateTime(dtRow["LatestReportDateVirtua"]) ,dtFromDate ,dtToDate ,CreatedBy ); } i have used IIF function; it checks if datevalue is true for dbnull then adds null in database else the datevalue of datatable column
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
-
try using the following code : foreach (DataRow dtRow in dtTrackingData.Rows) { SqlHelper.ExecuteNonQuery ( myConnectionString , "SP_NAME" , iWeekNo , Convert.ToInt32(dtRow["InvCoID"]) , IIF(IsDBNull(dtRow["LatestReportDateConsensus"]), "null" , dtRow["LatestReportDateConsensus"] ) , Convert.ToDateTime(dtRow["LatestReportDateVirtua"]) ,dtFromDate ,dtToDate ,CreatedBy ); } i have used IIF function; it checks if datevalue is true for dbnull then adds null in database else the datevalue of datatable column
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
-
IIF is in vb.net for C# its ((what) ? then : else) Apologies for the goof up
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
-
IIF is in vb.net for C# its ((what) ? then : else) Apologies for the goof up
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
-
google for ((what) ? then : else) C#
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
-
google for ((what) ? then : else) C#
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
-
google for ((what) ? then : else) C#
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com
got the solution...............:) SqlHelper.ExecuteNonQuery ( myConnectionString , "usp_InsertTrackingData" , iWeekNo , Convert.ToInt32(dtRow["InvCoID"]) , ((dtRow["LatestReportDateConsensus"] != DBNull.Value) ? dtRow["LatestReportDateConsensus"] : null) , ((dtRow["LatestReportDateVirtua"] != DBNull.Value) ? dtRow["LatestReportDateVirtua"] : null) ,dtFromDate ,dtToDate ,CreatedBy );