djohncon

djohncon

  • NA
  • 16
  • 0

Text Box Error when inserting data

Jan 18 2005 4:56 AM
Hi I am a newb to C#. the problem I have is if I insert into five fields in Windows Forms from a MS Access DB I receive an error syntax in insert code or missing operator. But if I i insert only the Primary key and one of the other four fields there is no error. Sample code of insertion block. try { if(t_fuelID.Text!=""&&t_fuelInvoice.Text!=""&&t_fuelLitres.Text!=""&&t_fuelKeyID.Text!=""&&t_fuelDate.Text!="") { string strConn="Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=Bburn_Fuel.mdb" ; OleDbConnection myConn = new OleDbConnection(strConn) ; myConn.Open(); //the string to get values from the textboxes and form an "INSERT INTO" // statement. string strInsert = "INSERT INTO Fuel2 (ID, Invoice, Litres, KeyID, fuelDate) VALUES ( "; strInsert += t_fuelID.Text+", ' "; strInsert += t_fuelInvoice.Text+", ' "; strInsert += t_fuelLitres.Text+",' "; strInsert += t_fuelKeyID.Text+",' "; strInsert += t_fuelDate.Text+")"; OleDbCommand inst = new OleDbCommand(strInsert,myConn) ; //Execute the statement inst.ExecuteNonQuery() ; statusBar.Text="Data Added to Database " ; //reset all the textboxes int i=int.Parse(t_fuelID.Text); i++; t_fuelID.Text=i.ToString() ; t_fuelInvoice.Text="" ; t_fuelLitres.Text= ""; t_fuelKeyID.Text="" ; t_fuelDate.Text = ""; statusBar.Text="Connected - Now you can Add records"; myConn.Close() ; } else { MessageBox.Show("All fields must be completed.", "Error"); } } catch(Exception ed) { MessageBox.Show("Error in Saving "+ed.ToString(), "Error"); } } /// /// This method is called when the help button is clicked. /// protected void helpClick(object sender, System.EventArgs e) { MessageBox.Show("Bburn Fuel - Data Addtion program ver01 ", "About ..."); } Has anyone had this problem? I hope someone can help? kamean

Answers (1)