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!

Updating DataSet using InsertCommand property of the DataAdapter

Status
Not open for further replies.

raabbasi

Technical User
Jun 21, 2005
52
PK
I retrieved the following function from VB.NET help and added a few statements for updating the datasource. But, it does not actually commence the InsertCommand property of the DataAdapter in order to physically insert a row into the DataSet. Please help!
----------------------------------------------------------
Public Function CreateCustomerAdapter(ByVal conn As OleDbConnection) As OleDbDataAdapter

Dim da As OleDbDataAdapter = New OleDbDataAdapter
Dim cmd As OleDbCommand

cmd = New OleDbCommand("SELECT * FROM Customers " & _
"WHERE Country = @Country AND City = @City", conn)

cmd.Parameters.Add("@Country", OleDbType.VarChar, 15).Value = "France"

cmd.Parameters.Add("@City", OleDbType.VarChar, 15).Value = "Paris"

da.SelectCommand = cmd
'---------------------------------------------------
Dim DS As DataSet = New DataSet
da.Fill(DS, "Customers")
DataGrid1.DataSource = DS.Tables("Customers")
Dim CB As OleDbCommandBuilder = New OleDbCommandBuilder(da)
'---------------------------------------------------
cmd = New OleDbCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", conn)

cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID").Value = "Prime"

cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName").Value = "Prime IT Solutions"

da.InsertCommand = cmd

Dim dt As New DataTable("Customers")
Dim NumRows As Long = da.Update(dt)
MessageBox.Show(NumRows & " Row(s) have been Inserted.", "Inserting Rows Successful.")

Return da

End Function
'---------------------------------------------------
Thanks in anticipation and regards.

R. A. Abbasi
 
Your DataTable, dt, is empty at the point of insert, and does not have any Added rows to insert.

Hope this helps,
Andrea
 
Thanks Andrea for the reply.
I want the concept cleared. I had added the table "dt" in an attempt to meet the Update rquirement of a DataTable. My confusion is that do not the following lines of code fill the table:

....
Dim DS As DataSet = New DataSet
da.Fill(DS, "Customers")
....
da.InsertCommand = cmd
....
How can I Update "Customers", included in the Fill method.

Regards
R. A. Abbasi
 
If you want to use that datatable, use
Code:
da.Update(DS.Tables("Customers"))

You should also change the parameters to
Code:
cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")
cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top