I got this exception when I'm trying to perform the INSERT INTO
command:
This is my code:
private void AddQuiz_Button_Submit_Click(object sender, EventArgs e)
{
// Connection when button is clicked
OleDbConnection myConnection = GetConnection();
string myQuery = "INSERT INTO QuestionBank (Question, Choice A, Choice B, Choice C, Choice D, Category, Correct Answer) VALUES ('" AddQuiz_RichTextBox_Question.Text "' , '" OptionA_TextBox.Text "' , '" OptionB_TextBox.Text "' , '" OptionC_TextBox.Text "' , '" OptionD_TextBox.Text "' , '" AddQuiz_Category_Listbox.Text "' , '" AddQuiz_ListBox_Answer.Text "')" ; //SQuery statement
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
try
{
// Opening connection
myConnection.Open();
AddQuiz_Label_CheckConnection.Text = "Connection Successful";
// Execute the query
myCommand.ExecuteNonQuery();
MessageBox.Show("Question saved");
}
catch (Exception ex)
{
MessageBox.Show("Exception in DBHandler" ex);
}
finally
{
// Close connection
myConnection.Close();
}
}
CodePudding user response:
I seen that 800xxxx message. In near all cases it means your project is running at the wrong bit size for the installed version of Access.
If you cpu setting is "Any", then you need to change the project settings to x86.
Give that a try.
So this setting:
And then in configuration manager - create a new setting for x86
So new config, and then:
Change dropdown to x86 - and yes, you can accept the default "Copy settings from"
You now have this:
Then close button - and you now have this:
And of course the reverse is also true - if you running office x64, then force the project to x64 and again do not use "any".
CodePudding user response:
The syntax error is caused by the spaces in your field names. Such names must, in Access SQL, be bracketed:
string myQuery = "INSERT INTO QuestionBank (Question, [Choice A], [Choice B], [Choice C], [Choice D], Category, [Correct Answer]) VALUES ('" AddQuiz_RichTextBox_Question.Text "' , '" OptionA_TextBox.Text "' , '" OptionB_TextBox.Text "' , '" OptionC_TextBox.Text "' , '" OptionD_TextBox.Text "' , '" AddQuiz_Category_Listbox.Text "' , '" AddQuiz_ListBox_Answer.Text "')" ; //SQuery statement
That said, do learn to use parameters - much cleaner and easier to debug.