Cast from type 'DBNull' to type 'String' is not valid.

Discussion in 'ASP .Net' started by tshad, Dec 15, 2004.

  1. tshad

    tshad Guest

    The error I am getting is:

    *******************************************************************
    Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
    type 'String' is not valid.

    Source Error:

    Line 144: firstName.text = ClientReader("firstName")
    Line 145: lastName.text = ClientReader("lastName")
    Line 146: middleName.text = ClientReader("middleName") <------
    Line 147: fullName.text = ClientReader("fullName")
    Line 148: address1.text = ClientReader("address1")
    *******************************************************************

    The code I am getting the error in is:

    if ClientReader.Read then
    applicantID.text = ClientReader("applicantID")
    firstName.text = ClientReader("firstName")
    lastName.text = ClientReader("lastName")
    middleName.text = ClientReader("middleName")
    fullName.text = ClientReader("fullName")
    allowedWorkUS.text = ClientReader("allowedWorkUS")
    end if

    How would I best do this? Do I have to check for null for each of my
    variables everytime I read a record?

    Thanks,

    Tom.
     
    tshad, Dec 15, 2004
    #1
    1. Advertising

  2. ClientReader.GetValue(0).ToString()
    is not so nice cause you set the num of th column and not the name, but
    works.


    --
    Daniel Fisher(lennybacon)
    MCP ASP.NET C#
    Blog: http://www.lennybacon.com/


    "tshad" <> wrote in message
    news:%...
    > The error I am getting is:
    >
    > *******************************************************************
    > Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
    > type 'String' is not valid.
    >
    > Source Error:
    >
    > Line 144: firstName.text = ClientReader("firstName")
    > Line 145: lastName.text = ClientReader("lastName")
    > Line 146: middleName.text = ClientReader("middleName") <------
    > Line 147: fullName.text = ClientReader("fullName")
    > Line 148: address1.text = ClientReader("address1")
    > *******************************************************************
    >
    > The code I am getting the error in is:
    >
    > if ClientReader.Read then
    > applicantID.text = ClientReader("applicantID")
    > firstName.text = ClientReader("firstName")
    > lastName.text = ClientReader("lastName")
    > middleName.text = ClientReader("middleName")
    > fullName.text = ClientReader("fullName")
    > allowedWorkUS.text = ClientReader("allowedWorkUS")
    > end if
    >
    > How would I best do this? Do I have to check for null for each of my
    > variables everytime I read a record?
    >
    > Thanks,
    >
    > Tom.
    >
     
    Daniel Fisher\(lennybacon\), Dec 15, 2004
    #2
    1. Advertising

  3. tshad

    Mythran Guest

    "tshad" <> wrote in message
    news:%...
    > The error I am getting is:
    >
    > *******************************************************************
    > Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
    > type 'String' is not valid.
    >
    > Source Error:
    >
    > Line 144: firstName.text = ClientReader("firstName")
    > Line 145: lastName.text = ClientReader("lastName")
    > Line 146: middleName.text = ClientReader("middleName") <------
    > Line 147: fullName.text = ClientReader("fullName")
    > Line 148: address1.text = ClientReader("address1")
    > *******************************************************************
    >
    > The code I am getting the error in is:
    >
    > if ClientReader.Read then
    > applicantID.text = ClientReader("applicantID")
    > firstName.text = ClientReader("firstName")
    > lastName.text = ClientReader("lastName")
    > middleName.text = ClientReader("middleName")
    > fullName.text = ClientReader("fullName")
    > allowedWorkUS.text = ClientReader("allowedWorkUS")
    > end if
    >
    > How would I best do this? Do I have to check for null for each of my
    > variables everytime I read a record?
    >
    > Thanks,
    >
    > Tom.
    >


    You can create a class that inherits whatever object type ClientReader is
    and do the dbnull checks inside there or check for nulls each time you
    access an item that can be null...

    What we did is created a method in our "common" module that looks similar to
    the following:

    Public Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    String) As String
    If Row.IsNull(FieldName)
    Return String.Empty
    Else
    Return CStr(Row(FieldName))
    End If
    End Function

    We then call this function whenever we need to get a database value (note,
    we use typed data sets but the above takes an untyped data row, we figured
    we didn't want to write a new GetFieldValue for each dataset we have so we
    created this generic one...).

    To call it, we would use middleName.Text =
    Common.GetFieldValue(ClientReader, "middleName"). That is, if ClientReader
    was a data row (looks to me like it's a DataReader).

    Ours does a lot more than the above, but for your purposes, you can build on
    that.

    Mythran
     
    Mythran, Dec 15, 2004
    #3
  4. tshad

    Karl Seguin Guest

    firstName.Text = GetStringFromReader("firstName", clientReader,
    String.Empty)

    public static function(byval columnName as string, byval dr as IDataRecord,
    byval defaultValue as string) as string
    if dr(columnName) is DBNull.Value then
    return defaultValue
    end if
    return Convert.ToString(dr(columnName))
    end function


    ..net 2.0 will support nullable types...yay!

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Daniel Fisher(lennybacon)" <info@(removethis)lennybacon.com> wrote in
    message news:...
    > ClientReader.GetValue(0).ToString()
    > is not so nice cause you set the num of th column and not the name, but
    > works.
    >
    >
    > --
    > Daniel Fisher(lennybacon)
    > MCP ASP.NET C#
    > Blog: http://www.lennybacon.com/
    >
    >
    > "tshad" <> wrote in message
    > news:%...
    > > The error I am getting is:
    > >
    > > *******************************************************************
    > > Exception Details: System.InvalidCastException: Cast from type 'DBNull'

    to
    > > type 'String' is not valid.
    > >
    > > Source Error:
    > >
    > > Line 144: firstName.text = ClientReader("firstName")
    > > Line 145: lastName.text = ClientReader("lastName")
    > > Line 146: middleName.text = ClientReader("middleName") <------
    > > Line 147: fullName.text = ClientReader("fullName")
    > > Line 148: address1.text = ClientReader("address1")
    > > *******************************************************************
    > >
    > > The code I am getting the error in is:
    > >
    > > if ClientReader.Read then
    > > applicantID.text = ClientReader("applicantID")
    > > firstName.text = ClientReader("firstName")
    > > lastName.text = ClientReader("lastName")
    > > middleName.text = ClientReader("middleName")
    > > fullName.text = ClientReader("fullName")
    > > allowedWorkUS.text = ClientReader("allowedWorkUS")
    > > end if
    > >
    > > How would I best do this? Do I have to check for null for each of my
    > > variables everytime I read a record?
    > >
    > > Thanks,
    > >
    > > Tom.
    > >

    >
    >
     
    Karl Seguin, Dec 15, 2004
    #4
  5. I would say yes, check if they are not DBNull before asigning them, but only
    in those records you know that could have a null (Allow nulls).

    -IFS

    "tshad" <> wrote in message
    news:%...
    > The error I am getting is:
    >
    > *******************************************************************
    > Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
    > type 'String' is not valid.
    >
    > Source Error:
    >
    > Line 144: firstName.text = ClientReader("firstName")
    > Line 145: lastName.text = ClientReader("lastName")
    > Line 146: middleName.text = ClientReader("middleName") <------
    > Line 147: fullName.text = ClientReader("fullName")
    > Line 148: address1.text = ClientReader("address1")
    > *******************************************************************
    >
    > The code I am getting the error in is:
    >
    > if ClientReader.Read then
    > applicantID.text = ClientReader("applicantID")
    > firstName.text = ClientReader("firstName")
    > lastName.text = ClientReader("lastName")
    > middleName.text = ClientReader("middleName")
    > fullName.text = ClientReader("fullName")
    > allowedWorkUS.text = ClientReader("allowedWorkUS")
    > end if
    >
    > How would I best do this? Do I have to check for null for each of my
    > variables everytime I read a record?
    >
    > Thanks,
    >
    > Tom.
    >
     
    Isaias Formacio-Serna, Dec 15, 2004
    #5
  6. tshad

    tshad Guest

    "Mythran" <> wrote in message
    news:...
    > "tshad" <> wrote in message
    > news:%...
    >> The error I am getting is:
    >>
    >> *******************************************************************
    >> Exception Details: System.InvalidCastException: Cast from type 'DBNull'
    >> to type 'String' is not valid.
    >>
    >> Source Error:
    >>
    >> Line 144: firstName.text = ClientReader("firstName")
    >> Line 145: lastName.text = ClientReader("lastName")
    >> Line 146: middleName.text = ClientReader("middleName") <------
    >> Line 147: fullName.text = ClientReader("fullName")
    >> Line 148: address1.text = ClientReader("address1")
    >> *******************************************************************
    >>
    >> The code I am getting the error in is:
    >>
    >> if ClientReader.Read then
    >> applicantID.text = ClientReader("applicantID")
    >> firstName.text = ClientReader("firstName")
    >> lastName.text = ClientReader("lastName")
    >> middleName.text = ClientReader("middleName")
    >> fullName.text = ClientReader("fullName")
    >> allowedWorkUS.text = ClientReader("allowedWorkUS")
    >> end if
    >>
    >> How would I best do this? Do I have to check for null for each of my
    >> variables everytime I read a record?
    >>
    >> Thanks,
    >>
    >> Tom.
    >>

    >
    > You can create a class that inherits whatever object type ClientReader is
    > and do the dbnull checks inside there or check for nulls each time you
    > access an item that can be null...
    >
    > What we did is created a method in our "common" module that looks similar
    > to the following:
    >
    > Public Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    > String) As String
    > If Row.IsNull(FieldName)
    > Return String.Empty
    > Else
    > Return CStr(Row(FieldName))
    > End If
    > End Function
    >
    > We then call this function whenever we need to get a database value (note,
    > we use typed data sets but the above takes an untyped data row, we figured
    > we didn't want to write a new GetFieldValue for each dataset we have so we
    > created this generic one...).
    >
    > To call it, we would use middleName.Text =
    > Common.GetFieldValue(ClientReader, "middleName"). That is, if
    > ClientReader was a data row (looks to me like it's a DataReader).


    Yes, it is a DataReader.

    But I tried to use your function, with and without the public and I get an
    error:

    ***********************************************************
    Compiler Error Message: BC30182: Type expected.

    Source Error:

    Line 36: End Sub
    Line 37:
    Line 38: Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    Line 39: String) As String
    Line 40: If Row.IsNull(FieldName)
    ***********************************************************

    Am I missing something? At the moment the function is in my page as just a
    function. Is "DataRow" causing a problem?

    Thanks,

    Tom
    Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    String) As String
    If Row.IsNull(FieldName)
    Return String.Empty
    Else
    Return CStr(Row(FieldName))
    End If
    End Function

    >
    > Ours does a lot more than the above, but for your purposes, you can build
    > on that.
    >
    > Mythran
    >
    >
    >
     
    tshad, Dec 15, 2004
    #6
  7. tshad

    tshad Guest

    "tshad" <> wrote in message
    news:...
    > "Mythran" <> wrote in message
    > news:...
    >> "tshad" <> wrote in message
    >> news:%...
    >>> The error I am getting is:
    >>>
    >>> *******************************************************************
    >>> Exception Details: System.InvalidCastException: Cast from type 'DBNull'
    >>> to type 'String' is not valid.
    >>>
    >>> Source Error:
    >>>
    >>> Line 144: firstName.text = ClientReader("firstName")
    >>> Line 145: lastName.text = ClientReader("lastName")
    >>> Line 146: middleName.text = ClientReader("middleName") <------
    >>> Line 147: fullName.text = ClientReader("fullName")
    >>> Line 148: address1.text = ClientReader("address1")
    >>> *******************************************************************
    >>>
    >>> The code I am getting the error in is:
    >>>
    >>> if ClientReader.Read then
    >>> applicantID.text = ClientReader("applicantID")
    >>> firstName.text = ClientReader("firstName")
    >>> lastName.text = ClientReader("lastName")
    >>> middleName.text = ClientReader("middleName")
    >>> fullName.text = ClientReader("fullName")
    >>> allowedWorkUS.text = ClientReader("allowedWorkUS")
    >>> end if
    >>>
    >>> How would I best do this? Do I have to check for null for each of my
    >>> variables everytime I read a record?
    >>>
    >>> Thanks,
    >>>
    >>> Tom.
    >>>

    >>
    >> You can create a class that inherits whatever object type ClientReader is
    >> and do the dbnull checks inside there or check for nulls each time you
    >> access an item that can be null...
    >>
    >> What we did is created a method in our "common" module that looks similar
    >> to the following:
    >>
    >> Public Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    >> String) As String
    >> If Row.IsNull(FieldName)
    >> Return String.Empty
    >> Else
    >> Return CStr(Row(FieldName))
    >> End If
    >> End Function
    >>
    >> We then call this function whenever we need to get a database value
    >> (note, we use typed data sets but the above takes an untyped data row, we
    >> figured we didn't want to write a new GetFieldValue for each dataset we
    >> have so we created this generic one...).
    >>
    >> To call it, we would use middleName.Text =
    >> Common.GetFieldValue(ClientReader, "middleName"). That is, if
    >> ClientReader was a data row (looks to me like it's a DataReader).

    >
    > Yes, it is a DataReader.
    >
    > But I tried to use your function, with and without the public and I get an
    > error:


    Nevermind this. I found the problem - when I copied the file, I didn't
    notice that part of the function statement went to the next line.

    I am, however, getting an error:

    **************************************************************
    Compiler Error Message: BC30311: Value of type
    'System.Data.SqlClient.SqlDataReader' cannot be converted to
    'System.Data.DataRow'.

    Source Error:

    Line 152: firstName.text = ClientReader("firstName")
    Line 153: lastName.text = ClientReader("lastName")
    Line 154: middleName.text = GetFieldValue(ClientReader,"middleName")
    Line 155: fullName.text = ClientReader("fullName")
    Line 156: address1.text = ClientReader("address1")
    ***************************************************************

    Tom

    >
    > ***********************************************************
    > Compiler Error Message: BC30182: Type expected.
    >
    > Source Error:
    >
    > Line 36: End Sub
    > Line 37:
    > Line 38: Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    > Line 39: String) As String
    > Line 40: If Row.IsNull(FieldName)
    > ***********************************************************
    >
    > Am I missing something? At the moment the function is in my page as just
    > a function. Is "DataRow" causing a problem?
    >
    > Thanks,
    >
    > Tom
    > Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
    > String) As String
    > If Row.IsNull(FieldName)
    > Return String.Empty
    > Else
    > Return CStr(Row(FieldName))
    > End If
    > End Function
    >
    >>
    >> Ours does a lot more than the above, but for your purposes, you can build
    >> on that.
    >>
    >> Mythran
    >>
    >>
    >>

    >
    >
     
    tshad, Dec 15, 2004
    #7
    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. Elmo Watson
    Replies:
    3
    Views:
    6,059
    Scott M.
    Dec 25, 2003
  2. Mike Newton
    Replies:
    0
    Views:
    2,689
    Mike Newton
    Jul 27, 2004
  3. Chris
    Replies:
    2
    Views:
    21,358
    Chris
    May 11, 2006
  4. Cirene
    Replies:
    2
    Views:
    915
    nick chan
    Jun 19, 2008
  5. Rob
    Replies:
    0
    Views:
    365
Loading...

Share This Page