Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with using InsertCommand property of the DataAdapter

Status
Not open for further replies.

raabbasi

Technical User
Jun 21, 2005
52
PK
The following procedure (refined from the previous one) still does not actually commence the InsertCommand property to physically insert a row into the DataSet.
'---------------------------------------------------------
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnInsert.Click
Dim DS As DataSet = New DataSet
Dim dadapter As OleDbDataAdapter = New OleDbDataAdapter
Dim CmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(dadapter)
Dim strSelect = "SELECT * FROM Customers"
Dim cmdSelect As OleDbCommand = New OleDbCommand(strSelect, Conxn)

dadapter.SelectCommand = cmdSelect
dadapter.Fill(DS, "Customers")

Dim strInsert = "INSERT INTO Customers(CustomerID, CompanyName) " & _
"VALUES('Pak', 'Prime IT Solutions')"
Dim cmdInsert As OleDbCommand = New OleDbCommand(strInsert, Conxn)
dadapter.InsertCommand = cmdInsert

Try
Dim NumRows As Long = dadapter.Update(DS, "Customers")
MessageBox.Show(NumRows & " Row(s) have been Inserted.", "Rows Inserted Successfully.")
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error in Inserting a Row.")
End Try
End Sub
'---------------------------------------------------
Please suggest a solution. Regards.

R. A. Abbasi
 
replace

Code:
 Dim strInsert = "INSERT INTO Customers(CustomerID, CompanyName) " & _
                              "VALUES('Pak', 'Prime IT Solutions')"
        Dim cmdInsert As OleDbCommand = New OleDbCommand(strInsert, Conxn)
        dadapter.InsertCommand = cmdInsert

        Try
            Dim NumRows As Long = dadapter.Update(DS, "Customers")
            MessageBox.Show(NumRows & " Row(s) have been Inserted.", "Rows Inserted Successfully.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Error in Inserting a Row.")
        End Try

with

Code:
 Dim strInsert = "INSERT INTO Customers(CustomerID, CompanyName) " & _
                              "VALUES('Pak', 'Prime IT Solutions')"
        Dim cmdInsert As OleDbCommand = New OleDbCommand(strInsert, Conxn)
       
        Try
            Dim NumRows As Long = cmdinsert.executenonquery
            MessageBox.Show(NumRows & " Row(s) have been Inserted.", "Rows Inserted Successfully.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Error in Inserting a Row.")
        End Try

BTW this and/or your previous method won't update any changes you have made to the datatale they will just write 'pak' and 'prime it solutions' to a new record.
so you don't need the dataadapter nor the dataset.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Thank you very much Christiaan Baes for the response.
The ExecuteNonQuery method is quite straightforward.
As a matter of fact, I am interested in finding a solution with the Update method of the DataAdapter for an academic purpose and I believe, there will be a way to do it.
Further help will be highly appreciated.

Regards,
R. A. Abbasi
 
Unfortunately there is no help pertaining to this particular scenario.
A solution with the Update method of the DataAdapter (Not using the ExecuteNonQuery method of the Command object) will be highly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top