473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
3 19342
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tamimi | last post by:
I have a guestbook form that inserts the fields into an Access Database. The database is residing in the "fpdb" folder that was created by FrontPage. The ASP page that writes the data into the...
1
by: Fie Fie Niles | last post by:
I have IIS installed on XP Professional workstation machine. I have an ASP page that open connection to an Access database, then when trying to update the database, it gave me the error "cannot...
1
by: Darn | last post by:
Hi all How do i solve this problem. I'm web developer from Malaysia, the problem occurs when i ftp an Access database which i'm using for this particular web site for it's database.The form...
0
by: PaulB | last post by:
Hi there, Allthough I searched for previous posts, I was unable to solve my problem. I try to link my front-mdb with an Excel file using the next line DoCmd.TransferSpreadsheet acLink, 8,...
5
by: Leanne | last post by:
I am doing customization for Microsoft Pos. I am working on installation package now. I added some new tables and stored procuedures and generated SQL Script for that. During the installation...
0
by: tom | last post by:
When I try to read in a csv file it gives me this error message. 'Cannot update. Database or object is read-only.' If I change the extension to txt it processes just fine. I have googled all...
4
by: Rohith | last post by:
I m not able to update Database through DataGrid.I am working in VB.Net .I can retrieve the Data but could not update the data through datagrid.Please help me.
6
by: Tark Siala | last post by:
hi i'm using VS.NET 2005 +SP1 C#, and i connect access database by ADO.NET 2.0. i'm using Dataset to (insert,update,delete) and in get data i use (Datareader), but my problem when i do this: 1 -...
1
by: adsaca | last post by:
here's my code in filling a datagridview,which i retrieve from database mysql, dim sql as String sql = "SELECT * FROM table_user" Dim myData As New DataTable ...
1
by: sweetjos | last post by:
Thank you sir, I have another question.... Recently I got this error. Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) Cannot update. Database or object is read-only....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.