#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2009
    Posts
    66
    Rep Power
    6

    VB.NET Trigger event after combox selected


    I am trying to run an SQL query on a database ONLY when the database has been selected from a dropdown list.

    I have tried the TextChanged and SelectedValueChanged but the both fire before the dropdown item is selected.

    here is my code

    Code:
      Private Sub cmbDataBases_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDataBases.s
            Dim connectString As String
            Dim selectSQL As String
            Dim errorMessages As New StringBuilder
            If Me.cmbServer.Text <> "" And Me.cmbDataBases.Text <> "" Then
                If Me.opUserAuthentication.Checked = True Then
                    connectString = "Server=" & Me.cmbServer.Text & ";Database=" & Me.cmbDataBases.Text & ";User Id=" & Me.opUsername.Text & ";Password=" & Me.opPassword.Text
                ElseIf My.Settings.windowsAuthentication = True Then
                    connectString = "Data Source=" & Me.cmbServer.Text & " ;Integrated Security=True;"
                Else
                    connectString = vbNullString
                End If
    
                Using vercon As New SqlConnection(connectString)
    
                    Try
                        'Open connection
                        vercon.Open()
                        MessageBox.Show(connectString)
    
                    Catch ex As SqlException
                        Dim i As Integer
                        For i = 0 To ex.Errors.Count - 1
                            errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _
                            & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
                            & "Error Number: " & ex.Errors(i).Number & ControlChars.NewLine _
                            & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
                            & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
                            & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
                        Next i
                        MsgBox(errorMessages.ToString())
                        Return
                    End Try
    
                    'Get databases names in server in a datareader
                    selectSQL = "SELECT Version FROM META_GaMIMS_Version;"
    
                    Try
                        Dim com As SqlCommand = New SqlCommand(selectSQL, vercon)
                        Dim dr As SqlDataReader = com.ExecuteReader()
    
                        dr.Read()
                        Me.TextBox1.Text = dr(0)
                    Catch ex As Exception
                        MessageBox.Show("Not a Valid GaMIMS Database - textchanged")
    
                    End Try
                End Using
            End If
        End Sub
    Last edited by itrs; December 1st, 2013 at 11:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo