Hello,
I am trying to load the results of my SQL query into a combobox called cboTemplate.
That works, but is there a way to select one of the items and do something with it?
I tried:
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If cboTemplate.SelectedItem = "New" Then MsgBox("wow") End If End Sub
But it returns an error:
An unhandled exception of type 'System.InvalidCastException'
occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DataRowView' to
type 'String' is not valid.
'Here is my code
VB Code:
Private Sub cboVolume_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboVolume.SelectedIndexChanged Dim strSQL As String 'Connect to the data source. Dim objConn As SqlConnection = New SqlConnection( _ "Initial Catalog=Card Services;" & _ "Data Source=CHRISXP\LASERFICHE;Integrated Security=SSPI;") Me.cboTemplate.Items.Clear() Try objConn.Open() strSQL = "SELECT DISTINCT Tstr.TemplateName " strSQL &= "FROM Vol, Toc, Tstr WHERE Vol.VolumeName = " & _ "'" & cboVolume.SelectedItem & "'" & _ " AND Vol.VolumeId = Toc.VolumeId AND Toc.TemplateId = Tstr.TemplateId" Dim command As SqlCommand = New SqlCommand(strSQL, objConn) Dim objAdapter As SqlDataAdapter = New SqlDataAdapter(command) Dim dataSet As DataSet = New DataSet() objAdapter.Fill(dataSet, "Tstr") Me.cboTemplate.DataSource = dataSet.Tables("Tstr").DefaultView Me.cboTemplate.DisplayMember = "TemplateName" Catch ex As Exception MsgBox(ex.Message) Finally objConn.Close() End Try End Sub
Thanks for your suggestions!
Chris




Reply With Quote