Exception when trying to fill DataTable with SqlDataAdapter

I am trying to make a SQL Login Form in VB.NET and it's not really working, well the form is loading but when i click Log in then it just crashes with an exception. Can someone tell me how to fix this?

Code:

Imports System.Data.SqlClient

Public Class Form1

Private Sub BTN_LOGIN_Click(sender As Object, e As EventArgs) Handles     BTN_LOGIN.Click

    Dim connection As New SqlConnection("Server= (localdb)\MSSQLLocalDB; Database = TestDB; Integrated Security = true")

    Dim command As New SqlCommand("select * from Table where Username = @username and Password = @password", connection)

    Dim reg As New SqlCommand("INSERT INTO Table (username, password) VALUES (@username, @password)")

    command.Parameters.Add("@username", SqlDbType.VarChar).Value = TextBoxUsername.Text
    command.Parameters.Add("@password", SqlDbType.VarChar).Value = TextBoxPassword.Text

    Dim adapter As New SqlDataAdapter(command)

    Dim data As New DataTable()

    adapter.Fill(data)

    adapter.Dispose()

    If data.Rows.Count() <= 0 Then

        MessageBox.Show("Username Or Password Are Invalid")


    Else

        MessageBox.Show("Login Successfully")

        Dim frm As New Form2()

        Me.Hide()

        frm.Show()

    End If

End Sub

Private Sub BTN_CANCEL_Click(sender As Object, e As EventArgs) Handles BTN_CANCEL.Click

    Me.Close()

End Sub
End Class

Also the table is fine. Just don't talk about the table

Exception

Upvotes: 0

Views: 1412

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460008

The exception states it: "Incorrect syntax near the keyword Table". Is the name of the table really Table? That is a reserved keyword. If it is the name you should consider to change it. But if you must use this name you need to embed it in square backets:

Dim command As New SqlCommand("SELECT * FROM [Table] where Username = @username and Password = @password", connection)

Upvotes: 3

Related Questions