Hi im having trouble linking my database. I dont understand what ive done wrong and keep getting the following message:

Syntax error in FROM clause.
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at...................................


My code ive used is:

using System.Data.OleDb;

namespace coursework2
{
    public partial class Form1 : Form
    {
        public OleDbConnection Coursework2;
        public Form1()
        {
            InitializeComponent();
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Coursework2.mdb";
            try
            {
                Coursework2 = new OleDbConnection(connectionString);
                Coursework2.Open();

                string queryString = "SELECT UserID, Forename, Surname FROM User";
                loadDataGrid(queryString);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + '\n' + ex.StackTrace);
            }
        }
        public void loadDataGrid(string sqlQueryString)
        {
            OleDbCommand SQLQuery = new OleDbCommand();
            SQLQuery.CommandText = sqlQueryString;
            SQLQuery.Connection = Coursework2;

            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SQLQuery);

            DataTable data = new DataTable();
            dataAdapter.Fill(data);

            dataGridView1.DataSource = data;

            dataGridView1.Columns[0].Visible = false;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ReadOnly = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

any help would be appreciated.

Thanks :)

Maybe you should single quote the field names "SELECT 'UserID', 'Forename', 'Surname' FROM 'User'".

I am just guessing, but the problem must be of that type.

Thanks

Maybe you should single quote the field names "SELECT 'UserID', 'Forename', 'Surname' FROM 'User'".

I am just guessing, but the problem must be of that type.

Thanks

still get the same error :(

User is reserved word of MsAccess.

string queryString = "SELECT [UserID], [Forename], [Surname] FROM [User]";

User is reserved word of MsAccess.

string queryString = "SELECT [UserID], [Forename], [Surname] FROM [User]";

Of course!
Thank you very much!

Thanks @ssudra!

Glad I could help. Please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.