Hi! When my login form is loaded, I wish to retrieve the list of usernames from the database login to a combo box on the login form. unfortunately I am stuck with this error messsage " syntax error in FROM clause". Below is my codes.. help me out please
Thank you.

String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:/Documents and Settings/Farheen/My Documents/login.mdb";

            //create and open the connection
            OleDbConnection conn = new OleDbConnection(connStr);
            conn.Open();

            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM user";

            //set up the adapter
            OleDbDataAdapter da = new OleDbDataAdapter("Select UserName from user",conn);
            da.SelectCommand = cmd;
            //create the dataset
           DataSet ds = new DataSet();
            
            da.Fill(ds);

            cbo_Username.DisplayMember = "UserName";
            cbo_Username.DataSource = ds.Tables[0];

Edited 6 Years Ago by Nick Evan: Snippet -> Thread, fixed tags

hi,

The problem is that 'user' is a sql keyword. Whilst it is syntactically possible to use keywords, it is best to avoid them. It is similar to trying to declare an variable like decimal decimal = 2; . In C# this throws an error, in SQL it will allow it.
To keep your column name as it is, use delimited identifiers ('[]') to ensure that the keyword is treated the way you intend it:

Select UserName from [user]

.

This article has been dead for over six months. Start a new discussion instead.