Insert new record in to access database

pjs

New member
Joined
Sep 18, 2008
Messages
3
Programming Experience
Beginner
this is my code which i try to insert records but gives an error ...i hav a form with text box,combobox and datetime picker


VB.NET:
Private Sub btnadd_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

        Dim ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=sample.mdb"
        

        Dim sqlinsert As String

        cmdadp = New OleDbDataAdapter(ConnectionString, cnnOLEDB)

        


        
       [B]sqlinsert = "Insert into sample values(" & txtno.Text & ",'" & dtdate1.Value.ToShortDateString & "','" & cbbdiv.SelectedValue() & "','" & cbbpersonnel.SelectedValue() & "', '" & cbbnames.SelectedValue() & "'," & txtdetails.Text & "," & txtreports.Text & "," & txtremarks.Text & "," & txtadvice.Text & "," & txtrmksales.Text & ", " & txtrfq.Text & ",'" & dtdate.Value.ToShortDateString & "','" & cbbstatus.SelectedValue() & "')"[/B]       

cnnOLEDB = New OleDb.OleDbConnection(ConnectionString)


        cmdOLEDB = New OleDbCommand(sqlinsert, cnnOLEDB)

        cnnOLEDB.Open()

       

        cmdOLEDB.CommandText = sqlinsert


        cmdOLEDB.Connection = cnnOLEDB


        cmdOLEDB.ExecuteNonQuery()
        ra = cmdOLEDB.ExecuteNonQuery()
        MessageBox.Show("New Row Inserted" & ra)
        ConnectionString.Close()
    End Sub
my acess db format is

enq no is an auto number and

enqdate ,due date is date time of short date others are text plz do help

error is syntax error in insert into
 
Last edited by a moderator:
I really do want to help you with this so i'm going to suggest that you read a few articles on the net about ADO.NET. The reason being fixing the code you have there simply wouldn't help you do it better next time.

Look into

Parameterised Queries.


Also the combobox SelectedValue is a property not a method so the parenthesis are unnecessary.

Additionally you have already set the connection object in the constructor of the OleDBCommand you don't need this bit...

VB.NET:
cmdOLEDB.Connection = cnnOLEDB
 
thanx for u'r reply again same error is coming kindly guide me vit code examples as i'm new to .net
 
Back
Top