473,657 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving data in a datatable

I'm new to ADO.NET--trying to make the switch from ADO, which I've been using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New SqlConnection(S QLConnectionStr ing)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE MR_StudentNumbe r = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now 'CreatedBy
End If
NewRecord.Rows( 0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Accep tChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes don't persist. How can I update the database with my changes?
Nov 29 '07 #1
7 1515
Kevin,

The point is here the dataadapter, that has to have commands, that includes
the working of the update, delete and insert as is used by SQL. If you have
made the dataadapter in any generic way, then the problem is probably alone
in your acceptchanges.

It means something as: "set the rows in the dataadapter updated in the
database and accept the changes as done correct". Therefore those rows will
never been updated in your code. (The dataadapter does this automaticly for
you so there is not really need for that).

Cor

"Kevin" <kmahoney@nospa m_fireacademy.o rgschreef in bericht
news:kb******** *************** *********@4ax.c om...
I'm new to ADO.NET--trying to make the switch from ADO, which I've been
using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New
SqlConnection(S QLConnectionStr ing)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumbe r = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now
'CreatedBy
End If
NewRecord.Rows( 0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Accep tChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes
don't persist. How can I update the database with my changes?
Nov 29 '07 #2
So, what does that mean in terms of code? Am I missing something or coding something incorrectly?
On Thu, 29 Nov 2007 22:00:19 +0100, "Cor Ligthert[MVP]" <no************ @planet.nlwrote :
>Kevin,

The point is here the dataadapter, that has to have commands, that includes
the working of the update, delete and insert as is used by SQL. If you have
made the dataadapter in any generic way, then the problem is probably alone
in your acceptchanges.

It means something as: "set the rows in the dataadapter updated in the
database and accept the changes as done correct". Therefore those rows will
never been updated in your code. (The dataadapter does this automaticly for
you so there is not really need for that).

Cor

"Kevin" <kmahoney@nospa m_fireacademy.o rgschreef in bericht
news:kb******* *************** **********@4ax. com...
>I'm new to ADO.NET--trying to make the switch from ADO, which I've been
using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New
SqlConnection( SQLConnectionSt ring)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumb er = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now
'CreatedBy
End If
NewRecord.Rows (0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Acce ptChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes
don't persist. How can I update the database with my changes?
Nov 29 '07 #3
Kevin,

Don't call the data table's AcceptChanges method before calling the data
adapter's Update method.

Kerry Moorman
"Kevin" wrote:
So, what does that mean in terms of code? Am I missing something or coding something incorrectly?
On Thu, 29 Nov 2007 22:00:19 +0100, "Cor Ligthert[MVP]" <no************ @planet.nlwrote :
Kevin,

The point is here the dataadapter, that has to have commands, that includes
the working of the update, delete and insert as is used by SQL. If you have
made the dataadapter in any generic way, then the problem is probably alone
in your acceptchanges.

It means something as: "set the rows in the dataadapter updated in the
database and accept the changes as done correct". Therefore those rows will
never been updated in your code. (The dataadapter does this automaticly for
you so there is not really need for that).

Cor

"Kevin" <kmahoney@nospa m_fireacademy.o rgschreef in bericht
news:kb******** *************** *********@4ax.c om...
I'm new to ADO.NET--trying to make the switch from ADO, which I've been
using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New
SqlConnection(S QLConnectionStr ing)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumbe r = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now
'CreatedBy
End If
NewRecord.Rows( 0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Accep tChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes
don't persist. How can I update the database with my changes?
Nov 29 '07 #4
Then it stops on the line 'dataAdapter.Up date(NewRecord) ' and I get an error:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

On Thu, 29 Nov 2007 15:36:00 -0800, Kerry Moorman <Ke**********@d iscussions.micr osoft.comwrote:
>Kevin,

Don't call the data table's AcceptChanges method before calling the data
adapter's Update method.

Kerry Moorman
"Kevin" wrote:
>So, what does that mean in terms of code? Am I missing something or coding something incorrectly?
On Thu, 29 Nov 2007 22:00:19 +0100, "Cor Ligthert[MVP]" <no************ @planet.nlwrote :
>Kevin,

The point is here the dataadapter, that has to have commands, that includes
the working of the update, delete and insert as is used by SQL. If you have
made the dataadapter in any generic way, then the problem is probably alone
in your acceptchanges.

It means something as: "set the rows in the dataadapter updated in the
database and accept the changes as done correct". Therefore those rows will
never been updated in your code. (The dataadapter does this automaticly for
you so there is not really need for that).

Cor

"Kevin" <kmahoney@nospa m_fireacademy.o rgschreef in bericht
news:kb******* *************** **********@4ax. com...
I'm new to ADO.NET--trying to make the switch from ADO, which I've been
using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New
SqlConnection( SQLConnectionSt ring)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumb er = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now
'CreatedBy
End If
NewRecord.Rows (0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Acce ptChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes
don't persist. How can I update the database with my changes?
Nov 30 '07 #5
Kevin,

The data adapter needs a valid UpdateCommand, InsertCommand and
DeleteCommand, in addition to the SelectCommand that you provided.

You can either provide the commands yourself or use a SQLCommandBuild er,
which will provide the commands for you, assuming a very simple situation.

If you look up SQLDataAdapter in the help system and then look up the data
adapter's UpdateCommand property you will see examples of how to do this.

Kerry Moorman
"Kevin" wrote:
Then it stops on the line 'dataAdapter.Up date(NewRecord) ' and I get an error:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

On Thu, 29 Nov 2007 15:36:00 -0800, Kerry Moorman <Ke**********@d iscussions.micr osoft.comwrote:
Kevin,

Don't call the data table's AcceptChanges method before calling the data
adapter's Update method.

Kerry Moorman
"Kevin" wrote:
So, what does that mean in terms of code? Am I missing something or coding something incorrectly?
On Thu, 29 Nov 2007 22:00:19 +0100, "Cor Ligthert[MVP]" <no************ @planet.nlwrote :

Kevin,

The point is here the dataadapter, that has to have commands, that includes
the working of the update, delete and insert as is used by SQL. If you have
made the dataadapter in any generic way, then the problem is probably alone
in your acceptchanges.

It means something as: "set the rows in the dataadapter updated in the
database and accept the changes as done correct". Therefore those rows will
never been updated in your code. (The dataadapter does this automaticly for
you so there is not really need for that).

Cor

"Kevin" <kmahoney@nospa m_fireacademy.o rgschreef in bericht
news:kb******** *************** *********@4ax.c om...
I'm new to ADO.NET--trying to make the switch from ADO, which I've been
using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New
SqlConnection(S QLConnectionStr ing)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumbe r = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now
'CreatedBy
End If
NewRecord.Rows( 0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Accep tChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes
don't persist. How can I update the database with my changes?
Nov 30 '07 #6
Kevin,

Beside the commandbuilder you can use the generic tools in VS, a pity is,
that every version has his own so first tell us what version you are using.

To make the update, insert and delete with hand is not easy, for the most
part it is SQL script. (Now you see maybe the need for the LINk to sQl)
there shall be made an other meaning for the characters, however in practise
it is this.

LINQ is not using datasets by the way, it uses DBML which are an other kind
of classes, however as most things, based on the same principles done in
another way.

Cor

Nov 30 '07 #7
I solved the problem myself by adding the following line:

Dim CmdBuilder As New SqlCommandBuild er = New SqlCommandBuild er (dataAdapter)
On Thu, 29 Nov 2007 14:29:37 -0500, Kevin <kmahoney@nospa m_fireacademy.o rgwrote:
>I'm new to ADO.NET--trying to make the switch from ADO, which I've been using in my VB2005 apps up until now. Here's what I have:
Sub Save_Record()
Dim OldRecord as DataTable
Dim NewRecord as DataTable

Using testConnection As SqlConnection = New SqlConnection(S QLConnectionStr ing)
Dim testCommand As SqlCommand = testConnection. CreateCommand()
testCommand.Com mandText = "SELECT * FROM MasterRoster WHERE MR_StudentNumbe r = '" & txtStudentNumbe r.Text & "'"
Dim dataAdapter As New SqlDataAdapter( testCommand)
dataAdapter.Fil l(OldRecord)
dataAdapter.Fil l(NewRecord)

If NewRecord.Rows. Count = 0 Then
NewRecord.Rows. Add()
NewRecord.Rows( 0).Item(53) = sysUser & " " & Now 'CreatedBy
End If
NewRecord.Rows( 0).Item(0) = txtLName.Text
NewRecord.Rows( 0).Item(1) = txtFName.Text
NewRecord.Rows( 0).Item(2) = txtMI.Text

NewRecord.Accep tChanges()
dataAdapter.Upd ate(NewRecord)
'Code to compare OldRecord to NewRecord cut from here

End Using
End Sub
Comparing the two datatables shows the changes I made, but the changes don't persist. How can I update the database with my changes?
Nov 30 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10812
by: John Kandell | last post by:
Hi, I posted this in the asp.net group, but didn't get a response. Maybe someone here can help me with this... --- Would someone be able to shed some light on what is the cost of saving a DataTable to session vs saving a custom object of the same data.
1
2992
by: Steven Thomas | last post by:
Can some one help me find a code snipit for saving the contents of a data set to a comma seperated file? I have created a web service in asp.net that will read the data from a AS/400 DB2 database via a OleDB connection. Then I create the dataset, Now what do I do Here is the function now ------------------------------------------------ Public Function GetTicketData() As DataSet 'Public Function GetTicketData(ByVal varbpno, ByVal
2
3186
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp The problem I am having is that the data in the custom datagridcolumn is not saved to viewstate and after postback, the column does not contain data.
5
5815
by: Mark | last post by:
Hi - I've a data table that I want to save to an external Access database. I'm interested in sample code that would perform this save. Thanks, Mark (VB.NET, .Net framework 1.0)
1
1701
by: Jim Andersen | last post by:
Hi, I want to make a backup of some data from an SQL server. The backup should be saved as an Excell file (or similar, csv or xml...) So I create a datatable and select the data from SQL server and manipulate it a bit. No problem. But how do I save it on the Client PC ? Do I have to go through an intermediary step of saving it on the server and then transferring the file (somehow) and deleting the file on the server.
6
2180
by: arun.hallan | last post by:
Hi, I have a datagrid whose datasource is a datatable which is saved to viewstate on page_load. I also have a cached string which handles what is shown on the rowfilter of the dataview of this datatable. I also have a cached string that holds the way the dataview is sorted.
1
2595
by: Magnus | last post by:
I'm testing walkthrough saving data to a Database (Multiple Tables). http://msdn2.microsoft.com/en-us/library/4esb49b4(VS.80).aspx In the famous Customer/Order example, I'm getting referential integrity problems "The INSERT statement conflicted with the FOREIGN KEY constraint..." if the identity/primary key column is "out of sync" with the identity value in the database. I can easily acheive this f.ex. by pressing add new record,...
0
1542
by: vacuit | last post by:
Visual Basic 2005 I want to save a copy of a datatable (that the user has changed) with a new name back to the database (Access) I have this code to save a copy with a new name to the dataset...how do I then update the database (without changing the original datatable)? Essentially I want to add a new datatable to the database. Dim dt As New DataTable dt = MyDataSet.OriginalDataTable.Copy dt.TableName = mytextbox.text...
5
3590
by: Lucvdv | last post by:
This would better be described by 'serialization' than 'interop', but I didn't find a newsgroup that seems closer on topic. The problem in a few words: I save data with DataSet.WriteXML, but I get different data back when I read it later with DataSet.ReadXml. More detail:
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.