0

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

4
  • 1
    What exception?? Commented Feb 7, 2017 at 16:00
  • Click on exception. Commented Feb 7, 2017 at 16:04
  • 1
    not related to your issue, you should not store plain password, you should hash them, look it up (don't forget the salt and pepper) Commented Feb 7, 2017 at 16:08
  • 1
    Your table is named Table? I'm hoping this is just used as an example? The error implies that there's something wrong with the SQL itself... Commented Feb 7, 2017 at 16:20

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that's the problem. It does take long to login, but thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.