471,073 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

error message.

Hi,

I am running a stored procedure when the user selects an
item from a combobox (combobox1). This stored procuedure
gets values based on what was just chosen by the user in
combobox1 and enters these values in combobox2.

However i keep encountering the following error message
which i do not know how to resolve. Combobox2 does not
get populated due to this error....

Specified cast is not valid.

here is my code....
'Display the caravan length based on the caravan name
selected.
Dim Cn As New SqlConnection("initial
catalog=TestCaraDBmsde;integrated security=SSPI;persist
security info=False;workstation id=USER;packet size=4096")
Dim cmdLength As New SqlCommand("SPSelLengths", Cn)
cmdLength.CommandType =
CommandType.StoredProcedure

'declare the parameters
cmdLength.Parameters.Add("@CaraName",
SqlDbType.Char, 50)
'now set the values
cmdLength.Parameters("@CaraName").Value =
CBoxDisCaraNames.SelectedItem
CBoxCaraLength.Items.Clear()
CBoxCaraLength.Text = ""

Try
Cn.Open()
Dim reader3 As SqlDataReader
reader3 = cmdLength.ExecuteReader()
While reader3.Read
CBoxCaraLength.Items.Add
(reader3.GetSqlString(0))
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
Cn.Close()
End Try
End Sub

can anyone help me resolve this?
Jul 21 '05 #1
1 1022
Is it possible that for some rows

reader3.GetSqlString(0)

Returns a Null Value?

To help you track down which line the error is actually happening on:

1) Debug Menu > Exceptions
2) Find "InvalidCastException" in the tree
3) Select "Break into the debugger" for this exception when it is thrown.

This should help you narrow down the possibilities.

Also I'd recommend switching "Option Strict" on for your project. This will
force you to be a little bit more consious about what variables you assign
to other types of variables.

Hope this helps,

Trev.
"Angelina" <an*******@discussions.microsoft.com> wrote in message
news:0e****************************@phx.gbl...
Hi,

I am running a stored procedure when the user selects an
item from a combobox (combobox1). This stored procuedure
gets values based on what was just chosen by the user in
combobox1 and enters these values in combobox2.

However i keep encountering the following error message
which i do not know how to resolve. Combobox2 does not
get populated due to this error....

Specified cast is not valid.

here is my code....
'Display the caravan length based on the caravan name
selected.
Dim Cn As New SqlConnection("initial
catalog=TestCaraDBmsde;integrated security=SSPI;persist
security info=False;workstation id=USER;packet size=4096")
Dim cmdLength As New SqlCommand("SPSelLengths", Cn)
cmdLength.CommandType =
CommandType.StoredProcedure

'declare the parameters
cmdLength.Parameters.Add("@CaraName",
SqlDbType.Char, 50)
'now set the values
cmdLength.Parameters("@CaraName").Value =
CBoxDisCaraNames.SelectedItem
CBoxCaraLength.Items.Clear()
CBoxCaraLength.Text = ""

Try
Cn.Open()
Dim reader3 As SqlDataReader
reader3 = cmdLength.ExecuteReader()
While reader3.Read
CBoxCaraLength.Items.Add
(reader3.GetSqlString(0))
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
Cn.Close()
End Try
End Sub

can anyone help me resolve this?

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Mairhtin O'Feannag | last post: by
16 posts views Thread by | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.