error message.

  • Thread starter Thread starter Angelina
  • Start date Start date
A

Angelina

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?
 
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.
 
Back
Top