Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing a login function for an application and I am getting the
C#
Incorrect syntax near the keyword 'USER'. at System.Data.SqlClient.SqlConnection


This is the code for when the login button is clicked

C#
protected void loginButton_Click(object sender, EventArgs e)
     {
         try
         {

             dbConnection.Open();
             SqlCommand cmd = new SqlCommand("SELECT * FROM USER WHERE EmailAddress = @EmailAddress and Password = @Password", dbConnection);
             cmd.Parameters.AddWithValue("@EmailAddres", tbEmail.Text);
             cmd.Parameters.AddWithValue("@Password", tbPass.Text);
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             DataTable dt = new DataTable();
             da.Fill(dt);
             if (dt.Rows.Count > 0)
             {
                 Response.Redirect("Default.aspx");
             }
             else
             {
                 lblText.Text = "WRONG USERNAME OR PASSWORD";
                
             }

         }

         catch (SqlException en)
         {
             lblText.Text = en.ToString();
         }
         finally
        {
             dbConnection.Close();
         }

     }


What I have tried:

Google searches have not been helpful.
Posted
Updated 14-Jun-16 11:56am
Comments
KevinClaassens 14-Jun-16 17:48pm    
The connection string is in the web.config. It is as follows..
<connectionstrings>
<add name="dbConnection"
="" connectionstring="server=KEVIN-PC;Integrated Security=true;database=XCellIT;">
PIEBALDconsult 14-Jun-16 18:19pm    
Try [USER]

USER is a reserved keyword in SQL:
Reserved Keywords (Transact-SQL)[^]

You either need to change your table name to something else, or enclose it in square brackets:
SQL
SELECT * FROM [USER] WHERE ...


You also don't need to select all columns and load a DataTable just to test whether a row exists:
C#
using (SqlCommand cmd = new SqlCommand("SELECT 1 FROM USER WHERE EmailAddress = @EmailAddress and Password = @Password", dbConnection))
{
    cmd.Parameters.AddWithValue("@EmailAddres", tbEmail.Text);
    cmd.Parameters.AddWithValue("@Password", tbPass.Text);
    
    object result = cmd.ExecuteScalar();
    if (result == null || Convert.IsDBNull(result))
    {
        lblText.Text = "WRONG USERNAME OR PASSWORD";
    }
    else
    {
        Response.Redirect("Default.aspx");
    }
}


You're storing passwords as plain-text. That's a security vulnerability waiting to happen. You should only ever store a salted hash of the user's password:
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]


And why are you re-inventing the wheel? ASP.NET has several perfectly good authentication systems available already - for example:
ASP.NET Identity | The ASP.NET Site[^]
BrockAllen.MembershipReboot[^]
Introduction to Membership[^]
 
Share this answer
 
Comments
KevinClaassens 14-Jun-16 17:59pm    
Thank you so much!!!
The user is a reserved keyword you could try using it in square brackets and it should work

select * from [user]
 
Share this answer
 
One thing it appears typo at EmailAddress at(cmd.Parameters.AddWithValue(@EmailAddres, tbEmail.Text);) where s is missing.
Also if possible try to run same query in SQL management studio
 
Share this answer
 
Comments
KevinClaassens 14-Jun-16 17:50pm    
I still get the same error in SQL management studio

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900