Skip to content
Advertisement

Displaying Data From Database With Vb.net with combobox

I face an error in my syntax which is indicating near the AND Operator

If ComboBox1.Text = "" And ComboBox2.Text = "" Then
    MsgBox("Please Fill In The Details")
Else
    Try
        con.Open()
        Select Case ComboBox1.Text
            Case "Students Info"
                Dim a = "SELECT * FROM studentdb1 WHERE class = " & ComboBox2.SelectedText & " AND WHERE date_registered BETWEEN " & date1.Text & " AND " & date2.Text & ""
                displayTable(a)
        End Select

        con.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End If

I created a class called displayTable to use command passed through the parameter to display into the datagridview

Public Sub displayTable(x As String)
    Dim command2 As New SqlClient.SqlCommand
    command2 = con.CreateCommand
    command2.CommandText = x
    command2.ExecuteNonQuery()

    Dim dt As New DataTable
    Dim da As New SqlDataAdapter(command2)
    da.Fill(dt)
    datagrid1.DataSource = dt
End Sub

Advertisement

Answer

It is a good idea to divide the code into User Interface code that interacts with the controls on the form and data access code that interacts with the database.

The Using...End Using block takes care of closing and disposing the connection and command even if there is an error.

The use of Parameters protects your database from sql injection. I had to guess at the size of the class column. Check your database for the actual value.

.ExecuteNonQuery is for Insert, Update and Delete not Select. You can load a DataTable with a reader.

Private ConStr As String = "Your connection string"
Private Function GetStudentsInfo(clss As String, d1 As Date, d2 As Date) As DataTable
    Dim dt As New DataTable
    Using con As New SqlConnection(ConStr),
            command2 As New SqlCommand("SELECT * FROM studentdb1 WHERE class = @Class AND date_registered BETWEEN @Date1 AND @Date2;", con)
        With command2.Parameters
            .Add("@Class", SqlDbType.VarChar, 100).Value = clss
            .Add("@Date1", SqlDbType.Date).Value = d1
            .Add("@Date2", SqlDbType.Date).Value = d2
        End With
        con.Open()
        dt.Load(command2.ExecuteReader)
    End Using
    Return dt
End Function

In the UI

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Validate input
    If ComboBox1.Text = "" OrElse ComboBox2.Text = "" Then
        MsgBox("Please Fill In The Details")
        Return
    End If
    Dim FirstDate, SecondDate As Date
    If Not Date.TryParse(Date1.Text, FirstDate) Then
        MessageBox.Show("Please enter a valid date.")
        Return
    End If
    If Not Date.TryParse(Date2.Text, SecondDate) Then
        MessageBox.Show("Please enter a valid date.")
        Return
    End If
    Dim dt As DataTable
    Try
        Select Case ComboBox1.Text
            Case "Students Info"
                dt = GetStudentsInfo(ComboBox2.Text, FirstDate, SecondDate)
            Case Else
                dt = Nothing
        End Select
    Catch ex As Exception
        MsgBox(ex.Message)
        dt = Nothing
    End Try
    DataGridView1.DataSource = dt
End Sub
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement