Auto-generate VB.NET forms using SQL Server 2008

11,416

It takes just a minute to fix, all functionality allready exists in Visual Studio.

Fire up Visual Studio, click on Add new datasource... to start the Data Source Configuration Wizard:

enter image description here

Select Database and follow the wizard:
enter image description here

When connected to the database, select the tables you are interrested in and press the Finnish button: enter image description here

Now, this will create a strongly named dataset in your solution, if you double click the xsd file you'll see the tables you selected in the schema editor, but leave that for now: enter image description here

Now, select "Show Data Sources" from the data-menu and you will see all tables you selected in the wizard. To the left of each field its an icon that tells what type of control that field will be represented by on the resulting form:

enter image description here

Now you can deside how the data will be presented on the form, as a datagridview or in detail mode, just use the dropdown on the table name (only when in form-design-mode).

enter image description here

If you have selected details-mode on the table, then you can change what control the field will be represented by (must be in form-design-mode, not code-mode):

enter image description here

Then just drag the table from the data source view to an empty form and it will magically create controls to edit/add/delete and move around the data.

This is the result if DataGridView-mode was selected:

enter image description here

And if Details was selected on the table:

enter image description here

In code behind it also magically add some code to load the data to the adapter when the form loads and some save/validating code:

Private Sub AccountBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AccountBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.AccountBindingSource.EndEdit()
    Me.AccountTableAdapter.Update(Me.MyDBDataSet.Account)

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'MyDBDataSet.Account' table. You can move, or remove it, as needed.
    Me.AccountTableAdapter.Fill(Me.MyDBDataSet.Account)

End Sub
Share:
11,416
Ayub
Author by

Ayub

Updated on June 11, 2022

Comments

  • Ayub
    Ayub almost 2 years

    I want to automatically generate VB.NET forms using tables from a SQL Server database (one form / database table). It is perhaps possible with writing custom custom code for it, but if there is already some feature that does the job that would be great (database has 40+ tables, manually doing this is a tedious task).

    Any answers, help, links, tips is greatly appreciated.

    Regards, Ayub

  • Stefan
    Stefan almost 13 years
    And now when you have a strongly named dataset you are in a new world of possibilities. Open the dataset and add queries to the tableadapter. Use LINQ on the dataset-data. Add relations to the tables (made automatically if the relation allready are in the db) and then be able to use dot-notation to access/change/save related data like this without needing to care about joins and how to save the related data: MydataSet.Customer.Adress.Street="" and so on.
  • Ayub
    Ayub almost 13 years
    Thanks for the detailed answer, seems promising, i will try that out and let you know..
  • Prajwal
    Prajwal about 12 years
    dear @Stefan, could this be done using code? i.e I want to create forms from database tables automatically and I don't want to use bound mode for forms. I would like to insert all operations from my own methods.
  • Ayub
    Ayub over 10 years
    It works Stefan, thanks!, sorry for the extremely late response :( lost password to my account :(