I have written this code, and get no errors, but it doesn't insert the information into the database. I don't know why it doesn't work.Can any one help?
try
{
oleDbConnection1.Open();
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO rfiddb(Customer ID, Date/Time, Item ID)"+
"VALUES ('" +
txtID.Text + "', '" +
stBarTime.Text.Substring(10,20) + "', '" +
txtItem.Text + "')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
}
catch (System.Data.OleDb.OleDbException oleException)
{
Console.WriteLine( oleException.StackTrace);
}
finally
{
oleDbConnection1.Close();
}
Comments
[code]OleDbCommand command = new OleDbCommand(
"INSERT INTO rfiddb (Customer ID, Date/Time, Item ID) VALUES (?, ?, ?)", connection);"
command.Parameters.Add(txtID.Text);
command.Parameters.Add(stBarTime.Text.Substring(10,20));
command.Parameters.Add(txtItem.Text);
command.ExecuteNonQuery();[/code]
: I have written this code, and get no errors, but it doesn't insert the information into the database. I don't know why it doesn't work.Can any one help?
:
:
: try
: {
:
: oleDbConnection1.Open();
: oleDbDataAdapter1.InsertCommand.CommandText =
: "INSERT INTO rfiddb(Customer ID, Date/Time, Item ID)"+
: "VALUES ('" +
: txtID.Text + "', '" +
: stBarTime.Text.Substring(10,20) + "', '" +
: txtItem.Text + "')";
:
: oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
:
: }
: catch (System.Data.OleDb.OleDbException oleException)
: {
: Console.WriteLine( oleException.StackTrace);
: }
: finally
: {
: oleDbConnection1.Close();
: }
:
: You don't need to use a DataAdapter if your data is not coming from a DataSet. Also, string concatenation is not a good way to build a SQL statement. You should use parameterized queries instead.
: [code]OleDbCommand command = new OleDbCommand(
: "INSERT INTO rfiddb (Customer ID, Date/Time, Item ID) VALUES (?, ?, ?)", connection);"
:
: command.Parameters.Add(txtID.Text);
: command.Parameters.Add(stBarTime.Text.Substring(10,20));
: command.Parameters.Add(txtItem.Text);
:
: command.ExecuteNonQuery();[/code]
: