PC Review


Reply
Thread Tools Rate Thread

Update Method Not Updating Database

 
 
Kenny Nemchak
Guest
Posts: n/a
 
      24th Nov 2004
I'm new to ASP.NET so I'm probably just missing a simple statement.
The code runs fine with no errors, but the update method does not
update the SQL database.
I open the database, fill the dataset, find a row, delete the row and
attempt to update the database.
All but the update works.
I have 2 asp.net books and have tried their ways…both different.
I've tried to read everything on msdn.microsoft and tried their ways.
The one books says the update method does everything for you…the other
book says you need to set a deletecommand…which I did and still no
update…you can see the deletecommand commented out below.
I also tried creating a second dataset and inserting the changes but
when I use this dataset with update, it errors out and says it's
null…those lines are commented out below.
You can see I put in a couple lines to show the dataset and any
changes…it shows no changes…???
However, when I write out the dataset after deleting the row, the
deleted row is missing.

Dim dbConnection As new SqlConnection("server='xsql'; user id='****';
password='****'; database='cconnect'")
Dim dataAdapter As New SqlDataAdapter("SELECT * FROM
sysiad",dbConnection)
Dim dataSet As New DataSet
dataAdapter.Fill(dataSet,"sysiad")

'Dim cmd as new SqlCommand("DELETE FROM sysiad",dbConnection)
'dataAdapter.deleteCommand = cmd

Dim mytable as DataTable = dataset.tables("sysiad")
Dim i as integer = 0
response.write(mytable.rows.count & "<br>")
for i = 0 to (mytable.rows.count - 1)
response.write("'" & mytable.rows(i)(0) & "'" & mytable.rows(i)(1)
& "<br>")
if trim(mytable.rows(i)(0)) = request.form("username") then
response.write("stop <br>")
mytable.rows.removeAt(i)
exit for
end if
next i
response.write(mytable.rows.count & "<br>")

'Dim datasetmod as dataset
'datasetmod = dataset.GetChanges()

dataAdapter.update(dataset,"sysiad")

if dataset.HasChanges()
response.write("Has Changes <br>")
else
response.write("Has No Changes <br>")
end if

Dim mydataview as dataview
mydataview = new dataview(dataset.tables("sysiad"))
If mydataview.count <> 0 then
response.write("<table border=1>")
for i = 0 to mydataview.count - 1
response.write("<tr><td>")
response.write(mydataview(i)("username").tostring)
response.write("</td><td>")
response.write(mydataview(i)("computername").tostring)
response.write("</td></tr>")
next
response.write("</table><br>")
end if

response.write("'" & request.form("username") &"' has been removed
from the database.")
 
Reply With Quote
 
 
 
 
Norman Yuan
Guest
Posts: n/a
 
      24th Nov 2004
Do not "Remove()/RemoveAt()" datarow from daratable, "Delete()" instead.
Study .NET document to find out the difference of Remove() and Delete().


"Kenny Nemchak" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm new to ASP.NET so I'm probably just missing a simple statement.
> The code runs fine with no errors, but the update method does not
> update the SQL database.
> I open the database, fill the dataset, find a row, delete the row and
> attempt to update the database.
> All but the update works.
> I have 2 asp.net books and have tried their ways.both different.
> I've tried to read everything on msdn.microsoft and tried their ways.
> The one books says the update method does everything for you.the other
> book says you need to set a deletecommand.which I did and still no
> update.you can see the deletecommand commented out below.
> I also tried creating a second dataset and inserting the changes but
> when I use this dataset with update, it errors out and says it's
> null.those lines are commented out below.
> You can see I put in a couple lines to show the dataset and any
> changes.it shows no changes.???
> However, when I write out the dataset after deleting the row, the
> deleted row is missing.
>
> Dim dbConnection As new SqlConnection("server='xsql'; user id='****';
> password='****'; database='cconnect'")
> Dim dataAdapter As New SqlDataAdapter("SELECT * FROM
> sysiad",dbConnection)
> Dim dataSet As New DataSet
> dataAdapter.Fill(dataSet,"sysiad")
>
> 'Dim cmd as new SqlCommand("DELETE FROM sysiad",dbConnection)
> 'dataAdapter.deleteCommand = cmd
>
> Dim mytable as DataTable = dataset.tables("sysiad")
> Dim i as integer = 0
> response.write(mytable.rows.count & "<br>")
> for i = 0 to (mytable.rows.count - 1)
> response.write("'" & mytable.rows(i)(0) & "'" & mytable.rows(i)(1)
> & "<br>")
> if trim(mytable.rows(i)(0)) = request.form("username") then
> response.write("stop <br>")
> mytable.rows.removeAt(i)
> exit for
> end if
> next i
> response.write(mytable.rows.count & "<br>")
>
> 'Dim datasetmod as dataset
> 'datasetmod = dataset.GetChanges()
>
> dataAdapter.update(dataset,"sysiad")
>
> if dataset.HasChanges()
> response.write("Has Changes <br>")
> else
> response.write("Has No Changes <br>")
> end if
>
> Dim mydataview as dataview
> mydataview = new dataview(dataset.tables("sysiad"))
> If mydataview.count <> 0 then
> response.write("<table border=1>")
> for i = 0 to mydataview.count - 1
> response.write("<tr><td>")
> response.write(mydataview(i)("username").tostring)
> response.write("</td><td>")
> response.write(mydataview(i)("computername").tostring)
> response.write("</td></tr>")
> next
> response.write("</table><br>")
> end if
>
> response.write("'" & request.form("username") &"' has been removed
> from the database.")



 
Reply With Quote
 
Kenny Nemchak
Guest
Posts: n/a
 
      27th Nov 2004
"Norman Yuan" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Do not "Remove()/RemoveAt()" datarow from daratable, "Delete()" instead.
> Study .NET document to find out the difference of Remove() and Delete().



That was it...thanks.

MSDN says "Remove()/RemoveAt()" is the same as "Delete()" plus
"AcceptChanges()".
So I changed RemoveAt to Delete AND AcceptChanges...it didn't
work...meaning the database appeared not to be updated.
So I removed the AcceptChanges and it works great.
I had to uncomment my deletecommand to make it work.

I have another question...what is Remove()/RemoveAt() used for?
Display only? You don't use it if you intend to update the source
database?
When would you use the AcceptChanges...after updating the database?

Thanks again.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update database from ODBC query (or another method) Richard Edwards Microsoft Excel Programming 4 24th Jun 2008 12:47 PM
Update database from ODBC query (or another method) Richard Edwards Microsoft Excel Worksheet Functions 4 24th Jun 2008 12:47 PM
Datatable Update method not updating Flomo Togba Kwele Microsoft ADO .NET 3 8th Aug 2007 10:45 PM
Error when updating database using the dataadapter.update method =?Utf-8?B?UmlhZA==?= Microsoft ADO .NET 5 4th Aug 2005 05:21 AM
Cascading issues while updating multiple tables with SqlDataAdapter.Update method geeksgk@yahoo.com Microsoft ADO .NET 1 8th Jan 2005 04:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:18 PM.