Member Avatar for b1izzard

Hi all I am trying to insert a record into MS-Access database using VS2008 as front end and this is my code i am using to connect to the database.
login.mdb has two tables(login,schedules)

Dim con As New OleDb.OleDbConnection
        Dim cmd As OleDbCommand
        Dim myquery As String
        Dim count As Integer
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\login.mdb"
        con.Open()
        Try
           
            myquery = "INSERT INTO schedules VALUES('" & (txtcompanyname.Text) & _
            "','" & (rtaddress.Text) & _
            "','" & (txtcontactperson.Text) & _
            "','" & CInt(txtphoneno.Text) & _
            "','" & (txtemailid.Text) & _
            "','" & (rtabout.Text) & _
            "','" & (txtwebsite.Text) & _
            "','" & (comboindustry.SelectedItem) & _
            "','" & (combojobcategory.SelectedItem) & _
            "','" & (txtvacancy.Text) & _
            "','" & (combosalaryrange.SelectedItem) & _
            "','" & (combotype.SelectedItem) & _
            "','" & (DateTimePicker1.Value) & _
            "','" & (combotime.SelectedValue) & _
            "','" & (txtvenue.Text) & _
            "','" & (rtotherinfo.Text) & "')"

            cmd = New OleDbCommand(myquery, con)
            count = cmd.ExecuteNonQuery()
            MsgBox(" Number of Records inserted :  " & count)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
        con.Close()

Msgbox displays (Number of Records inserted:1) but nothing gets inserted!

I want to use company name+date field as a primary key for inserting or updating a record little help needed here.

use the System.Transaction class with your connection.
for commit the transaction and rollback transaction.

Dim trans As SqlTransaction
            trans = con.BeginTransaction
            Try
                 
                cmd = New SqlCommand("insert query", con, trans) ''con is a connection
                cmd.ExecuteNonQuery
                trans.Commit()
                 MsgBox(" Number of Records inserted :  " & count)
            Catch ex As Exception
                trans.Rollback()
                 MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
            End Try
            con.Close()
            trans.Dispose()
            cmd = Nothing

sorry i had given the example of sql query try with OleDb it will work.

Member Avatar for b1izzard

@pritesh: Thanks for ur help now the code works fine

use the System.Transaction class with your connection.
for commit the transaction and rollback transaction.

Dim trans As SqlTransaction
            trans = con.BeginTransaction
            Try
                 
                cmd = New SqlCommand("insert query", con, trans) ''con is a connection
                cmd.ExecuteNonQuery
                trans.Commit()
                 MsgBox(" Number of Records inserted :  " & count)
            Catch ex As Exception
                trans.Rollback()
                 MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
            End Try
            con.Close()
            trans.Dispose()
            cmd = Nothing

i've try this,but it didn't work for me...I've the same problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.