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

Row Update Not Updating Data source

I've got a procedure designed to modify the contents of a single row
in a data table. The code appears to work fine in that it compiles
and executes without error and the changes are reflected in the
dataset. However, when I close and re-open the app, the changes are
lost, which means that they are not reaching the datasource. Can
anybody see why this is happening? I've searched all over and I think
that this should work - but it obviously doesn't (and I'm kind of a
moron). Any help is appreciated.

Thanks.

Dim strSQL As String
strSQL = "SELECT * FROM Orders WHERE OrderID = @OrderID"

cn.Open()

Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("@OrderID ",
tbOrder.Text)

Dim tbl As New DataTable("Orders")
With tbl
.Columns.Add("OrderID", GetType(String))
.PrimaryKey = New DataColumn() {.Columns("OrderID")}
.Columns.Add("Item", GetType(String))
End With
da.Fill(tbl)

Dim rowToUpdate As DataRow
rowToUpdate = tbl.Rows.Find(tbOrder.Text)
strSQL = "UPDATE Orders " & _
"SET OrderID = @OrderID_New, " & _
"Item = @Item_New " & _
"WHERE OrderID = @OrderID_Old"

Dim cmdUpdate As New SqlCommand(strSQL, cn)
cmdUpdate.Parameters.AddWithValue("@OrderID_New",
rowToUpdate("OrderID"))
cmdUpdate.Parameters.AddWithValue("@Item_New",
rowToUpdate("Item"))

cmdUpdate.Parameters.AddWithValue("@OrderID_Old",
rowToUpdate("OrderID", DataRowVersion.Original))
cmdUpdate.Parameters.AddWithValue("@Item_Old",
rowToUpdate("Item", DataRowVersion.Original))

Try
Dim intRecordsAffected As Integer
intRecordsAffected = cmdUpdate.ExecuteNonQuery()
If intRecordsAffected = 1 Then
rowToUpdate.AcceptChanges()
ElseIf intRecordsAffected = 0 Then
MessageBox.Show("Update Failed - Query Affected No
Rows", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else : MessageBox.Show("Query affected " &
intRecordsAffected & " rows?!?", "Error - Multiple Records Found",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End If
End Sub

May 1 '07 #1
3 1607
After you run this, and before it exits, is the data in the database?

If you are using SQLServerExpress, there's some setting when you add a data
source to your project that says "copy it over every time I run my app",
and you could be replacing the copy you have just updated.

Robin S.
--------------------------------

"Alex" <fk****@yahoo.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
I've got a procedure designed to modify the contents of a single row
in a data table. The code appears to work fine in that it compiles
and executes without error and the changes are reflected in the
dataset. However, when I close and re-open the app, the changes are
lost, which means that they are not reaching the datasource. Can
anybody see why this is happening? I've searched all over and I think
that this should work - but it obviously doesn't (and I'm kind of a
moron). Any help is appreciated.

Thanks.

Dim strSQL As String
strSQL = "SELECT * FROM Orders WHERE OrderID = @OrderID"

cn.Open()

Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("@OrderID ",
tbOrder.Text)

Dim tbl As New DataTable("Orders")
With tbl
.Columns.Add("OrderID", GetType(String))
.PrimaryKey = New DataColumn() {.Columns("OrderID")}
.Columns.Add("Item", GetType(String))
End With
da.Fill(tbl)

Dim rowToUpdate As DataRow
rowToUpdate = tbl.Rows.Find(tbOrder.Text)
strSQL = "UPDATE Orders " & _
"SET OrderID = @OrderID_New, " & _
"Item = @Item_New " & _
"WHERE OrderID = @OrderID_Old"

Dim cmdUpdate As New SqlCommand(strSQL, cn)
cmdUpdate.Parameters.AddWithValue("@OrderID_New",
rowToUpdate("OrderID"))
cmdUpdate.Parameters.AddWithValue("@Item_New",
rowToUpdate("Item"))

cmdUpdate.Parameters.AddWithValue("@OrderID_Old",
rowToUpdate("OrderID", DataRowVersion.Original))
cmdUpdate.Parameters.AddWithValue("@Item_Old",
rowToUpdate("Item", DataRowVersion.Original))

Try
Dim intRecordsAffected As Integer
intRecordsAffected = cmdUpdate.ExecuteNonQuery()
If intRecordsAffected = 1 Then
rowToUpdate.AcceptChanges()
ElseIf intRecordsAffected = 0 Then
MessageBox.Show("Update Failed - Query Affected No
Rows", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else : MessageBox.Show("Query affected " &
intRecordsAffected & " rows?!?", "Error - Multiple Records Found",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End If
End Sub

May 1 '07 #2
How can I determine if the data is in the db before it exits?

I don't think that the db is being copied. I know what you are
referring to, but I don't think that this is the case. I have similar
code to add and delete records and that code works just fine. If this
was the issue, I would think that it would be a problem in those
cases, too.

Other than that, do you see any problems with the code itself?

Thanks, Robin.

May 1 '07 #3
You can determine if the data is in the db before it exits by doing
something like re-querying that specific record and displaying the values
in the record to see if they have changed. I would close your connection,
then re-open it and do this, and check and see if the values are there. If
they aren't, then they did not get committed.

Why are you doing this

Dim tbl As New DataTable("Orders")
With tbl
.Columns.Add("OrderID", GetType(String))
.PrimaryKey = New DataColumn() {.Columns("OrderID")}
.Columns.Add("Item", GetType(String))
End With

before this? The fill should get the field names.

da.Fill(tbl)

What is the value of intRecordsAffected after it runs the query?

Robin S.
---------------------
"Randy" <ra************@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
How can I determine if the data is in the db before it exits?

I don't think that the db is being copied. I know what you are
referring to, but I don't think that this is the case. I have similar
code to add and delete records and that code works just fine. If this
was the issue, I would think that it would be a problem in those
cases, too.

Other than that, do you see any problems with the code itself?

Thanks, Robin.

May 3 '07 #4

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

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
5
by: Klemens | last post by:
I get SQL30090 reason 18 by trying to do an insert in a federated table and an update in a local table in one transaction Do I have to change some settings to get done or ist this not possible by...
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
13
by: abdoly | last post by:
i wrote a code to update datagrid with the datagrid updatecommand but i cant get the updated values after being update that is the code private void DataGrid1_UpdateCommand(object source,...
3
by: RSH | last post by:
Hi, I have a situation in where i have two instances of SQL server, the first is our Production Environment, the second is our Development environment. Both servers contain the same databases...
0
by: saraDotNet | last post by:
hi everybody , I'm a beginner student in vb.net,and I'm working on a very small project as a homework,and I've some questions which I ought to search for their solutions via any source . My...
11
by: John | last post by:
Hi I had a working vs 2003 application with access backend. I added a couple fields in a table in access db and then to allow user to have access to these fields via app I did the following; ...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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,...
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.