'Update requires a valid UpdateCommand when passed DataRow collection with modified rows.' - vb.net

In my application I have three tables all from the same Access database. I have used the Wizard in Visual Studio Express 2019. This is an extension of my unresolved question here.
Database.TwixBindingSource.EndEdit()
Database.NPCsBindingSource.EndEdit()
Database.EffectsBindingSource.EndEdit()
Database.TableAdapterManager.UpdateAll(Database.DatabaseDataSet)
I have these three tables ("Twix","NPCs",and "Effects"). The above code automatically runs every 15 seconds, like an autosave feature. My issue is when I try to edit "NPCs" or "Effects" I recieve the following error:
'Update requires a valid UpdateCommand when passed DataRow collection
with modified rows.'
Most of the online answers in regard to this error are solved by ensuring each table has a Primary Key column that is recognised by Visual Studio in order to automatically generate the necessary commands to edit, updte, delete etc.
However, I am stumped because I have PKs on each table.
The only thing I can think is that it has something to do with the 'TableAdapterManager,'
ALthough I have no clue at this point.
EDIT: I have replaced the UPDATE ALL with individual UPDATES, but the same problem persists...
Database.TwixBindingSource.EndEdit()
Database.TwixTableAdapter.Update(Database.DatabaseDataSet)
Database.NPCsBindingSource.EndEdit()
Database.NPCsTableAdapter.Update(Database.DatabaseDataSet)
Database.EffectsBindingSource.EndEdit()
Database.EffectsTableAdapter.Update(Database.DatabaseDataSet)

Right click on your tableadapter (on the header, not the Fill command), and choose Configure
Click Advanced Options and verify that "Generate I/U/D" is ticked
When this isn't ticked, the resulting TA doesn't have any DML statements built:
If your SELECT statement doesn't select the column that is set as the primary key in the database, then this Generate IUD tickbox may be greyed out, or it will be ticked but the DML statements won't generate. Pay attention to the final page of the wizard. Here is what happened when I made a table "Other" that had no primary key:
INSERT generates, because it's easy to generate an insert on a keyless table, but update and delete cannot be generated
If you don't select the PK column you get a warning:
It's important that your DB tables have a PK; it's not the same thing to declare some datacolumns of a datatable in a dataset to be a primary key. A Dataset is not a database; it may have more or fewer tables/columns and the presentation and datatypes of row data do not have to match the DB. I can see that your dataset screenshot shows some tables have PKs declared in the DataSet side, but this is not a statement that they are definitely PKs on the DB side
Feel free to delete the DataTable; it will delete the TableAdapter too. You can then recreate that one TA by right click, new, tableadapter.. SELECT * FROM table
If you hadn't already realized, remember that you can (and should) declare more queries per tableadapter than just keeping with the basic Fill, which appears to be a SELECT * FROM without a WHERE clause in your case. Personally I always make my first query SELECT * FROM table WHERE id = #id because it's really rare that you want to download a whole table.. You can leave the default as a WHEREless query, but consider adding others, such as SELECT * FROM Twix WHERE Location = #location and naming the query FillByLocation. In code you can then fill just the locations you want, rather than downloading 10000 Twixes into the app just to show some of them (with a rowfilter, i guess)

This question and answers helped me figure out my problem.
NOTE that if your problem is your table (for example you forgot to set a primary key and you go back and fix it in SSMS while debugging) you will need to find the menus in Caius answer, ensure boxes are ticked, and hit Finish if nothing else.
This will refresh something about the statements(unclear what), and allow this to work. Otherwise you will continue to receive these errors as if you'd fixed nothing, and probably continue to search to no avail.

Related

DataGridView bound to MS Access table with Autonumber Primary key causes Concurrency errors

A have a volunteer timesheet data entry system which allows the volunteers to enter the times they have spent on various activities. I used the VB.net Designer to create the system (OK, I know now that that was not a good move!) so please don't ask me to show my code, most of it is generated by the Designer. My problem is this:
Each new record is assigned a negative number as a primary key when it is entered which is the way a dgv works with Access Automumber keys. I am executing the following statements in the RowValidating event when the row is valid.
a_dgv.EndEdit()
a_dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
Me.TimeSheets2BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.MembershipDataSet)
This code does not update the primary key value on the dgv although it does so in the Access table. If a user then attempts to delete or alter a record he has earlier added in the same session the update fails with a concurrency error. The only answer if have found to this problem is to refill the whole table. This is obviously not a desirable solution. Does anyone have a proven tested one?
I should probably mention that my table has two databound comboboxes
I was under the impression that a datagrid that is the result of a dataset from say Access does not show the PK values as -1, -2, -3.
If you created the disconnected dataset (or datatable) in code from a fill (pull data from Access), then each row normally does not show the PK.
However, regardless of the above, assuming you entered 5 rows, and now need to see the PK values?
You will during data entry in the grid should see this:
In above, I have added two rows. Your save code is somewhat like this:
tblHotels = DataGridView1.DataSource
rstDataReader.Update(tblHotels)
tblHotels.AcceptChanges()
That will send the data back to SQL server (or Access), and the autonumber PK 'ids are then generated. However, such changes are NOT pulled back into the dataset/datatable. In other words, the PK id's are generated in the database, but UNLESS you re-pull the data, you are not going to see the PK values.
You WILL have to re-pull the data. However, you can keep the current position of the grid, and re-fill the data like this:
rstDataReader.Update(tblHotels)
tblHotels.AcceptChanges()
Dim MyTop As Integer = DataGridView1.FirstDisplayedScrollingRowIndex
tblHotels.Clear()
rstDataReader.Fill(tblHotels)
DataGridView1.FirstDisplayedScrollingRowIndex = MyTop
And then you should see this:
The other way would be to send + update each row as you edit data, and then pull the PK, but obvious then you not be able to update the all your grid changes with a SAVE button, and thus of course no un-do ability.
I find the above that re-positions the top of the grid does not flicker. On the other hand, I suppose this could/would depend on how large the data set is (but then again, loading up a grid with too many rows is less then ideal).
So, as far as I can tell, you have to re-pull the dataset/datatable to get the new generated PK id's, or you have to save + pull for each row you edit. For a gridview with even several 100 rows, I don't see any flicker with the above code.

SQL Server 2012 Intellisense issue

My problem is that Intellisense does not provide complete auto suggest for the columns that I have in my tables .
Here is an example:
As you can see on SSMS it does give me auto suggest for my tables, but does not for columns. I have read couple articles about solving some Intellisense issues, but nothing helped. Here is things I tried described in this article: http://www.mssqltips.com/sqlservertip/2591/troubleshooting-intellisense-in-sql-server-management-studio-2012/
Any suggestions would be greatly appreciated, thank you for your time!
IntelliSense can't predict which table you're going to select from, and will wait until you have at least one table in the FROM clause, and probably only until you specify an alias. before populating the columns in the case of a join or other multi-table query.
There's a good reason for this. Imagine if you have CustomerID or InvoiceID in 20 different tables in your database. Should it list this 20 times? Which one should you pick? Do you really want all the columns in your entire database in a drop-down list? In a lot of scenarios this will be a very long list. And not pretty either, in things like SharePoint, NAV Dynamics, etc.
If you're not happy with the way the native IntelliSense works, there are 3rd party tools that might do what you want, but I'm not sure what you want will actually help you work any better.
First, that's because at the time of the screenshot SSMS does not know from what object you are selecting. In other words, it cannot guess what columns you're interested in when there is not a from clause in your select statement. If you try to type in the columns in the following select statement...
select from dbo.Invoices
you will see that SSMS will start to pick up your columns because you have already specified a from clause, so SSMS knows how to suggest you column names...because there is a table specified in the from clause
This is mainly occurs when you add any table in the Database OR Add any Column in the Table.
This is the known issue of the SQL Server 2012. You need to refresh the Cache for that.
You can achieve using 2 ways..
1) You can Refresh the Local Cache of the IntelliSense.
Open New Query window and Navigate to following menu.
Edit > IntelliSense > Refresh Local Cache
Shortcut key for this is (Ctrl + Shift + R)
2) ReConnect Your Database.

Table '' could not be loaded

I was hoping someone out there may have experienced this before.
I have a database that (as far as I'm aware) is in perfect working order. I have no problems with it whatsoever. I'm trying to add a column to some of the tables but when I save the changes I get the following message
This error message is then stuck in a loop and the only thing I can do is kill the SQL Management Studio process.
The database exists, the table exists, I can run any query I want against it, I just can't make any changes to it.
The steps I'm taking are:
Right click table
Select "design"
Right click "add new column" in designer
Fill in the details as normal
Click Save
Anyone know how I can resolve this?
Thanks.
It's telling you that you haven't specified the name of the table. The name of the table should be between the two single quotes.
Without knowing how you're doing this it's hard to tell more, but the first two possibilities off the top of my head are:
If you're looping through tables in code to do something, you may be hitting a record with no table name.
If it's pure SQL, perhaps an error in your syntax

How to update the Dataset to reflect an added column in the data source without deleting the adapter?

I've made a dataset using the dataset designer, and I'm trying to add a column to reflect changes made to the database (added a column, nothing fancy). Is there a way to 'refresh' the dataset schema from the datasource without deleting my adapter (and all the methods and queries I've created)?
I know its been a while since you posted but as I was having the same problem and figured out how to do this I reckoned I'll post the solution that worked for me.
Right click on the dataset object you want to update (on the strip at the bottom of your viewpane)
Select "Edit in Dataset Designer"
in the dataset designer, right click on the header of the table you want to add a column to
select configure... this will bring up the sql statement that is used to draw values into the dataset for this table
Edit the sql to include the column you want to include in your dataset's table and click finish i.e. in the select statement, include your columns name in the list
close the dataset designer then go to any controls (in my case its a datagridview), click on the tasks arrow (top right hand corner next to the handle) and select add column
select the newly created column from the list of databound columns and click "add"
select "edit columns" from the task menu
move the column to the correct position (it will always be placed as the last column in your grid and you may not want it to be the last column)
voila, I know its hardly snappy but it beats the hell out of deleting the dataset and then fixing up all the coding errors that come up... also after doing it a few times it'll be like second nature (I hope)
regards
p.s. am working in VS2010
Had to just delete the adapter and the table. It's rather annoying but I guess there really isn't a way around it. Maybe in VS2010 or later versions of .net.

MS Acess 2003 - VBA for Update SQL query?

hey guys, could someone show me the simple update query through vb? I need to add new fields to the table (just 3) and add a couple text boxes on a form so that users can add some additional data relative to the record (which is already what this form is based on).
So the first form I have is a form that populates a list, when the user double clicks on a selection from that list, it opens a new form, so that the ID of the the table that is tied to this form that I need to add the these text boxes on (all the combo boxes and text boxes relative to one record are tied to the active form at this point, however there are all unbound. On a button click there is already vb that saves the information to the table). I did not create this however, it was built by someone who is not there anymore, and apparently is better than I at this stuff. My problem is that there is soooo much vb that checks for records, and various sql statements based on case, that I cannot decipher it to its simplest form.
So I was looking for a simple example of an update sql statement in vb so I can try to break this apart.
I need it to update the record based on the ID: sql WHERE RecordID = me.RecordID
I actually thought I knew how to do this based on examples, however every time I try, then try to run on button click, I get a run-time error of SYNTAX error, and the debug just highlights the db.execute(sql) part. So I tried to get the resulting immediate window of the sql statement, and it looks fine to me:
UPDATE tblMain
SET [Name] = "John Doe",
[DATE] = #9/30/2009#,
[TYPE] = "TypeA",
WHERE RecordID = 958;
Can I update a table without accounting for every field in the table (because this one has about 15 plus the new 3, so I am ignoring about 14 fields here, but I do not want to alter those anyway???
So as always, I appreciate the help yall!! Thanks!
EDIT:
Sorry I always forget this....I was actaully trying it DAO....
Dim db as DAO.Database
Dim sql as String
set db = CurrentDb
etc
You were thaaat close! You have a simple extra comma after your last column. Get rid of it and it works fine.
UPDATE tblMain SET
[Name] = "John Doe",
[DATE] = #9/30/2009#,
[TYPE] = "TypeA"
WHERE RecordID = 958;
Yes, you can absolutely update only a few columns rather than all of them. That is a best practice, BTW.
Finally, It's considered bad practice to name your columns after reserved words like "Name" and "Date", but I know you inherited this.
You were wise to include Debug.Print sql in your code. bpayne already pointed out the extra comma in your SQL statement.
I want to point out another trouble shooting technique you may find useful to debug SQL statement problems.
Copy the statement from the Immediate Window, and paste it into the SQL View of a new query. Modify the query in the query designer until you can get it working, then revise your VBA code to generate a matching SQL statement.
In this case you might not have noticed the extra comma. However, you could create another new query and build the UPDATE statement from scratch in the query designer. After getting that one working, you could compare its SQL View to the failing query.