I'm loading a combobox from a stored procedure, which appears to be working fine(see code below). So now I want to get the ValueMember from the combobox when the user selects a value and here's where my problem starts.
I figured I could get the ValueMember from the SelectedIndexChanged event by using the following:
comboboxValue = Me.cboCategory.SelectedValue
When I use this code I get an error (Argument 'Prompt' cannot be converted to type 'String'.) during what appears to be the loading of the combobox.
I can however remove that code, add a button to the form and after a value is selected from the combobox, click the button and the same code returns the correct value.
I next tried to drop a message box to see the SelectedIndex value in the SelectedIndexChanged event and to my amazement it appeared that during the loading of the combo box the message box show three values, being 0,0 and -1 respectively.
I next tried the Click event of the combobox adding a messagebox returning the msgbox(SelectedIndex), but as soon as I click on the drop down arrow on the combo box the message box comes up with a -1 value and closes the combo box.
So the questions are:
1. Am I loading the combobox correctly from the stored procedure?
2. Why is the combobox returning index values (0,0,-1) during the load?
3. Why is the Click event return -1 when I click the drop down on the combobox?
If I'm doing something wrong could someone give me some code examples on how to return the ValueMember from the combobox and which event should I use.
Thanks,
Tom
Public Function LoadCategories(ByVal cboBox As ComboBox) As Boolean
Dim cmdsub As New SqlCommand
Dim da As New SqlDataAdapter(cmdsub)
Dim ds As New DataSet()
If objConn.State <> ConnectionState.Open Then
objConn = objConnClass.GetConnection
End If
cmdsub.Connection = objConn
cmdsub.CommandText = "GetVendorCategories"
cmdsub.CommandType = CommandType.StoredProcedure
cmdsub.Parameters.Add("@rc", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
Try
da.Fill(ds)
da.Fill(ds, "Disk")
With cboBox
.DataSource = ds.Tables("Disk")
.DisplayMember = "VendorCategory"
.ValueMember = "vcID"
.SelectedIndex = -1
End With
return true
Catch ex As SqlException
MsgBox("Error Encountered In Loading Vendor Categories." & ex.Message, MsgBoxStyle.OkOnly, "Error")
Return false
cmdsub.Dispose()
objConn.Dispose()
End Try
End Function
I figured I could get the ValueMember from the SelectedIndexChanged event by using the following:
comboboxValue = Me.cboCategory.SelectedValue
When I use this code I get an error (Argument 'Prompt' cannot be converted to type 'String'.) during what appears to be the loading of the combobox.
I can however remove that code, add a button to the form and after a value is selected from the combobox, click the button and the same code returns the correct value.
I next tried to drop a message box to see the SelectedIndex value in the SelectedIndexChanged event and to my amazement it appeared that during the loading of the combo box the message box show three values, being 0,0 and -1 respectively.
I next tried the Click event of the combobox adding a messagebox returning the msgbox(SelectedIndex), but as soon as I click on the drop down arrow on the combo box the message box comes up with a -1 value and closes the combo box.
So the questions are:
1. Am I loading the combobox correctly from the stored procedure?
2. Why is the combobox returning index values (0,0,-1) during the load?
3. Why is the Click event return -1 when I click the drop down on the combobox?
If I'm doing something wrong could someone give me some code examples on how to return the ValueMember from the combobox and which event should I use.
Thanks,
Tom
Public Function LoadCategories(ByVal cboBox As ComboBox) As Boolean
Dim cmdsub As New SqlCommand
Dim da As New SqlDataAdapter(cmdsub)
Dim ds As New DataSet()
If objConn.State <> ConnectionState.Open Then
objConn = objConnClass.GetConnection
End If
cmdsub.Connection = objConn
cmdsub.CommandText = "GetVendorCategories"
cmdsub.CommandType = CommandType.StoredProcedure
cmdsub.Parameters.Add("@rc", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
Try
da.Fill(ds)
da.Fill(ds, "Disk")
With cboBox
.DataSource = ds.Tables("Disk")
.DisplayMember = "VendorCategory"
.ValueMember = "vcID"
.SelectedIndex = -1
End With
return true
Catch ex As SqlException
MsgBox("Error Encountered In Loading Vendor Categories." & ex.Message, MsgBoxStyle.OkOnly, "Error")
Return false
cmdsub.Dispose()
objConn.Dispose()
End Try
End Function