2

The error I am receiving is "Incorrect syntax near 'user'" - however, I cannot see anything wrong with my code - does anyone have any ideas?

protected void Page_Load(object sender, EventArgs e)
{
    string db = "";
    db = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
    SqlConnection con = new SqlConnection(db);
    con.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT * FROM user";
    cmd.Connection = con;
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        Response.Write(dr[0].ToString());
        Response.Write(dr[1].ToString());
        Response.Write(dr[2].ToString());
        Response.Write(dr[3].ToString());
    }

    con.Close();
}
2
  • Do you have a table called user? Is your connection string correct? Commented Sep 14, 2012 at 7:18
  • 1
    Is it because user is a reserved word (as well as, presumably, the name of your table)? Try "SELECT * FROM [user]" Commented Sep 14, 2012 at 7:19

3 Answers 3

3

Try this

"SELECT * FROM [user]";

user is a keyword/reserved word. I guess that's the problem. I always end up using USERS instead of user. Try as much as possible to avoid using reserved words as column names, table names, etc. Use them whenever you can't avoid it

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

Comments

3

user is a reserved word - you would need to change it to

"select * from [user]"

Comments

2

Is it because user is a reserved word (as well as, presumably, the name of your table)?

Try

"SELECT * FROM [user]"

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.