Results 1 to 3 of 3

Thread: DBNull data type to string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182

    DBNull data type to string

    I created a DataSet fro a query and populate a table with its values. But when a field contains NULL value I receive an error message about conversion DBNull data type to string. Can anyone help me how to solve this problem?
    The code as follows:

    ByVal strTableName As String)
    Dim r As TableRow
    Dim c As TableCell
    Dim myTable As DataTable
    Dim myRow As DataRow
    Dim myColumn As DataColumn
    Dim strTemp As String

    ' For each table in the DataSet, print the row values.
    myTable = myDataSet.Tables(strTableName)

    For Each myRow In myTable.Rows
    r = New TableRow()

    For Each myColumn In myTable.Columns
    c = New TableCell()

    strTemp = myRow(myColumn)

    c.Controls.Add(New LiteralControl(strTemp))
    r.Cells.Add(c)

    Next myColumn

    Table1.Rows.Add(r)
    Next myRow

    End Sub

  2. #2
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774

    Re: DBNull data type to string

    Originally posted by bona
    I created a DataSet fro a query and populate a table with its values. But when a field contains NULL value I receive an error message about conversion DBNull data type to string. Can anyone help me how to solve this problem?
    The code as follows:
    .
    .
    .
    strTemp = myRow(myColumn)
    .
    .
    .
    I only have a C# version, but you should be able to see the direction to go.

    Code:
    .
    .
    .
    strTemp = CheckNull(myRow(myColumn))
    .
    .
    .
    private String CheckNull(Object objIn)
    {
        string strRetVal;
        if (objIn == DBNull.Value) {
            strRetVal = "";
        }
        else {
           strRetVal = objIn.ToString();
        }
        return strRetVal;
    }

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182
    Thanks, Patoooey.
    It works excellent, the VB code:



    ...
    strTemp = CheckNull(myRow(myColumn))
    ...

    Private Function CheckNull(ByVal objIn As Object) As String
    Dim strRetVal As String

    If (objIn Is DBNull.Value) Then
    strRetVal = ""
    Else
    strRetVal = objIn.ToString()
    End If

    Return strRetVal
    End Function
    Last edited by bona; Jul 9th, 2002 at 05:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width