Re: typecast error!

Discussion in 'ASP .Net' started by Mark Fitzpatrick, Feb 1, 2006.

  1. 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 > wrote in message
    news:%...
    > 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 122:public 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 = "";
    >
    > }
    >
    > }
    >
    >
    Mark Fitzpatrick, Feb 1, 2006
    #1
    1. Advertising

  2. And I'd prefer to use Convert.IsDBNull() or DataRow.IsNull() instead.

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

    "Mark Fitzpatrick" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > 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 > wrote in message
    > news:%...
    >> 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 122:public 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 = "";
    >>
    >> }
    >>
    >> }
    >>
    >>

    >
    >
    Lau Lei Cheong, Feb 8, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Van Meer

    Re: typecast error!

    Joe Van Meer, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    Bob Lehmann
    Feb 2, 2006
  2. Otis Mukinfus

    Re: typecast error!

    Otis Mukinfus, Feb 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    365
    Terry Burns
    Feb 4, 2006
  3. Tomba

    generics typecast

    Tomba, Jan 9, 2006, in forum: Java
    Replies:
    8
    Views:
    1,327
  4. Martin Himmel
    Replies:
    0
    Views:
    312
    Martin Himmel
    Jul 12, 2006
  5. Martin Himmel
    Replies:
    1
    Views:
    396
    Victor Bazarov
    Jul 12, 2006
Loading...

Share This Page