Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2005, 03:20 PM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to paulhopkins
Default Updating MS SQL 2000 with C# Dataset

Hi,

I am trying to update my SQL 2000 Database with a modified dataset but am having trouble doing so. I have taken a look through the MS documentation etc but can't seem to solve the problem. The project is being written using the .NET compact framework for deployment on a PDA but I am sure the procedure is similiar, if not the same, as a windows app.

I have a list of items, which have a column in the dataset named 'picked' and when a button is clicked the status of that column is changed to 'y' and this is done for each item in turn. The dataset is filled in another form then the connection is closed.

The data is then manipulated in later forms and I now want to write those changes back to the DB, all examples seem to do the fill and update on the same page.

I modify the DS with the following code:

DataRow drItemsUpdate = dtItems.Rows.Find(itemNo);
        drItemsUpdate.BeginEdit();
            drItemsUpdate["Picked"] = "y";
            drItemsUpdate.EndEdit();

I have confirm, printing in a text box, the change has been made to the dataSet.

Once all the items have been iterated through I then want to update the dataset to the DB.

This is the code I use to try and do this

string cmdUpdateSelect = "update Items set Picked = 'y' where itemNo = "+itemNo;

SqlConnection conn = new SqlConnection(conString);
SqlDataAdapter adptDsUpdate = new SqlDataAdapter();
adptDsUpdate.UpdateCommand = new SqlCommand(cmdUpdateSelect,conn);
SqlCommandBuilder cbDsUpdate = new SqlCommandBuilder(adptDsUpdate);
conn.Open();

adptDsUpdate.Update(dsItems, "Items");

This execute the code with no problems but does not update the database! Where am I going wrong? I am not totally sure what the update command does?! Surely, the dataset would compare with the database and make the changes where the values were inconsistant??? Maybe it's not that easy!

Any help would be truely appreciated.
Thanks in advance

Paul
 
Old March 2nd, 2005, 05:15 PM
Registered User
 
Join Date: Feb 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Paul,

I find the command builder to be a complete nightmare to work with. For it to work you need to provide a 'select' statement to a data adapter, and command builder will then (hopefully) deduce the relevant insert, update and delete commands. You seem to be trying to get the command builder to work from an update command seed - I don't think this will work!

It's more long winded but the best way is to create a new dataset comprising the rows changed or added from your original dataset. You then need to loop through the new dataset and apply the relecvant update, insert or delete commands.

Good luck,

Dave






Similar Threads
Thread Thread Starter Forum Replies Last Post
How Run .sql Script file in MS SQL Server 2000? aarkaycee SQL Server 2000 5 October 12th, 2009 05:43 AM
SQl Injection through ASP and MS SQl 2000 cancer10 Classic ASP Databases 1 October 27th, 2007 03:21 AM
Database migration MS Access 2003 to MS SQL 2000 ayazhoda SQL Server 2000 3 April 23rd, 2007 11:38 AM
How to use ASP and MS SQL 2000 eapsokha Classic ASP Professional 2 February 26th, 2004 11:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.