Question Have DataSet reflect modified database?

gs99

Active member
Joined
May 8, 2010
Messages
42
Programming Experience
Beginner
I'm using VB and SQL Server 2008 Express.
These are the steps I take to make a simple SQL database application:

Create new Windows Application project.
Add new item; Service-based Database named MyDB.mdf.
In Data Explorer, Add new table named Person.
Add two columns, ID and FirstName.
Add New Data Source, creates MyDBDataSet.
Drag Person table from DataSet to Form1, generating PersonDataGridView etc..
The code generated by VS includes navigation and a Save button.
The application runs OK. I can add records and save changes.

Now I need to change the database (add LastName column).

What steps are needed to have the DataSet reflect the modified database?

I've tried several methods but have problems.
A. If I delete the DataSet from Solution Explorer,
Form1.vb [Design] changes to a special Error Report, beginning with this line:
"To prevent possible data loss before loading the designer, the following errors must be resolved:"
So that doesn't look good.

B. If I delete objects in other ways, and Add New Data Source, and drag the DataSet again,
The PersonBindingNavigator is not generated.

Is there a way to do this when using these "simple" methods?
I would think this is a common challenge when new versions of business applications are released. But perhaps they have other tools.
 
There's a button at the top of the Data Sources window that will re-run the configuration wizard and let you make changes. You can also right-click a DataTable in the DataSet designer and tell it to refresh from the database or the like.
 
Thanks for your reply.
There's a button at the top of the Data Sources window that will re-run the configuration wizard and let you make changes.
I tried that (Configure DataSet with Wizard), and it works fine.
In the "Choose Your Database Objects", check newly-added column, uncheck deleted column etc..

In Data Sources, the modified DataSet appears.

But how is the form changed? I've had apparent success doing these things:

If the Table had been dragged in Datagrid mode:
Delete the datagrid, then drag the new dataset.

If the Table had been dragged in Details mode:
To add a column, drag only that column.
When a column has been removed, delete the controls for that column.

You can also right-click a DataTable in the DataSet designer and tell it to refresh from the database or the like.
If you mean "Configure...", it starts Table Adapter Configuration Wizard. In Advanced Options, "Refresh the data table" is checked by default.
I clicked Finish on the Wizard. When completed, the Table did not reflect the modified database. If you mean something else I don't see, please advise.

However, the first option answers my question at this point. Thanks again!
 
But how is the form changed? I've had apparent success doing these things:

If the Table had been dragged in Datagrid mode:
Delete the datagrid, then drag the new dataset.
You can also click 'Add' in DataGridViews Columns dialog and choose from the new available databound columns.
You can also right-click a DataTable in the DataSet designer and tell it to refresh from the database or the like.
Perhaps jmcilhinney meant the 'refresh' option in context menu for dataset/table in Data Sources window.
 
I meant the Configure DataSet with Wizard button at the top of the Data Sources window and right-clicking a DataTable in the DataSet designer and selecting Configure.
 
JohnH,

Thanks, that is a more precise way to do it.
It's not an alternative. You can't bind your grid to a column if it doesn't exist in the DataSet, so you need to update your Data Source first regardless.
 
Yes, you need to update the data source first, then reconfigure the UI with the two options gs99 described or configure columns as I described.
 
I've tried several methods but have problems.
A. If I delete the DataSet from Solution Explorer,
Form1.vb [Design] changes to a special Error Report, beginning with this line:
"To prevent possible data loss before loading the designer, the following errors must be resolved:"
So that doesn't look good.
Indeed, the DataSet.VB file contains code to qhich your form refers. If you delete it, you break the code in the form

B. If I delete objects in other ways, and Add New Data Source, and drag the DataSet again,
The PersonBindingNavigator is not generated.

Probably because it's already on the form

Is there a way to do this when using these "simple" methods?
Try just opening the dataset and using its visual designer, in a similar way to how you open a form and use its visual designer. Right click on stuff, see what the context menu does etc

Note that when you drag things from Data Sources to the form, and e.g. a Grid appears, its COLUMNS collection has been exactly filled in according to what is in the dataset at the time it was dragged - it is disconnected from the shape of the dataset in the same way that the dataset is disconnected from the shape of the database tables..

If you add columns to your database, you have to then add them to the dataset, an dpossibly then, add those columns to grids/forms that will use them etc.
It is this way because a dataset can contain more or fewer columns than in the database table, and the grid can contain more or fewer columns than in the dataset
 
Back
Top