Solved

vb.net tableadapter update command

Posted on 2011-03-02
6
1,322 Views
Last Modified: 2012-05-11
I do not know how to write an update command, and could use some help.

I have a datagridview that is linked to an access database. The table is "tblLetters", and the fields are "Descr", "Sent", "Return", "Rec", "Notes"

In the DataGridView1_CellEndEdit routine I have the following code:
Me.Validate()
Me.TblLettersBindingSource.EndEdit()
Me.TblLettersTableAdapter.Update(Me.FODBDataSet.tblLetters)

In the DataSet tblLettersTableAdapter I have the following Command Text in the Update Command:
UPDATE       tblLetters
SET                Descr = ?, Sent = ?, Return = ?, Rec = ?, Notes = ?

Now I know this is wrong, and if you are interested in knowing what this does, well whatever record you make the update on, lets say letter #5, now becomes the record for every letter.  Instead of having letters 1-10, you now have 10 letters that all have the same information as letter #5.  As you can tell, I'm not a programmer, more of a hack just trying to write a small program for our admin people.  

There is no primary key in the tblLetters, not sure if I need to have one or not.

Hopefully I haven't left anything out.  Thanks for any assitance!
0
Comment
Question by:bhlabelle
  • 3
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 300 total points
ID: 35017612
Hi,

Your Update command is updating ALL your records because you have not included an WHERE clause, the WHERE clause tells the Update command wich record to update.

Is better if you have a primary key in your table, this way it will be easy to find the record that you want to update.

Lets say you add a primary key on a field called ID, then your Where clause should be

WHERE ID = theidynumberyouwant to update

So, assuming you want to update the ID number 5 your Update command should be

UPDATE       tblLetters
SET                Descr = ?, Sent = ?, Return = ?, Rec = ?, Notes = ?
WHERE        ID = 5

Hope that helps
0
 

Author Comment

by:bhlabelle
ID: 35017835

I made the Descr the Primary Key in my Access DB, and I added the following Where clause:
UPDATE       tblLetters
SET                Descr = ?, Sent = ?, Return = ?, Rec = ?, Notes = ?
WHERE        (Descr = Descr)

However, when I try to update a record I get the following error message:
The changes you requested to the table were not successful because they would create values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

Seems like my vb application is still doing the same thing, but now Access is not permiting copying the same record multiple times since there is a primay key.
0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 200 total points
ID: 35018411
Hi!

Your query should not SET the Descr(PrimaryKey in your case)

UPDATE       tblLetters
SET                Descr = ?, Sent = ?, Return = ?, Rec = ?, Notes = ?
WHERE        (Descr = Descr)

it should be appear like this:

UPDATE       tblLetters
SET              Sent = ?, Return = ?, Rec = ?, Notes = ?
WHERE        (Descr = 5)

This will update the all the values of the row having primary key = 5.

Hope this can help
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
bhlabelle earned 0 total points
ID: 35018895
I added Num to my database as the primary key...it is an autonumber (Descr is a description of the letter).  

My question is, how should the WHERE clause be written...it can't say where Num = 5...what if they are updaing the 10th letter?  So I tried:
UPDATE       tblLetters
SET              Descr = ?, Sent = ?, Return = ?, Rec = ?, Notes = ?
WHERE        (Num = [Num])

Now I'm back to updating every record to show what the last record I changed lists for each field, except of course for the Num which doesn't get changed.  
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35020002
Fine by me, nice you can resolve the problem
0
 

Author Closing Comment

by:bhlabelle
ID: 35067765
I awarded points to both, a few more to the first response, hope that is ok with you two.

I had to make the Where clause:
Num = ?

And all was good.  

I appreciate the assitance.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question