Solved

vb.net tableadapter update command

Posted on 2011-03-02
6
1,311 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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