ComboBox SelectedIndexChanged

L

Lezza

I found a lot of posters asking the following question,
but no answer? *Frustrated at the SQL PASS Convention*

Okay, SQL Database using SQLDataAdapter and a DataTable.

I've got one form with two ComboBoxes. On Form Load, I
fill da with a SELECT SPROC and populate ComboBox1. Works
great.

Now, I need to take SelectedValue from ComboBox1, and pass
it as a parameter on SelectedIndexChanged event to another
SPROC which will fill da and populate ComboBox2. On Debug,
I'm getting 'System.InvalidCastException' which leads me
to believe I'm not returning any values on da.Fill.

Following Code:

Private Sub cmbPersonalityTypeSP1_SelectedIndexChanged
(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
cmbPersonalityTypeSP1.SelectedIndexChanged
'Create Connection String
Dim con As New SqlConnection("Data Source=
(local);Initial Catalog=StoryBook;Integrated
Security=True")
'Create New DataTable for Results Set
Dim dsPersTrait As New DataTable
'Create variable to hold sproc command
Dim cmd As New SqlCommand("c_GetMainTrait", con)
' Create a data adapter and pass in the SQL
statement and connection reference
Dim da As New SqlDataAdapter(cmd)
'Tell Object it is a SPROC
cmd.CommandType = CommandType.StoredProcedure
' Fill Parameter Value from ComboBox
cmd.Parameters.Add(New SqlParameter
("@TraitTypeID", SqlDbType.Int)).Value = _
cmbPersonalityTypeSP1.SelectedValue

da.SelectCommand.Connection.Open()
'Fill Dataset
da.Fill(dsPersTrait) <--ERROR Occurs HERE
da.SelectCommand.Connection.Close()

' Set the combo box data source to our datatable
cmbTraitSP1.DataSource = dsPersTrait
' Set the display value for the combobox to be the
column to be displayed
cmbTraitSP1.DisplayMember = "Trait"
' Set the id value for the combobox
cmbTraitSP1.ValueMember = "TraitID"

End Sub
 
B

Brian P. Hammer

Just throwing some out....

Have you tried the various .Text / .SelectedText / .SelectedIndex and the
likes for getting the value for your parameter? Just a thought?

Put some debug.writeline in to see what is happening and what value is being
used.

Just a couple of thoughts.
 
D

David

Okay, SQL Database using SQLDataAdapter and a DataTable.

I've got one form with two ComboBoxes. On Form Load, I
fill da with a SELECT SPROC and populate ComboBox1. Works
great.

Now, I need to take SelectedValue from ComboBox1, and pass
it as a parameter on SelectedIndexChanged event to another
SPROC which will fill da and populate ComboBox2. On Debug,
I'm getting 'System.InvalidCastException' which leads me
to believe I'm not returning any values on da.Fill.

How exactly are you binding the first ComboBox1? One cause of what you
see below is if you set DataSource before you set ValueMember and
DisplayMember, then you get spurious IndexChanged events where
SelectedValue is a DataRow.

Are you getting the cast error before you select anything, that is
as soon as the form loads?
 

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