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

DataSet Not Updating

Hi,

I am trying to insert a row into the Categories Table in
Nothwinds database in MSDE. I am able to manually insert
it using a query tool.

Using the Debugger, it appears the DataSet loads
correctly, the Data Row is populated correctly, and the
new row is added to the DataSet. But the database is not
updated. What am I missing?

Thanks in advance,

Tom

======

Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection
("Data Source=COL-TP5170-LT1\VSdotNET;Integrated
Security=SSPI;Initial Catalog=Northwind;")
Dim nwindDA As SqlDataAdapter = New SqlDataAdapter
("SELECT CategoryID, CategoryName, Description FROM
Categories", nwindConn)
nwindConn.Open()

Dim SqlInsertCommand1 As SqlCommand = New SqlCommand()

SqlInsertCommand1.CommandText = "INSERT INTO Categories
(CategoryName, Description) VALUES (@CategoryName,
@Description)"
SqlInsertCommand1.Connection = nwindConn
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@CategoryName" ,
System.Data.SqlDbType.NVarChar, 15, "CategoryName"))
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Description",
System.Data.SqlDbType.NVarChar, 1073741823, "Description"))

Dim nwindDS As DataSet = New DataSet()
nwindDA.Fill(nwindDS, "Categories")

Dim dtTable As DataTable
Dim drRow As DataRow

dtTable = nwindDS.Tables("Categories")
drRow = dtTable.NewRow()
drRow("CategoryName") = "New Category Name"
drRow("Description") = "New Description"
dtTable.Rows.Add(drRow)

nwindDS.AcceptChanges()
nwindDA.Update(nwindDS, "Categories")
nwindConn.Close()
End Sub

Nov 20 '05 #1
11 8380
Cor
Hi Tom

Try this to change it as this (I did not see all of your code but this is
defenatly wrong)
nwindDA.Update(nwindDS, "Categories")
nwindDS.AcceptChanges()


The "accept changes" is one of the most misunderstanded methods I think.
It set all the rowstates in the dataset to updated.

I hope this helps,

cor


Nov 20 '05 #2
Tomp,
dtTable.Rows.Add(drRow)
nwindDS.AcceptChanges()
nwindDA.Update(nwindDS, "Categories")
In addition to the Cor's comments.

Calling "dtTable.Rows.Add(drRow)" marks the row as DataRowState.Added.
Calling DataSet, DataTable, DataRow AcceptChanges marks the respective rows
as processed (DataRowState.UnChanged).

DataAdapter.Update only processes rows that have DataRow.RowState of
DataRowState.Added, DataRowState.Deleted, or DataRowState.Modified, as that
is how it knows to use the InsertCommand, DeleteCommand, or UpdateCommand
respectively.

Hence calling Rows.Add followed by AcceptChanges causes nothing to
occur on your database.

Note: DataAdapter.Update will itself call DataRow.AcceptChanges on each row
that it processes, so you don't need to call AcceptChanges after calling
DataAdapter.Update either.

David Sceppa's book "Microsoft ADO.NET - Core Reference" from MS Press
covers this plus a plethora of other items about ADO.NET. I highly recommend
it as a good tutorial for ADO.NET & a good desk reference once you know
ADO.NET.

Hope this helps
Jay

"tomp" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl... Hi,

I am trying to insert a row into the Categories Table in
Nothwinds database in MSDE. I am able to manually insert
it using a query tool.

Using the Debugger, it appears the DataSet loads
correctly, the Data Row is populated correctly, and the
new row is added to the DataSet. But the database is not
updated. What am I missing?

Thanks in advance,

Tom

======

Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection
("Data Source=COL-TP5170-LT1\VSdotNET;Integrated
Security=SSPI;Initial Catalog=Northwind;")
Dim nwindDA As SqlDataAdapter = New SqlDataAdapter
("SELECT CategoryID, CategoryName, Description FROM
Categories", nwindConn)
nwindConn.Open()

Dim SqlInsertCommand1 As SqlCommand = New SqlCommand()

SqlInsertCommand1.CommandText = "INSERT INTO Categories
(CategoryName, Description) VALUES (@CategoryName,
@Description)"
SqlInsertCommand1.Connection = nwindConn
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@CategoryName" ,
System.Data.SqlDbType.NVarChar, 15, "CategoryName"))
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Description",
System.Data.SqlDbType.NVarChar, 1073741823, "Description"))

Dim nwindDS As DataSet = New DataSet()
nwindDA.Fill(nwindDS, "Categories")

Dim dtTable As DataTable
Dim drRow As DataRow

dtTable = nwindDS.Tables("Categories")
drRow = dtTable.NewRow()
drRow("CategoryName") = "New Category Name"
drRow("Description") = "New Description"
dtTable.Rows.Add(drRow)

nwindDS.AcceptChanges()
nwindDA.Update(nwindDS, "Categories")
nwindConn.Close()
End Sub

Nov 20 '05 #3
Cor
Hi Jay B,

Of course I can be wrong, but I think you mix some thing up here.

Note: DataAdapter.Update will itself call DataRow.AcceptChanges on each row that it processes, so you don't need to call AcceptChanges after calling
DataAdapter.Update either.


My expirience is else I and think you are mixing it up with the concurrency
states.

You know that I did tried it three times before I wrote this, but still I
can be wrong.

Cor
Nov 20 '05 #4
Cor,
See:
http://msdn.microsoft.com/library/de...pdatetopic.asp

The section close to the middle that starts "When using Update, the order of
execution is as follows".

Sceppa's book explains it more detail then the above statement.

Hope this helps
Jay

"Cor" <no*@non.com> wrote in message
news:eQ*************@TK2MSFTNGP10.phx.gbl...
Hi Jay B,

Of course I can be wrong, but I think you mix some thing up here.

Note: DataAdapter.Update will itself call DataRow.AcceptChanges on each row
that it processes, so you don't need to call AcceptChanges after calling
DataAdapter.Update either.


My expirience is else I and think you are mixing it up with the

concurrency states.

You know that I did tried it three times before I wrote this, but still I
can be wrong.

Cor

Nov 20 '05 #5
Cor
Hi Jay B,

I could not find that text. (I even have searched for "using" on that page)

Did you send maybe the wrong link?

Here is another page
http://msdn.microsoft.com/library/de...terdataset.asp
Does not write what you say, but what I think you mix up.

But still I can be wrong.

Cor
Cor,
See:
http://msdn.microsoft.com/library/de...pdatetopic.asp
The section close to the middle that starts "When using Update, the order of execution is as follows".

Sceppa's book explains it more detail then the above statement.

Hope this helps
Jay

Nov 20 '05 #6
Cor,
Sorry wrong URL, I changed pages in the browser, but the url did not change,
try this page (the page immediately after the one I sent before).

http://msdn.microsoft.com/library/de...dateTopic1.asp
Correct that page does not explicitly state that Update calls AcceptChanges,
nor do I see where it states (on that page) that you need to call
AcceptChanges. The examples do not include AcceptChanges.

The only reference to AcceptChanges is in the note about what happens when
AcceptChanges is called.

Also I cannot think of a case where I would NOT want AcceptChanges called
when I called Update, there may be a case, however I cannot think of one...

Hope this helps
Jay

"Cor" <no*@non.com> wrote in message
news:eV****************@tk2msftngp13.phx.gbl...
Hi Jay B,

I could not find that text. (I even have searched for "using" on that page)
Did you send maybe the wrong link?

Here is another page
http://msdn.microsoft.com/library/de...terdataset.asp Does not write what you say, but what I think you mix up.

But still I can be wrong.

Cor
Cor,
See:

http://msdn.microsoft.com/library/de...pdatetopic.asp

The section close to the middle that starts "When using Update, the

order of
execution is as follows".

Sceppa's book explains it more detail then the above statement.

Hope this helps
Jay


Nov 20 '05 #7
Cor
Hi Jay B,

Works exactly as described there.

:-)

Cor
Nov 20 '05 #8

Jay and Cor,

Thanks for the help! I think I have made some progress by
removing the AcceptChanges command altogether (I was
confused in your messages if I should remove or change the
order of the AcceptChanges or the Update. I concluded
that removing it seemed the most logical). Now it appears
that it is at least trying to process an Insert.

But, I am getting the following error:

"An unhandled exception of
type 'System.InvalidOperationException' occurred in
system.data.dll Additional information: Update requires a
valid InsertCommand when passed DataRow collection with
new rows."

Do you see anything wrong with the Insert? I basically
copy and pasted generated code for this fragment. Thanks
again!

Tom

====
Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection
("Data Source=COL-TP5170-LT1\VSdotNET;Integrated
Security=SSPI;Initial Catalog=Northwind;")
Dim nwindDA As SqlDataAdapter = New SqlDataAdapter
("SELECT CategoryID, CategoryName, Description FROM
Categories", nwindConn)
nwindConn.Open()

Dim SqlInsertCommand1 As SqlCommand = New
SqlCommand()
SqlInsertCommand1.CommandText = "INSERT Categories
(CategoryName, Description) VALUES (@CategoryName," & _
" @Description); SELECT CategoryID, CategoryName,
Description, " & _
"FROM Categories WHERE (CategoryID = @@IDENTITY)"
SqlInsertCommand1.Connection = nwindConn
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@CategoryName" ,
System.Data.SqlDbType.NVarChar, 15, "CategoryName"))
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Description",
System.Data.SqlDbType.NVarChar, 1073741823, "Description"))
'
Dim nwindDS As DataSet = New DataSet()
nwindDA.Fill(nwindDS, "Categories")

Dim dtTable As DataTable
Dim drRow As DataRow
dtTable = nwindDS.Tables("Categories")
drRow = dtTable.NewRow()
drRow("CategoryName") = "New Category Name"
drRow("Description") = "New Description"
dtTable.Rows.Add(drRow)

nwindDA.Update(nwindDS, "Categories")
nwindConn.Close()
End Sub

Nov 20 '05 #9
Cor
Hi Tom,

Read it to the end before you become afraid

If you do it nice you have to make an Insert, an Update, and a Delete
command.

That you can do using the designer that makes it and then copy and paste it.

I never do that.

I was changing little things, but after all I have changed all your code,
not because it was fault, but that was easier to show, look to it as an
alternative.

I typed it here not in the IDE so there can be little errors but basicly it
is OK
\\\
Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection
("Data Source=COL-TP5170-LT1\VSdotNET;Integrated
Security=SSPI;Initial Catalog=Northwind;")
Dim nwindDA As SqlDataAdapter = New SqlDataAdapter
("SELECT CategoryID, CategoryName, Description FROM
Categories", nwindConn)
Dim nwindDS As DataSet = New DataSet()
nwindDA.Fill(nwindDS, "Categories")
dim drRow as datarow = nwindDS.tables("Categories").NewRow()
drRow("CategoryName") = "New Category Name"
drRow("Description") = "New Description"
nwindDS.tables("Categories").Add(drRow)
dim cmd as new SQLcommandbuilder(nwindDA)
if nwindDS.haschanges then
nwindDA.Update(nwindDS, "Categories")
end if
nwindCon.Close
end sub
///

I deleted the nwind.open (does the dataadapter) and changed some things to
add a row. Also added the commandbuilder, which makes all the commands from
the select string (but forget never the primary key column to have in the
select).

I have read in the newsgroups that people told that the commanbuilder is not
able to do real very complex select commands. (But I never use that, I bring
it into VB.net if it is complex, I hate SQL)

I added also the test for if the datase has changes (here always of course),
than it skips the update if that is not, little bit nicer.

I hope this helps?

Cor
Jay and Cor,

Thanks for the help! I think I have made some progress by
removing the AcceptChanges command altogether (I was
confused in your messages if I should remove or change the
order of the AcceptChanges or the Update. I concluded
that removing it seemed the most logical). Now it appears
that it is at least trying to process an Insert.

But, I am getting the following error:

"An unhandled exception of
type 'System.InvalidOperationException' occurred in
system.data.dll Additional information: Update requires a
valid InsertCommand when passed DataRow collection with
new rows."

Do you see anything wrong with the Insert? I basically
copy and pasted generated code for this fragment. Thanks
again!

Tom

====
Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection
("Data Source=COL-TP5170-LT1\VSdotNET;Integrated
Security=SSPI;Initial Catalog=Northwind;")
Dim nwindDA As SqlDataAdapter = New SqlDataAdapter
("SELECT CategoryID, CategoryName, Description FROM
Categories", nwindConn)
nwindConn.Open()

Dim SqlInsertCommand1 As SqlCommand = New
SqlCommand()
SqlInsertCommand1.CommandText = "INSERT Categories
(CategoryName, Description) VALUES (@CategoryName," & _
" @Description); SELECT CategoryID, CategoryName,
Description, " & _
"FROM Categories WHERE (CategoryID = @@IDENTITY)"
SqlInsertCommand1.Connection = nwindConn
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@CategoryName" ,
System.Data.SqlDbType.NVarChar, 15, "CategoryName"))
SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Description",
System.Data.SqlDbType.NVarChar, 1073741823, "Description"))
'
Dim nwindDS As DataSet = New DataSet()
nwindDA.Fill(nwindDS, "Categories")

Dim dtTable As DataTable
Dim drRow As DataRow
dtTable = nwindDS.Tables("Categories")
drRow = dtTable.NewRow()
drRow("CategoryName") = "New Category Name"
drRow("Description") = "New Description"
dtTable.Rows.Add(drRow)

nwindDA.Update(nwindDS, "Categories")
nwindConn.Close()
End Sub

Nov 20 '05 #10
Cor,

Thanks so much for taking the time to show me how to make
this work. I do appreciate the effort on your part. I
copied/pasted your code into my ide, made a few minor
syntax fixes and got the following error :

Unhandled Exception: System.Data.SqlClient.SqlException:
String or binary data would be truncated.
at System.Data.Common.DbDataAdapter.Update(DataRow[]
dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet
dataSet, String srcTable)
at WindowsApplication1.Module1.Main() in C:\Documents
and Settings\TPhalen\My Documents\Visual Studio
Projects\WindowsApplication1\Module1.vb:line 44The
program '[1816] WindowsApplication1.exe' has exited with
code 0 (0x0).

Any ideas?

Tom

Nov 20 '05 #11
Cor,

Nevermind. I overlooked the obvious. The data I was
trying to put in a column was more charaters than allowed
in that column.

Tom
Nov 20 '05 #12

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

Similar topics

4
by: Frnak McKenney | last post by:
I'm using an in-core DataSet as an image of my application's 'database' (a multi-table Access97 mdb file). Updates are made to the DataTables within the DataSet via forms with bound TextBoxes,...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
0
by: Vijay Balki | last post by:
I am fetching data in DataSet - myDataSet, from a remote database using a Web Service in my VB.NET client..Once I fetch it I store the data in XML file (myXMLFile) using the WriteXML method of the...
10
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1)...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
0
by: OldStd | last post by:
Updating data using 2 data sets I am having some problems in updating the database using two datasets as suggested by someone. 1. Data is displayed in a data grid from a dataset generated using...
2
by: Sheikko | last post by:
ADO.NET - Working with DataSet ========================= HI, I want to create an application that interact with a DB. I wont to use SQL statement, but I want to use DataSet. - I have created my...
1
by: jonbartlam | last post by:
Hi There I'm not sure what exactly is going wrong here. I'm writing an application that retreives a table from a database (tbl_internalfaults) and updates it. (Actually, just the status column will...
3
by: Gun Slinger | last post by:
Hi guys, I have run into a little problem once again in my program. I have struggled with this for a while looking around for answers, and have made a make shift method to do a work around. But...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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...

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.