DataTable dt = new DataTable();
dt = GetGeneric.GetInstance<AdminDriverHelper>().GetDriversHolidays("");
dt.Columns.Add("Id", Type.GetType("System.Int64"));
dt.Columns.Add("EventStartDate", Type.GetType("System.DateTime"));
dt.Columns.Add("EventEndDate", Type.GetType("System.DateTime"));
dt.Columns.Add("EventHeader", Type.GetType("System.String"));
// int idCount = 1;
DataRow dr;
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
dr = dt.NewRow();
dr["Id"] = Convert.ToInt64(dt.Rows[i]["DriverID"]==DBNull.Value?"0":dt.Rows[i]["DriverID"]);
dr["EventStartDate"] = Convert.ToDateTime(dt.Rows[i]["DriverDate"]==DBNull.Value?DateTime.Now:dt.Rows[i]["DriverDate"]);
dr["EventEndDate"] = Convert.ToDateTime(dt.Rows[i]["DriverDate"]==DBNull.Value?DateTime.Now:dt.Rows[i]["DriverDate"]);
dr["EventHeader"] = Convert.ToString(dt.Rows[i]["DriverName"]==DBNull.Value?"":dt.Rows[i]["DriverName"]);
dt.Rows.Add(dr);
}
return dt;
I still get this
Object cannot be cast from DBNull to other types.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
dag78
Member
44 Points
25 Posts
Re: Dataset problem, please help
Aug 16, 2011 08:34 AM|LINK
using this
DataTable dt = new DataTable(); dt = GetGeneric.GetInstance<AdminDriverHelper>().GetDriversHolidays(""); dt.Columns.Add("Id", Type.GetType("System.Int64")); dt.Columns.Add("EventStartDate", Type.GetType("System.DateTime")); dt.Columns.Add("EventEndDate", Type.GetType("System.DateTime")); dt.Columns.Add("EventHeader", Type.GetType("System.String")); // int idCount = 1; DataRow dr; for (int i = dt.Rows.Count - 1; i >= 0; i--) { DataRow row = dt.Rows[i]; dr = dt.NewRow(); dr["Id"] = Convert.ToInt64(dt.Rows[i]["DriverID"]==DBNull.Value?"0":dt.Rows[i]["DriverID"]); dr["EventStartDate"] = Convert.ToDateTime(dt.Rows[i]["DriverDate"]==DBNull.Value?DateTime.Now:dt.Rows[i]["DriverDate"]); dr["EventEndDate"] = Convert.ToDateTime(dt.Rows[i]["DriverDate"]==DBNull.Value?DateTime.Now:dt.Rows[i]["DriverDate"]); dr["EventHeader"] = Convert.ToString(dt.Rows[i]["DriverName"]==DBNull.Value?"":dt.Rows[i]["DriverName"]); dt.Rows.Add(dr); } return dt;Object cannot be cast from DBNull to other types.
Description:
An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and
where it originated in the code.
Exception Details:
System.InvalidCastException: Object cannot be cast from DBNull to other
types.
Source Error:
Stack Trace: