Auto-generate VB.NET forms using SQL Server 2008
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:
Select Database and follow the wizard:
When connected to the database, select the tables you are interrested in and press the Finnish button:
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:
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:
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).
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):
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:
And if Details was selected on the table:
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
Comments
-
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 almost 13 yearsAnd 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 almost 13 yearsThanks for the detailed answer, seems promising, i will try that out and let you know..
-
Prajwal about 12 yearsdear @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 over 10 yearsIt works Stefan, thanks!, sorry for the extremely late response :( lost password to my account :(