I tried writing codes to access database but there was OldDbException error at the line with the bold words. How should I change my words so that there will not be any errors at run time?
private void submitbutton_Click(object sender, EventArgs e)
{
availabilitytabControl.SelectedTab = orderlisttabPage;
OleDbConnection myAccessConn = myAccessConnection();
OleDbCommand myAccessCommand = new OleDbCommand();
DataSet myDataSet = new DataSet();
try
{
int i;
myAccessConn.Open();
String insert ="insert into Particulars (Title,FirstName,LastName,Nationality,PassportNumber,PhoneNumber) VALUES(";
for (i = 0; i < 100; i++)
{
myAccessCommand.CommandText = insert;
String title = titlecomboBox.Items[i].ToString();
String firstname = firstnametextBox.Text;
String lastname = lastnametextBox.Text;
String nationality = nationalitycomboBox.Items[i].ToString();
String passportno = passporttextBox.Text;
String phoneno = phonenotextBox.Text;
myAccessCommand = new OleDbCommand(insert,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
**myAccessCommand.ExecuteNonQuery();**
}
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
}
myAccessCommand.ExecuteNonQuery();and look at themyAccessCommand.CommandText. You'll notice that it's not a valid SQL syntax. And that's the problem. You've set the first half of the INSERT where you define the columns, then you started to define the VALUES but you never actually do anything with all those string variables in yourforloop. Also, general convention is to usestringfor variables andStringwhen utilizing the actual class. Such asString.IsNullOrWhiteSpace(). And in the future, include whatex.Messagesays in your post.