0

The 'OleDbException' says that the problem is a 'Type mismatch in expression.', but I've checked in the database (access) and the combobox, and both are string type.

I've tried making the combobox as string with comboBox_CourseID.SelectedItem.ToString() and changing to comboBox_CourseID.Text, but still the same exact error. I've also tried checking the type of column in Access, but it was Short Text. I've tried using a textbox instead of a combobox, but to no success.

This is my code:

OleDbCommand show_table = new OleDbCommand
{
    Connection = connect,

    CommandText = "Select Student.StudentID, Student.First_Name, Student.Last_Name, Student.Contact_Number, Student.Email " +
    "From Student Inner Join Course on Course.CourseID = Student.CourseID " +
    "Where Course.CourseID = '" + comboBox_CourseID.SelectedItem.ToString() + "'"
};

OleDbDataReader dataReader =show_table.ExecuteReader();  
*//error here* 

if (dataReader.Read() == false)
    MessageBox.Show("Record Not Found.");

dataReader.Close();

OleDbDataAdapter displayCourseID = new OleDbDataAdapter(show_table);
DataTable table = new DataTable();
displayCourseID.Fill(table);
dataGridView_StudentInfo.DataSource = table;

I expected it to fill the dataGridView with the data from the database (Access) as table.

Note: I put the Student.CourseID as MultiValued in Access, will that affect the type?

Axell Lim
  • 23
  • 6
  • comboBox_CourseID is int maybe. – ares777 Jun 07 '19 at 09:41
  • 3
    is `Course.CourseID` of an `integer` type? if yes you are trying to compare and `int` with a `string` as you have enclosed your concatenated value in single quotes(`'`). And just a suggestion try to avoid creating a `dynamic` queries instead use [parameterized queries](https://stackoverflow.com/a/5893956/2417602). – vikscool Jun 07 '19 at 09:44
  • I set the CourseID as Short Text in Access, so it's probably string – Axell Lim Jun 07 '19 at 10:34

0 Answers0