I have DatagridView on form.

My database has three fields.
- userId (bigint)
- userName (varchar)
- userDob (date)

Now, on Form I have comboBox, which lists Months (Jan, Feb, March and so on...). I want my program to work like this.

If I select any month on ComboBox, birthdays of clients related to that month should only be displayed in DatagridView. Help me how to do it.

Dani AI

Generated

As requested, the goal is to show only rows whose DOB falls in the selected month. Both and are on the right track: filtering by month is the correct idea. A more robust pattern is to pass the month as a parameter from the ComboBox and let the SQL compare the month number (not a formatted string). This avoids localization/formatting pitfalls and eliminates string concatenation (safer and immune to injection).

Example VB.NET pattern (WinForms, SqlClient):

Imports System.Data.SqlClient

Private Sub cbMonth_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbMonth.SelectedIndexChanged
    Dim monthNumber As Integer = cbMonth.SelectedIndex + 1   ' assumes items Jan..Dec in order
    LoadBirthdays(monthNumber)
End Sub

Private Sub LoadBirthdays(monthNumber As Integer)
    Dim sql As String =
        "SELECT userId, userName, userDob " &
        "FROM Users " &
        "WHERE MONTH(userDob) = @month " &
        "ORDER BY DATEPART(day, userDob)"

    Using cn As New SqlConnection(connectionString)
        Using cmd As New SqlCommand(sql, cn)
            cmd.Parameters.Add("@month", SqlDbType.TinyInt).Value = monthNumber
            Dim da As New SqlDataAdapter(cmd)
            Dim dt As New DataTable()
            da.Fill(dt)
            DataGridView1.DataSource = dt
        End Using
    End Using
End Sub

Notes and troubleshooting:

  • If userDob can be NULL, add an appropriate NULL check in the WHERE clause.
  • For large tables, applying a function to a column (MONTH(userDob)) prevents use of a plain index. Consider adding a computed, persisted month column and index it for performance:
ALTER TABLE Users ADD userDobMonth AS MONTH(userDob) PERSISTED;
CREATE INDEX IX_Users_userDobMonth ON Users(userDobMonth);
  • If the ComboBox is populated with month names, map names to numbers reliably (SelectedIndex+1 is simplest when months are listed in order). For non‑SQL Server backends the function name may differ (e.g., EXTRACT/MONTH equivalents).

Recommended Answers

All 2 Replies

add this to your query.

where userDob like '"+cbYear.Text+"-"+cbMonth.Text+"-%' order by userDob

example output: `where userDob like '2015-01-%' order by userDob`

the format of date is `yyyy-MM-dd`:


for `yyyy` represent 4 digit year.
    `MM` represent 2 digit month.
    `dd` represent 2 digit day.



Happy Programming and God Bless you.

If this is SQL Server then you could use the DATENAME method:

SELECT * FROM YourTable where DATENAME(month, YourDateField) = 'July'

In this way, you can select all records that match a month regardless of year which is what I believe you want.

commented: May be this will solve my problem... I'll try this code ASAP +0
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.