Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: typecast error!

Reply
Thread Tools

Re: typecast error!

 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      02-01-2006
Which line is generating the error? First, don't convert a null value to a
string. You can't do it, that's why you have DBNull.Value to begin with.
Your conditional statement should evaluate


dr["session_startdate"] != DBNull.Value

If you cast try to send a null value to a string you'll get an error as a
null value is a special value that cannot be converted into anything.

If dr is a datareader, use the datareader functions to do the convertion so
you don't have to cast it yourself. Datareaders should have a datetime
member for just this reason. Use the GetDateTime() method to perform this
function. Most of these functions require the actual ordinal position of the
column and not the name so you'll need to get the name first. This is a good
thing though as indexing a datareader column by name is a performance hit
since it has to evaluate the fields each time in order to find the column.

Use dr.GetOrdinal("session_startdate") to get the integer position of the
column. Store that as a local integer and you can then reference it each
time you need to evaluate the column. If you get this column number early
enough, your conditional statement then becomes
if(!dr.IsDBNull(intPosition)) where intposition is the integer that contains
the ordinal position of your column.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Arvind R" <arvind http://www.velocityreviews.com/forums/(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Whats the solution for this?
>
> Specified cast is not valid.
> 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: Specified cast is not
> valid.
>
> Source Error:
>
> Line 118: if ((dr["session_startdate"].ToString() !=
> DBNull.Value.ToString()))
> Line 119: {
> Line 120: DateTime dtEvent = (DateTime)dr["session_startdate"];
> Line 121:
> Line 122ublic void CalendarDRender(object sender,
> System.Web.UI.WebControls.DayRenderEventArgs e)
>
> {
>
>
> // If the month is CurrentMonth
>
> if (!e.Day.IsOtherMonth )
>
> {
>
> foreach (DataRow dr in ds.Tables[0].Rows)
>
> {
>
> if ((dr["session_startdate"].ToString() != DBNull.Value.ToString()))
>
> {
>
> DateTime dtEvent = (DateTime)dr["session_startdate"];
>
>
> // = (DateTime)dr["session_startdate"];
>
> if (dtEvent.Equals(e.Day.Date))
>
> {
>
> e.Cell.BackColor = Color.PaleVioletRed;
>
> }
>
> }
>
> }
>
> }
>
> //If the month is not CurrentMonth then hide the Dates
>
> else
>
> {
>
> e.Cell.Text = "";
>
> }
>
> }
>
>



 
Reply With Quote
 
 
 
 
Lau Lei Cheong
Guest
Posts: n/a
 
      02-08-2006
And I'd prefer to use Convert.IsDBNull() or DataRow.IsNull() instead.

I suspect it's more or less the same anyway.

"Mark Fitzpatrick" <(E-Mail Removed)> glsD:(E-Mail Removed) l...
> Which line is generating the error? First, don't convert a null value to a
> string. You can't do it, that's why you have DBNull.Value to begin with.
> Your conditional statement should evaluate
>
>
> dr["session_startdate"] != DBNull.Value
>
> If you cast try to send a null value to a string you'll get an error as a
> null value is a special value that cannot be converted into anything.
>
> If dr is a datareader, use the datareader functions to do the convertion
> so you don't have to cast it yourself. Datareaders should have a datetime
> member for just this reason. Use the GetDateTime() method to perform this
> function. Most of these functions require the actual ordinal position of
> the column and not the name so you'll need to get the name first. This is
> a good thing though as indexing a datareader column by name is a
> performance hit since it has to evaluate the fields each time in order to
> find the column.
>
> Use dr.GetOrdinal("session_startdate") to get the integer position of the
> column. Store that as a local integer and you can then reference it each
> time you need to evaluate the column. If you get this column number early
> enough, your conditional statement then becomes
> if(!dr.IsDBNull(intPosition)) where intposition is the integer that
> contains the ordinal position of your column.
>
> Hope this helps,
> Mark Fitzpatrick
> Microsoft MVP - FrontPage
>
> "Arvind R" <arvind (E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Whats the solution for this?
>>
>> Specified cast is not valid.
>> 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: Specified cast is not
>> valid.
>>
>> Source Error:
>>
>> Line 118: if ((dr["session_startdate"].ToString() !=
>> DBNull.Value.ToString()))
>> Line 119: {
>> Line 120: DateTime dtEvent = (DateTime)dr["session_startdate"];
>> Line 121:
>> Line 122ublic void CalendarDRender(object sender,
>> System.Web.UI.WebControls.DayRenderEventArgs e)
>>
>> {
>>
>>
>> // If the month is CurrentMonth
>>
>> if (!e.Day.IsOtherMonth )
>>
>> {
>>
>> foreach (DataRow dr in ds.Tables[0].Rows)
>>
>> {
>>
>> if ((dr["session_startdate"].ToString() != DBNull.Value.ToString()))
>>
>> {
>>
>> DateTime dtEvent = (DateTime)dr["session_startdate"];
>>
>>
>> // = (DateTime)dr["session_startdate"];
>>
>> if (dtEvent.Equals(e.Day.Date))
>>
>> {
>>
>> e.Cell.BackColor = Color.PaleVioletRed;
>>
>> }
>>
>> }
>>
>> }
>>
>> }
>>
>> //If the month is not CurrentMonth then hide the Dates
>>
>> else
>>
>> {
>>
>> e.Cell.Text = "";
>>
>> }
>>
>> }
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: typecast error! Otis Mukinfus ASP .Net 1 02-04-2006 05:32 PM
Re: typecast error! Joe Van Meer ASP .Net 1 02-02-2006 02:00 AM
generics typecast Tomba Java 8 01-13-2006 02:02 AM
Overloading the typecast operator Nimmi Srivastav C++ 2 02-01-2004 01:24 AM
typecast int to string Venkat C++ 9 01-08-2004 01:51 PM



Advertisments