Solved

vb.net tableadapter update command

Posted on 2011-03-02
6
1,300 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Accepted Solution

by:
bhlabelle earned 0 total points
Comment Utility
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
Comment Utility
Fine by me, nice you can resolve the problem
0
 

Author Closing Comment

by:bhlabelle
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now