Specified cast is not valid

D

Drygast

I'm newbie to programming and it seems I'm stuck on this problem....

the code:
_____________________________
Private Sub fetchOrder()

If txtOrdernummer.Text <> "" Then

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\temp\dummy.mdb"

Dim cmd As New OleDbCommand("Select [Order].* From [Order] Where
Order.Ordernummer = " & txtOrdernummer.Text & "", New
OleDbConnection(strConn))

cmd.Connection.Open()

Dim myReader As OleDbDataReader = cmd.ExecuteReader()

Try

While myReader.Read()

lblButik.Text = myReader.GetInt32(2).ToString

lblAnkom.Text = myReader.GetDateTime(3).ToShortDateString

If myReader.GetDateTime(6).ToShortDateString <> "" Then

lblSkickad.Text = myReader.GetDateTime(6).ToShortDateString

End If

End While

Finally

myReader.Close()

cmd.Connection.Close()

End Try

Else

MsgBox("Ange Ordernummer!")

txtOrdernummer.Focus()

End If

End Sub

___________________


The Problem:
The code gets data from an access database, it works like a charm if there
is a value in the field.
However, when the field is null in the database I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred in
system.data.dll

Additional information: Specified cast is not valid.

It seems like there's something wrong with this line:
"If myReader.GetDateTime(6).ToShortDateString <> "" Then"
but I can't figure it out..

Regards
/Drygast
 
K

Ken Tucker [MVP]

Hi,

Try checking to see if the field is a dbnull instead or ""

If not myreader.isdbnull(6) Then

lblSkickad.Text = myReader.GetDateTime(6).ToShortDateString

End If

Ken
 
D

Drygast

whow, thanks for the fast response!!!!
your suggestion worked! You're the man!

Thanks Ken
/Drygast

Ken Tucker said:
Hi,

Try checking to see if the field is a dbnull instead or ""

If not myreader.isdbnull(6) Then

lblSkickad.Text = myReader.GetDateTime(6).ToShortDateString

End If

Ken
---------------------------
Drygast said:
I'm newbie to programming and it seems I'm stuck on this problem....

the code:
_____________________________
Private Sub fetchOrder()

If txtOrdernummer.Text <> "" Then

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\temp\dummy.mdb"

Dim cmd As New OleDbCommand("Select [Order].* From [Order] Where
Order.Ordernummer = " & txtOrdernummer.Text & "", New
OleDbConnection(strConn))

cmd.Connection.Open()

Dim myReader As OleDbDataReader = cmd.ExecuteReader()

Try

While myReader.Read()

lblButik.Text = myReader.GetInt32(2).ToString

lblAnkom.Text = myReader.GetDateTime(3).ToShortDateString

If myReader.GetDateTime(6).ToShortDateString <> "" Then

lblSkickad.Text = myReader.GetDateTime(6).ToShortDateString

End If

End While

Finally

myReader.Close()

cmd.Connection.Close()

End Try

Else

MsgBox("Ange Ordernummer!")

txtOrdernummer.Focus()

End If

End Sub

___________________


The Problem:
The code gets data from an access database, it works like a charm if there
is a value in the field.
However, when the field is null in the database I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred in
system.data.dll

Additional information: Specified cast is not valid.

It seems like there's something wrong with this line:
"If myReader.GetDateTime(6).ToShortDateString <> "" Then"
but I can't figure it out..

Regards
/Drygast
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top