Results 1 to 2 of 2

Thread: SQL & Loading results into combobox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    SQL & Loading results into combobox

    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

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    a collection is referenced by an index. you have to use .selectedindex to tell the program what you picked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width