Results 1 to 2 of 2

Thread: How to display data from database based on selected combobox value

  1. #1
    johnkosten is offline VB.NET Forum Newbie
    .NET Framework
    Join Date
    Aug 2005
    Posts
    2
    Reputation
    0

    How to display data from database based on selected combobox value

    Hello,

    I'm having trouble in displaying the information based on the combobox selection. Here is my code so far

    Private Sub cmbBoxIncidentDate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBoxIncidentDate.SelectedIndexChanged
    Dim dateValue As String
    dateValue = cmbBoxIncidentDate.SelectedValue
    Me.txtBoxIncidentType.Text = dateValue

    '*------------------------------------------------------------------------------------------------------

    Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Kasturi\My Documents\Visual Studio Projects\WindowsApplication1\db\test.mdb"
    Dim Cmd As OleDbCommand
    Dim Reader As OleDbDataReader
    Dim SQL As String

    Dim Con = New OleDbConnection(connString)

    SQL = "SELECT * FROM [Incident] WHERE [Incident_Date] = " & dateValue & ""
    Cmd = New OleDbCommand(SQL, Con)

    Con.Open()


    Con.Close()



    End Sub

    How do i reference the information needed to display in combo boxes? These are the fields in my database :- Incident_ID, Incident_Date, Incident_Name. Thanks in advance.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,504
    Reputation
    1553
    Note that if you want to specify a date literal in an SQL statement you should either enclose it in hash characters, e.g. #30/08/2005#, or use the DATEVALUE function (which may be Access-specific, although I'm not sure), e.g. DateValue('30/08/2005').

    You can create a parameterised query and set the values of the parameters based on the selections from your ComboBoxes, e.g.
    Code:
    Dim selectCommand As New OleDbCommand("SELECT * FROM [Incident] WHERE [Incident_Date] = @Incident_Date", Con)
    
    selectCommand.Parameters.Add("@Incident_Date", OleDbType.DBDate)
    
    selectCommand.Parameters("@Incident_Date").Value = dateValue
    Note that the command and parameter can be created at the class-level and thus reused. Then the only thing you need to do is set the parameter Value each time you want to perform a query.

Bookmarks

Posting Permissions

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