When inserting a new row, technically it is not required in all cases but it is good practice to always include a field list as well as a value list, e.g.
Code:INSERT INTO Table1 (field1, field2, field3) VALUES (value1, value2, value3)
Otherwise it is just assumed that the first value goes in the first column, the second value does in the second column, etc. This method can be error-prone, so it is better to be explicit to be sure you are inserting the correct value into the correct column.
I highly recommend using the String.Format method when building an SQL statement like this. It is no more correct than the way you are doing it but it makes your code more readable, in my opinion.
Code:SQLstr = String.Format("INSERT INTO TableTestEmployes VALUES('{0}','{1}','{2}','{3}')", TextBox1.Text, TextBox2.Text, TextBox3.Text, ComboBox1.Text)
The way you are using the Catch block is not necessarily wrong. You should put a call to OleDbConnection.Open in a Try block and you probably don't want to give the user too technical a message if it fails. You do want to get the technical message yourself while you're debugging though. You might combine what you are already doing and what Rollershade suggests by using conditional compilation, which many people are unaware that VB.NET supports. Also, you should close the connection in a Finally block, so that it gets done whether an exception is thrown or not. If execution simply continues after the Try...Catch block anyway, the Finally block is unnecessary and you can just close the connection after the End Try. The finally is only needed if one of either the Try or Catch block exits the current method.
Code: Catch ex As Exception
If DEBUG Then
'Will be compiled into a debug build.
MessageBox.Show(ex.ToString())
Else
'Will be compiled into a release build.
MessageBox.Show("The new row could not be inserted.")
End If
Finally
Mycn.Close()
End Try
Enter your message below
Sign in or Join us (it's free).