Inserting New Records in Database

sibdrive

New member
Joined
Nov 1, 2006
Messages
4
Programming Experience
Beginner
Hi all!. I am having a problem when trying to insert a new row in the database. No Errors are showing up but when i check the MS-Access database, the new row is not there. Iam reading the contents from 3 controls as shown in code below:
VB.NET:
If con.State = ConnectionState.Closed Then
                con.Open()
            End If
dim insert as string
Try                                                                                                                                                                                    
                insert = _
                "INSERT INTO MeetingsDate(Dates, EntryType, Category) VALUES('" & DateTimePicker1.Text & "','" _
                & cboDetailsType.Text & "','" & cboMeetingType.Text & "')"
                saveData() 'Some function to write details to a text file
                Dim Cmd As New OleDbCommand(insert, con)
                
                Cmd.ExecuteNonQuery()
                MessageBox.Show("Your entry has been succesfully saved...", "Entry Saved", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch
                MsgBox(Err.Description & " " & Err.Source)
            End Try
            con.Close()
Please, can some one tell me what's wrong with my code or is there any other way of inserting records in a table? Thanks very much in advance.
 
Parameterised queries are the way to go here, and assuming that my .net 2003 is ok still. This is the way to do it..

VB.NET:
dim insert as string
Try                                                                                                                                                                                    
insert  =  _
"INSERT INTO MeetingsDate(Dates, EntryType, Category), VALUES (?, ?, ?)"

Cmd.CommandText = insert
cmd.Parameters.Add("@Dates", OleDbType.Date, 0 , "Dates"). Value = CDate(DateTimePicker1.Text)
cmd.Parameters.Add("@EntryType", OledbType.VarWChar, 255, "EntryType").Value = cboDetailsType.Text
cmd.parameters.Add("@Category", OledbType.VarWChar, 255, "Category").Value = cboMeetingType.Text


If con.State = ConnectionState.Closed Then
                con.Open()
            End If

                Cmd.ExecuteNonQuery()
                MessageBox.Show("Your entry has been succesfully saved...", "Entry Saved", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch
                MsgBox(Err.Description & " " & Err.Source)

Finally
con.Close()
            End Try

It's been a while since i've used 2003 but give that a go.
 
Thanks for the quick response...actually i am using vb 2005 and didn't realize that my profile says i am using vb2003...i have just updated that. Nevertheless, i'll give it a try and see what happens.
 
Back
Top