0

Why am I getting an error when I use Inner join in C#? The query is working when I try it in Access.

try
{
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    string query = "SELECT BookID, BookName, CategoryDescription, Language, AuthorName, YearPublished FROM tblCategory INNER JOIN (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) ON tblCategory.CategoryID = tblBook.CategoryID";
    command.CommandText = query;

    OleDbDataAdapter da = new OleDbDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataGridView1.DataSource = dt;

    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Error " + ex);
}

enter image description here

1
  • I'm not into Access, but is that INNER JOIN between brackets ok? It's not valid T-SQL at least. Have you tried if a very simple select, and then start adding stuff back to see where it fails? Commented Jul 15, 2016 at 6:08

1 Answer 1

2

Actually, it's because you're using the keyword language in your query. you can get around this by changing it to [Language]

Change your query from

string query = "SELECT BookID, BookName, CategoryDescription, Language, AuthorName, YearPublished FROM tblCategory INNER JOIN (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) ON tblCategory.CategoryID = tblBook.CategoryID";

to

string query = "SELECT BookID, BookName, CategoryDescription, [Language], AuthorName, YearPublished FROM tblCategory INNER JOIN (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) ON tblCategory.CategoryID = tblBook.CategoryID";

Just to be safe, always use square brackets([]) around all column names and table names.

For future reference, here's a list of keywords for Access 2007. It might have grown since:

Access Reserved words

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

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.