Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2004, 12:52 AM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Invalid Casting for Null column using ODBC

Hi,i'm a new member here.
I'm working on the database which use ODBC as the connection object.
Somehow the datasource that i need to read have the Year column that have data type smallint (equals to int16 in ADO.NET). Unfortunately the smallint data type is not returning the right value , in my case instead of returning the value 1998,1999 or 2000 it return the value 48 for all year. If i'm to use the standard Fill method of ODBCDataAdapter object, i'm getting stuck because there are no way we can do the casting/converting when the Fill method is invoke.
For this case i have simulate the Fill method of ODBCDataAdapter by creating the wrapper which actually use the ODBCDataReader inside to read the content of the table(s). Every time the reader advance to next column i instruct the method to do the conversion. The code looks like below:
     Public Shared Sub FillDataSet(ByRef daName As OdbcDataAdapter, ByVal tableToFill As String, ByRef dsName As DataSet)
        Dim rdr As OdbcDataReader
        Dim shortCol As Short
        Dim schema, table, tbl As DataTable
        Dim tblName As ArrayList
        Dim drSchema, drData As DataRow
        Dim i As Integer
        Dim firstRow As Boolean = True
        rdr = daName.SelectCommand.ExecuteReader()
        While rdr.Read()
            If firstRow Then
                If Not dsName.Tables.Contains(tableToFill) Then
                    table = New DataTable(tableToFill)
                    schema = rdr.GetSchemaTable()
                    For Each drSchema In schema.Rows
                        table.Columns.Add(drSchema("ColumnName").ToString, drSchema("DataType"))
                    Next drSchema
                    dsName.Tables.Add(table)
                End If
                tbl = dsName.Tables(tableToFill)
                firstRow = False
            End If
            drData = tbl.NewRow()
            Dim resultStr As String
            For i = 0 To tbl.Columns.Count - 1
                If tbl.Columns(i).DataType.Name = "Int16" Then
                    Try
                        drData.Item(i) = Convert.ToInt16(rdr.GetInt32(i))
                    Catch ex As Exception
                        ' do nothing
                    End Try
                Else
                    Try
                        drData.Item(i) = rdr.Item(i)
                    Catch ex As Exception
                        ' do nothing
                    End Try
                End If
            Next i
            tbl.Rows.Add(drData)
        End While
        rdr.Close()
    End Sub

Above code are working fine for the int16 column which is not empty/null. But if it is null it'll throw an exception, so to prevent this i use the IsDBNULL to check against the content of the column. But the result is worst, because if i put the checking for null value all the conversion like below won't work even the content of column is not null.
 ...
 drData.Item(i) = Convert.ToInt16(rdr.GetInt32(i))
 ...
Could someone tell me if i'm doing wrong or is there any way to solve my problem other than to have simulate the Fill method like i do with above code.
Please i need your help.
Thanks
Rusli


 
Old December 15th, 2004, 03:24 PM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am new too!
How do you check against Null value?
One way to go around checking for null is to cast it into string like so:
if ("" & SomeVariantVar) = "" then

I don't know if this will work for your case.

Cheers.


 
Old December 15th, 2004, 11:17 PM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The way i do the checking against the null value is like below:

' if the column have a content then convert it
if Not rdr.IsDBNull(i) then ...
     Try
      drData.Item(i) = Convert.ToInt16(rdr.GetInt32(i))
     Catch ex As Exception
          ' do nothing
     End Try
end if
above code will always throw an exception regardless Null or not null.
By now i take out the checking for null value and it works fine for the not null column, because when the column content is null it will throw an exception that's why i didn't put any message when it throw an exception. But it delay the process a little bit. What i want is to make the conversion smooth.

i've try your code but still it throw the same exception as
"Specified cast is not valid." But thanks anyway pal : )

Thanks in advance,
rusli

 
Old December 17th, 2004, 06:32 PM
Registered User
 
Join Date: Dec 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Check this link

http://msdn.microsoft.com/library/de...snulltopic.asp





Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid Use of Null arholly Access VBA 5 February 5th, 2007 08:18 AM
invalid column length message cronid SQL Server DTS 3 August 5th, 2006 12:49 PM
problem with Invalid column name 'TX'. bleutiger Classic ASP Databases 1 February 1st, 2005 02:35 PM
Invalid use of Null: 'cstr' shoakat Classic ASP Databases 1 November 2nd, 2004 07:51 PM
Invalid use of Null LT2003 All Other Wrox Books 3 December 1st, 2003 04:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.