Solved

vb.net tableadapter update command

Posted on 2011-03-02
6
1,327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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