PC Review


Reply
Thread Tools Rate Thread

SqlDataAdapter

 
 
daveL
Guest
Posts: n/a
 
      19th Oct 2008
Hello
I have a Windows Form with Controls
when the user clicks the save button
I place the Controls Values into a DataRow
this Row is then added To a DataTable with (dtTmp)
Row count of 1

I Set up DataAdapter with the update command and insertCommand
and it Inserts a new Row into the physical Table when its a Existing Row
with a identity Column
if I issue dtTmp.AcceptChanges() , then it does not insert, But it does not
do the Update

dtTmp is Cleared after each Write its used to insert or update depending
whether the user is adding or editing
so it always has a new row in it , with count of 1.
below is Code im Using

dtTmp.Clear();
dtTmp.Rows.Add(this.oRow)

SqlDataAdapter ad = new SqlDataAdapter();

ad.SelectCommand = new SqlCommand("select Top 0 * from users order by
userid");

ad.SelectCommand.Connection = this.SqlConn1.Conn;

SqlCommandBuilder cmd = new SqlCommandBuilder(ad);

ad.InsertCommand = cmd.GetInsertCommand();

ad.UpdateCommand = cmd.GetUpdateCommand();

ad.UpdateCommand.Connection = this.SqlConn1.Conn;

ad.InsertCommand.Connection = this.SqlConn1.Conn;

ad.UpdateCommand.Parameters.Add("@Userid", SqlDbType.Int);

ad.UpdateCommand.Parameters["@UserId"].SourceColumn = "UserId";



ad.update(dtTmp) // creates duplicate row if dttmp.acceptchanges() not
issued this row does exist in physical table



DaveL






 
Reply With Quote
 
 
 
 
William Vaughn \(MVP\)
Guest
Posts: n/a
 
      20th Oct 2008
Stop. Unless you're implementing your own routines to handle update
operations (you aren't), you should not use AcceptChanges. It tells ADO.NET
to reset the data changed state on the client dataset.
Next, consider that the CommandBuilder is designed for use behind the scenes
by the VS code-generators, not by application code. See any of the dozens of
articles that discuss this issue.


--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________



"daveL" <(E-Mail Removed)> wrote in message
news:jzGKk.4124$(E-Mail Removed)...
> Hello
> I have a Windows Form with Controls
> when the user clicks the save button
> I place the Controls Values into a DataRow
> this Row is then added To a DataTable with (dtTmp)
> Row count of 1
>
> I Set up DataAdapter with the update command and insertCommand
> and it Inserts a new Row into the physical Table when its a Existing Row
> with a identity Column
> if I issue dtTmp.AcceptChanges() , then it does not insert, But it does
> not do the Update
>
> dtTmp is Cleared after each Write its used to insert or update depending
> whether the user is adding or editing
> so it always has a new row in it , with count of 1.
> below is Code im Using
>
> dtTmp.Clear();
> dtTmp.Rows.Add(this.oRow)
>
> SqlDataAdapter ad = new SqlDataAdapter();
>
> ad.SelectCommand = new SqlCommand("select Top 0 * from users order by
> userid");
>
> ad.SelectCommand.Connection = this.SqlConn1.Conn;
>
> SqlCommandBuilder cmd = new SqlCommandBuilder(ad);
>
> ad.InsertCommand = cmd.GetInsertCommand();
>
> ad.UpdateCommand = cmd.GetUpdateCommand();
>
> ad.UpdateCommand.Connection = this.SqlConn1.Conn;
>
> ad.InsertCommand.Connection = this.SqlConn1.Conn;
>
> ad.UpdateCommand.Parameters.Add("@Userid", SqlDbType.Int);
>
> ad.UpdateCommand.Parameters["@UserId"].SourceColumn = "UserId";
>
>
>
> ad.update(dtTmp) // creates duplicate row if dttmp.acceptchanges() not
> issued this row does exist in physical table
>
>
>
> DaveL
>
>
>
>
>
>

 
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
SQLDataAdapter.Fill fails after call to SQLDataAdapter.FillSchema zdrakec Microsoft ADO .NET 1 5th Sep 2012 05:11 PM
SqlDataAdapter, a SqlCommandBuilder, and a SqlTransaction Kal Microsoft ADO .NET 2 22nd Jul 2003 03:23 PM
SQLDataAdapter filled from temporary table Willy Esteban Microsoft ADO .NET 7 22nd Jul 2003 01:45 AM
Question about SqlDataAdapter.RowUpdating Event Programatix Microsoft ADO .NET 7 19th Jul 2003 06:04 PM
SqlDataAdapter question Jan Nielsen Microsoft ADO .NET 2 7th Jul 2003 04:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.