Hi all, i'm working on a search function for a music CD store project.

The form consists of a combobox that displays the artiste names from my database during runtime.
Below the combobox are check boxes that represents the genre, such as Pop and Jazz.

So for example in run time, the user can select a artiste names from the combobox and check any genre he or she wants. After that the user will press a button name, 'Search', and it will lead to another form that will display the search results.

Right now i have finished the codes to display the values from my database into the combobox. I don't know what to do next. Please help :(

Thanks alot.

Dani AI

Generated

Short plan: read the selected artiste from the ComboBox, collect the checked genres, build a parameterized WHERE clause, run the query into a DataTable, then open frmSearchResults and bind that DataTable to a grid. This avoids globals, prevents SQL injection and keeps the search logic testable and reusable.

As asked for pseudo code, start with this:

  • Read selected artiste (ComboBox.SelectedValue or .Text depending on how you bound it).
  • Build a list of selected genres from the checkboxes.
  • Build a parameterized SQL WHERE clause dynamically (one parameter per selected genre).
  • Execute the query with a DataAdapter into a DataTable.
  • Pass the DataTable (or a simple SearchCriteria object) to frmSearchResults and set DataGridView.DataSource = results.

Example VB.NET skeleton (replace table/column names and connection string to match your DB):

Public Class SearchCriteria
  Public Property ArtisteId As Nullable(Of Integer)
  Public Property Genres As List(Of String)
  Public Sub New()
    Genres = New List(Of String)()
  End Sub
End Class

Private Function QueryCds(criteria As SearchCriteria) As DataTable
  Dim dt As New DataTable()
  Dim connString As String = "PUT_YOUR_CONNECTION_STRING_HERE"
  Using cn As New SqlConnection(connString)
    Using cmd As New SqlCommand()
      cmd.Connection = cn
      Dim whereParts As New List(Of String)()
      If criteria.ArtisteId.HasValue Then
        whereParts.Add("[ArtisteId] = @ArtisteId")
        cmd.Parameters.AddWithValue("@ArtisteId", criteria.ArtisteId.Value)
      End If
      If criteria.Genres.Count > 0 Then
        Dim pNames As New List(Of String)()
        For i = 0 To criteria.Genres.Count - 1
          Dim pn = "@g" & i.ToString()
          pNames.Add(pn)
          cmd.Parameters.AddWithValue(pn, criteria.Genres(i))
        Next
        whereParts.Add("[Genre] IN (" & String.Join(", ", pNames) & ")")
      End If
      cmd.CommandText = "SELECT * FROM [YourCdsTable]" & If(whereParts.Count>0, " WHERE " & String.Join(" AND ", whereParts), "")
      Using da As New SqlDataAdapter(cmd)
        da.Fill(dt)
      End Using
    End Using
  End Using
  Return dt
End Function

Troubleshooting notes: ensure ComboBox.ValueMember is set so SelectedValue returns the ID (or use .Text if you stored names); make sure the genre strings exactly match DB values (trim/case); always use Using blocks; prefer proper parameter types instead of AddWithValue for production. Pass a DataTable or a small SearchCriteria object to frmSearchResults rather than using shared/global state.

Recommended Answers

All 2 Replies

Show me your pseudo code.

Hi, thanks for the reply.

The following attachment is my whole assignment.
I am currently working on the form,
and by pressing the 'Search' button on that form, it will lead to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.