473,396 Members | 1,945 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,396 software developers and data experts.

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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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 1498
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@nospam_fireacademy.orgschreef 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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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@nospam_fireacademy.orgschreef 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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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@nospam_fireacademy.orgschreef 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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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.Update(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**********@discussions.microsoft.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@nospam_fireacademy.orgschreef 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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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 SQLCommandBuilder,
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.Update(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**********@discussions.microsoft.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@nospam_fireacademy.orgschreef 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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE
MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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 SqlCommandBuilder = New SqlCommandBuilder (dataAdapter)
On Thu, 29 Nov 2007 14:29:37 -0500, Kevin <kmahoney@nospam_fireacademy.orgwrote:
>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(SQLConnectionString)
Dim testCommand As SqlCommand = testConnection.CreateCommand()
testCommand.CommandText = "SELECT * FROM MasterRoster WHERE MR_StudentNumber = '" & txtStudentNumber.Text & "'"
Dim dataAdapter As New SqlDataAdapter(testCommand)
dataAdapter.Fill(OldRecord)
dataAdapter.Fill(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.AcceptChanges()
dataAdapter.Update(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
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...
1
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...
2
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...
5
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
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...
6
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...
1
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...
0
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.