I need help

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

  • 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]

    : 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();
    : }
    :

  • When I compile this I get The type or namespace name 'OleDbCommand' could not be found (are you missing a using directive or an assembly reference?). Does anyone know how to fix this problem?











    : 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]
    :

  • The OleDbCommand class is defined in the System.Data.OleDb namespace. Either use the fully-qualified name (System.Data.OleDb.OleDbCommand) or add a using statement for System.Data.OleDb to the top of your code file.
  • Thank you!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories