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:
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.  
  3. If cboTemplate.SelectedItem = "New" Then
  4.         MsgBox("wow")
  5. End If
  6.  
  7. 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:
  1. Private Sub cboVolume_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboVolume.SelectedIndexChanged
  2.  
  3.         Dim strSQL As String
  4.         'Connect to the data source.
  5.         Dim objConn As SqlConnection = New SqlConnection( _
  6.         "Initial Catalog=Card Services;" & _
  7.         "Data Source=CHRISXP\LASERFICHE;Integrated Security=SSPI;")
  8.  
  9.         Me.cboTemplate.Items.Clear()
  10.         Try
  11.  
  12.             objConn.Open()
  13.  
  14.             strSQL = "SELECT DISTINCT Tstr.TemplateName "
  15.             strSQL &= "FROM Vol, Toc, Tstr WHERE Vol.VolumeName = " & _
  16.                     "'" & cboVolume.SelectedItem & "'" & _
  17.                     " AND Vol.VolumeId = Toc.VolumeId AND Toc.TemplateId = Tstr.TemplateId"
  18.  
  19.             Dim command As SqlCommand = New SqlCommand(strSQL, objConn)
  20.             Dim objAdapter As SqlDataAdapter = New SqlDataAdapter(command)
  21.             Dim dataSet As DataSet = New DataSet()
  22.             objAdapter.Fill(dataSet, "Tstr")
  23.  
  24.             Me.cboTemplate.DataSource = dataSet.Tables("Tstr").DefaultView
  25.             Me.cboTemplate.DisplayMember = "TemplateName"
  26.  
  27.         Catch ex As Exception
  28.             MsgBox(ex.Message)
  29.         Finally
  30.             objConn.Close()
  31.         End Try
  32.     End Sub

Thanks for your suggestions!

Chris