PC Review


Reply
Thread Tools Rate Thread

DataAdapter UpdateCommand

 
 
GatorBait
Guest
Posts: n/a
 
      12th Aug 2005
Hi All,

I have a dataset that has more than one table in it so I am unable to
use the CommandBuilder to automatically generate an update command so I
have to do it by hand. I am a little confused on how to write such a
command and was hoping someone could help me out. Basically, I have 2
tables: 'Customers' and 'Letters' that are joined by a 'Customer_ID'
field. There is a field 'Printed' inside the Letters table.

In my application, the user can generate letters for customers, and I
keep the 'batch' of letters in a dataset. When the user prints the
letter for a particular customer, I set the Printed field to 1. There
can be multiple records that get printed so I'm confused on how I write
the update command for the data adapter.

UPDATE Letters SET Printed = 1 WHERE ??????

Can someone help me? Thanks a lot in advance!

 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      12th Aug 2005
Are you only updating columns that belong to one table? In which case, just
change the CommandText of the SelectCommand on the adapter to a query that
only includes only the columns from the one table you want to update, and
that one table.

So, if your original query was:

Select T1.SomeKeyCol, T1.Col1,T2.Col2 FROM T1,T2 WHERE T1.SomeKeyCol=
T2.SomeKeyCol WHERE ...

change it to:

Select SomeKeyCol,Col1 From T1

You don't need a WHERE clause or anything on this new 'fake' query. The
CommandBuilder will just use this new query to read the schema for T1.


"GatorBait" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All,
>
> I have a dataset that has more than one table in it so I am unable to
> use the CommandBuilder to automatically generate an update command so I
> have to do it by hand. I am a little confused on how to write such a
> command and was hoping someone could help me out. Basically, I have 2
> tables: 'Customers' and 'Letters' that are joined by a 'Customer_ID'
> field. There is a field 'Printed' inside the Letters table.
>
> In my application, the user can generate letters for customers, and I
> keep the 'batch' of letters in a dataset. When the user prints the
> letter for a particular customer, I set the Printed field to 1. There
> can be multiple records that get printed so I'm confused on how I write
> the update command for the data adapter.
>
> UPDATE Letters SET Printed = 1 WHERE ??????
>
> Can someone help me? Thanks a lot in advance!
>



 
Reply With Quote
 
GatorBait
Guest
Posts: n/a
 
      12th Aug 2005
Hi Marina,

Thanks for your reply. I am selecting from 2 tables, and I need to in
this case because the data is used in a Crystal report. All I want to
do is update the table with the changed dataset (I change the value of
the 'Printed' field in the 'Letters' table). Here is the code I am
using:

Cursor = Cursors.WaitCursor

Dim conn As New SqlConnection()
Dim cmdBuilder As SqlCommandBuilder
Dim rpt As New Letter()
Dim strSQL As String

conn = GetMainConnSQL("UserName", "Password", "Server")

strSQL = "SELECT " & _
"Letters.*, CLNMAS.CompanyName, CLNMAS.FNAME,
CLNMAS.LNAME, CLNMAS.ADDRESS1, CLNMAS.ADDRESS2, CLNMAS.CITY,
CLNMAS.STATE, CLNMAS.ZIP, Email " & _
"FROM " & _
"CLNMAS RIGHT JOIN Letters (NOLOCK) ON
CLNMAS.CLIENT_ID = Letters.Client_ID " & _
"WHERE " & _
"Letters.Printed = 0"

daLetters = New SqlDataAdapter(strSQL, conn)
dsLetters = New DataSet()

daLetters.Fill(dsLetters, "tLetters")
cmdBuilder = New SqlCommandBuilder(daLetters)
(of course, this errors out!)
daLetters.UpdateCommand = cmdBuilder.GetUpdateCommand

rpt.SetDataSource(dsLetters.Tables("tLetters"))

fCrystalViewer = New CrystalViewer()
fCrystalViewer.crvMain.ReportSource = rpt
fCrystalViewer.MdiParent = Me.MdiParent
fCrystalViewer.Show()

Cursor = Cursors.Arrow


When the user closes the Crystal Report, the following line is executed
daLetters.Update(dsLetters, "tLetters")

Thanks!!!

 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      12th Aug 2005
I already answered what one technique for solving this kind of problem is...

"GatorBait" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Marina,
>
> Thanks for your reply. I am selecting from 2 tables, and I need to in
> this case because the data is used in a Crystal report. All I want to
> do is update the table with the changed dataset (I change the value of
> the 'Printed' field in the 'Letters' table). Here is the code I am
> using:
>
> Cursor = Cursors.WaitCursor
>
> Dim conn As New SqlConnection()
> Dim cmdBuilder As SqlCommandBuilder
> Dim rpt As New Letter()
> Dim strSQL As String
>
> conn = GetMainConnSQL("UserName", "Password", "Server")
>
> strSQL = "SELECT " & _
> "Letters.*, CLNMAS.CompanyName, CLNMAS.FNAME,
> CLNMAS.LNAME, CLNMAS.ADDRESS1, CLNMAS.ADDRESS2, CLNMAS.CITY,
> CLNMAS.STATE, CLNMAS.ZIP, Email " & _
> "FROM " & _
> "CLNMAS RIGHT JOIN Letters (NOLOCK) ON
> CLNMAS.CLIENT_ID = Letters.Client_ID " & _
> "WHERE " & _
> "Letters.Printed = 0"
>
> daLetters = New SqlDataAdapter(strSQL, conn)
> dsLetters = New DataSet()
>
> daLetters.Fill(dsLetters, "tLetters")
> cmdBuilder = New SqlCommandBuilder(daLetters)
> (of course, this errors out!)
> daLetters.UpdateCommand = cmdBuilder.GetUpdateCommand
>
> rpt.SetDataSource(dsLetters.Tables("tLetters"))
>
> fCrystalViewer = New CrystalViewer()
> fCrystalViewer.crvMain.ReportSource = rpt
> fCrystalViewer.MdiParent = Me.MdiParent
> fCrystalViewer.Show()
>
> Cursor = Cursors.Arrow
>
>
> When the user closes the Crystal Report, the following line is executed
> daLetters.Update(dsLetters, "tLetters")
>
> Thanks!!!
>



 
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
why is dataAdapter.UpdateCommand not updating? =?Utf-8?B?UmljaA==?= Microsoft VB .NET 6 7th Jun 2006 07:08 AM
UpdateCommand and InsertCommand for the same DataAdapter? Rico Microsoft ADO .NET 4 23rd Mar 2006 12:01 AM
DataAdapter UpdateCommand Mark Matheney Microsoft ADO .NET 3 1st May 2004 02:14 AM
DataAdapter.UpdateCommand problem Sébastien Microsoft ADO .NET 3 27th Jan 2004 12:42 AM
How to use UpdateCommand in DataAdapter vivek srivastav Microsoft ADO .NET 2 4th Oct 2003 04:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:30 AM.