How do I programatically add a row and update the database?

  • Thread starter Thread starter Martin Horn
  • Start date Start date
M

Martin Horn

Hi all,

I am using VB2005 and the new data controls, which all work fine, except
there is a situation where I want to add a new row to the database without
using the 'AddNewItem' button on the BindingNavigator control; it has to be
done programatically.

This is the best I can come up with but it fails on line 3 with error
'Objects added to a BindingSource's list must all be of the same type.'

Dim dt As New MainDataSet.InvoicesDataTable
Dim dr As MainDataSet.InvoicesRow = dt.NewInvoicesRow
InvoicesBindingSource.Add(dr) <Error here>
' Set field values here
dr.field1 = 1
dr.field2 = ...etc
' Save the new row to the database
InvoicesBindingSource.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)

Any advice will be appreciated.

Thanks very much,

Martin Horn.
 
Martin,

This is mostly because the datarow does not have a primary key in the way
you do it..

However, there is no need to add it direct after creating. You can first
fill it and add it than.

There are very much methods to add a row, therefore I take the most standard
one (not forever the best to use)
\\\
dim dr as new datarow = dt.newrow
dr(0) = 1
dr(1) = "mytext"
dt.rows.add(dr)
///

I hope this helps,

Cor
 
Hi,

Martin Horn said:
Hi all,

I am using VB2005 and the new data controls, which all work fine, except
there is a situation where I want to add a new row to the database without
using the 'AddNewItem' button on the BindingNavigator control; it has to
be done programatically.

This is the best I can come up with but it fails on line 3 with error
'Objects added to a BindingSource's list must all be of the same type.'

Dim dt As New MainDataSet.InvoicesDataTable
Dim dr As MainDataSet.InvoicesRow = dt.NewInvoicesRow
InvoicesBindingSource.Add(dr) <Error here>
' Set field values here
dr.field1 = 1
dr.field2 = ...etc
' Save the new row to the database
InvoicesBindingSource.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)

Like Cor suggest you can add the new DataRow directly to the underlying
DataTable. The new record will be added to the UI, but the BindingSource
will not position to the new record:

Dim row As MainDataSet.InvoicesRow = _
MainDataSet.Invoices.NewInvoicesRow()

row.field1 = ...
row.field2 = ...

MainDataSet.Invoices.Rows.Add(row)

InvoicesTableAdapter.Update(MainDataSet.Invoices)

--Or you can add a new row using the BindingSource and a DataRowView, this
way the BindingSource will position to the new record:

Dim drv As DataRowView = DirectCast( _
InvoicesBindingSource.AddNew(), _
DataRowView)

drv("field1") = ...
drv("field2") = ...

drv.EndEdit()

InvoicesTableAdapter.Update(MainDataSet.Invoices)


HTH,
Greetings
 
Thanks Bart and Cor that did the trick,

I have used your suggestion with a slight alteration that enables me to make
use of the strong typing of the InvoicesDataRow object.

Dim drv As DataRowView = DirectCast(_
InvoicesBindingSource.AddNew(), _
DataRowView)
Dim dr As MainDataSet.InvoicesRow = DirectCast(drv.Row,
MainDataSet.InvoicesRow)
dr.CustID = 4
dr.InvoiceDate = Date.Today
drv.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)

Thanks once again, this is exactly what I was trying to achieve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top