/Home /Archive /Syndicate /Blog /Support /About /Contact  
All Visual Basic Feeds in one place!





One of my favourite lines from The Simpsons is where Homer says, “Doh, every time I learn something new I have to forget something I know to make some room in my head!”.     When I find myself making repeat mistakes that I made years ago, I know just how he feels.

  Here’s one that came back to bite me, which might help you if you’re currently sat staring at the above error message in Visual Studio.

While trying out the drag and drop dataset features, I had created a new very basic SQL Server database in Visual Studio using the Server Explorer tools by right-clicking on Data Connections and selecting Create New SQL Server Database.

Once I’d set up a couple of tables and saved all, I stayed in Server Explorer just to add a few demo entries into the database.

Next, I opened a Windows Form in design view and then used the Data Sources window to Add a New Data Source.   Selecting my newly created database gained me a DataSet with the relevant table data and I dragged and dropped a table from this DataSet on to the form.   As expected, this created a DataGridView displaying my demo data, together with a toolstrip containing navigation, add, delete and save buttons.

 

sql001

 

Inserting a new row and saving that worked fine, but if I tried to edit an existing item, I would get an error message:

 

sql002

 

Bearing mind that the DataGridView specifically allows for edits and deletes:

sql003

 

you may be wondering why this message keeps appearing.   However if you select the dataset in the Solution Explorer, double click it to display the design view and then click on the TableAdapter, you will see in the Properties Window that those two commands are in fact missing.

sql004

The solution to this problem isn’t – as you might think – to sit down and laboriously type in the missing commands.   It’s a more underlying SQL Server problem and it is simply this. If the Table doesn’t have a Primary Key then this error will occur.  

So all you have to do is edit the database and ensure that at least one column is identified as a Primary Key.   In my simple example, this could be the name column – although more realistically I would add an auto-numbering identity column which will ensure that all values in that column are unique.   Resave everything, recreate a new DataSet, replace the DataGridView using drag and drop as before, and you’re good to go.   As usual, the answer is simple when you know it – incredibly frustrating when you don’t. 

So if you’ve landed here because you’re staring at the above message – or a similar one regarding DeleteCommand – I hope this will help to restore your sanity and get you back up and running again.

© 2005 Serge Baranovsky. All rights reserved.
All feed content is property of original publisher. Designated trademarks and brands are the property of their respective owners.

This site is maintained by SubMain(), a division of vbCity.com, LLC