0

i am stuck in my login page..my button click event is as follows:

protected void Button1_Click(object sender, EventArgs e)         
{               
    string cs = "Data Source=ims-aab46237892;Initial Catalog=Inventory;Integrated          Security=True";                      

    string SelectString = "SELECT COUNT(*) FROM user WHERE username = @Username AND password = @Password";           

    SqlConnection con = new SqlConnection(cs);         
    SqlCommand cmd = new SqlCommand(SelectString,con);         
    cmd.Connection = con;         
    cmd.CommandType = CommandType.Text;         
    cmd.CommandText = SelectString;          

    SqlParameter username = new SqlParameter("@Username",SqlDbType.VarChar,50);                   
    username.Value = TextName.Text.Trim().ToString();                   
    cmd.Parameters.Add(username);           

    SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50);                      
    password.Value = TextPass.Text.Trim().ToString();                      

    cmd.Parameters.Add(password);                      
    con.Open();  

    if(cmd.ExecuteScalar() != null)                 
        Response.Redirect("Home.aspx");            
     else                
        Response.Redirect("wrongpasspage.aspx");              

    con.Close(); 
}  

and my data table has the required username and password fields.. error i am getting is incorrect syntax near keyword user... plz help

1
  • Potential duplicate. Please see my post here: - [ASP.NET Login with SQL Server Database - LINQ-To-SQL, Entity Framework, and Publish/Subscribe event pattern][1] [1]: stackoverflow.com/questions/13397268/… Commented Dec 15, 2013 at 2:15

2 Answers 2

3

user is a reserved keyword in SQL server. Try [user] or rename your table to Users.

Sign up to request clarification or add additional context in comments.

2 Comments

its working now. but regardless of any username or password. i am directed to home.aspx
Copy and paste the value of SelectString from your debugging window into SQL server and run the query, see what happens.
0

use [user] instead of user in SelectString statement.

Comments

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.