By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Dec 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Dec 23 '05 #2

P: n/a
Hi,

"Martin Horn" <mv****@theinternet.com> wrote in message
news:Df******************@newsfe2-gui.ntli.net...
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

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.

Dec 23 '05 #3

P: n/a
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.
"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:uo****************@TK2MSFTNGP10.phx.gbl...
Hi,

"Martin Horn" <mv****@theinternet.com> wrote in message
news:Df******************@newsfe2-gui.ntli.net...
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

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.


Dec 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.