HOWEVER, if there IS a null value I get the following error
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
pointing to line 11
Line 9: if (DataBinder.Eval(e.Row.DataItem, "Sunday")!= null) Line 10: { Line 11: su += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Sunday")); Line 12: }
BUT didnnt i just check for the not null condition???
Yes, you must check if it is equal to DBNull first because DBNull does not equal "null" and C#, which means default values are not used when conversion fails.
I recommended binding it not directly to the data source, so its more readable to other developers. And its better practice to have a class handle all the datatype checking,setting the appropriate values and implementing caching techiniques behind the scenes.
Your class should basically make your datasource a simple "plug and play" interface.
However, it was just a suggestion. If you prefer doing:
Harperator
Member
140 Points
329 Posts
Object cannot be cast from DBNull to other types.
Sep 24, 2007 02:10 PM|LINK
Hi-
I have a little chunk of code that runs in the rowdatabound event of a gridview.
I am checking for the value of the field (decimal 8,6) and running a total in the footer.
Here is my code that runs fine of there are nno null values in the sunday column:
decimal su=0, mon, tue, wed, thu, fri, sat, sun;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (DataBinder.Eval(e.Row.DataItem, "Sunday")!= null)
{
su += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Sunday"));
}
//tcash += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "cash"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Totals: ";
e.Row.Cells[1].Text = su.ToString();
}
}
HOWEVER, if there IS a null value I get the following error
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
pointing to line 11
BUT didnnt i just check for the not null condition???
thanks
(MONDAY, UGH!)
triggered
Contributor
3356 Points
908 Posts
Re: Object cannot be cast from DBNull to other types.
Sep 24, 2007 02:18 PM|LINK
Yes, you must check if it is equal to DBNull first because DBNull does not equal "null" and C#, which means default values are not used when conversion fails.
So you should have something like this:
this._intFlag = (sdrReader["Sunday"] == DBNull.Value)? "0" : sdrReader["Sunday"];
But I used a SqlDataReader, to highlight the problem.
Harperator
Member
140 Points
329 Posts
Re: Object cannot be cast from DBNull to other types.
Sep 24, 2007 02:51 PM|LINK
Ahhhh!
That makes sense, thanks.
How come I can't just say :
if (DataBinder.Eval(e.Row.DataItem, "Sunday")!= DBNull)
???
triggered
Contributor
3356 Points
908 Posts
Re: Object cannot be cast from DBNull to other types.
Sep 24, 2007 03:07 PM|LINK
I recommended binding it not directly to the data source, so its more readable to other developers. And its better practice to have a class handle all the datatype checking,setting the appropriate values and implementing caching techiniques behind the scenes. Your class should basically make your datasource a simple "plug and play" interface.
However, it was just a suggestion. If you prefer doing:
DataBinder.Eval(e.Row.DataItem, "Sunday")!= DBNull)? "0" : DataBinder.Eval(e.Row.DataItem, "Sunday")
I say give it a shot and see if it works for you.
Hope I helped.
Harperator
Member
140 Points
329 Posts
Re: Object cannot be cast from DBNull to other types.
Sep 24, 2007 04:08 PM|LINK
Totally helped, THANKS!
FYI here is my final code I run the if statement at every day
if (DataBinder.Eval(e.Row.DataItem, "Monday") != DBNull.Value)
{
mon += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Monday"));
}
and just for kicks set the 'null' property of the day to the string '0' just so it looks better.
thanks again
dan