?
Solved

vb.net tableadapter update command

Posted on 2011-03-02
6
Medium Priority
?
1,352 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 1200 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 800 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
Industry Leaders: 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

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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