1
Answer

A problem with inserting a row

Ray Brand

Ray Brand

9y
681
1
Hi and Thank you so much for your great tutorials. Yours was the only one that worked and I remotely understood.
I have a problem though I am not sure how to resolve.

I have a table as follows... (Access 2010) The table is called Item

ItemNo, PickUpDate, DeliveryDate, Source, Destination, ItemID

ItemNo,  Primary Key
PickUpDate, Date
DeliveryDate, Date
Source, Integer
Destination, Integer
ItemID Integer

I have adapted your basic database browser in that it works for browsing existing records, however when I try to insert I get the error...

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error (missing operator) in query expression '' 22/01/2015 12:00:00 AM,' 22/01/2015 12:00:00 AM'.

Here is your modified method I am using....

  private void buttonInsert_Click(object sender, EventArgs e)
  {
  // These are the textboxes I am using  
  //textBoxPickUpDate.Text = ds.Tables[0].Rows[rno][1].ToString();
  //textBoxDeliverDate.Text = ds.Tables[0].Rows[rno][2].ToString();
  //textBoxSource.Text = ds.Tables[0].Rows[rno][3].ToString();
  //textBoxDestination.Text = ds.Tables[0].Rows[rno][4].ToString();
  //textBoxItemID.Text = ds.Tables[0].Rows[rno][5].ToString();
  // There is another column (col 0) in the datagridview that holds the 
// primary key 
// which autonumbers when in access
 
  cmd = new OleDbCommand("insert into Item values(" + "5" + ",' " +  textBoxPickUpDate.Text + ",' " + textBoxDeliverDate.Text + ",' " + textBoxSource.Text + ",' " + textBoxDestination.Text + " ',' " + textBoxItemID.Text + " ')", con);
  con.Open();
  int n = cmd.ExecuteNonQuery();
  con.Close();

  if (n > 0)
  {
  MessageBox.Show("record inserted");
  loaddata();
  }
  else
  MessageBox.Show("insertion failed");
  }


I hard coded the "5" in as the first column is the primary key and has only 4 records, I dont know how to insert the modified details and auto increment the primary key.

Any Help would be appreciated.
Answers (1)