School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,456 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,636 people online right now. Registration is fast and FREE... Join Now!




Add a Dataset to a Combo box to populate Textboxes VB.net Express

 
Reply to this topicStart new topic

> Add a Dataset to a Combo box to populate Textboxes VB.net Express, Populate textboxes from a database with only one line of code Using a

sharpy
Group Icon



post 2 Dec, 2008 - 08:23 AM
Post #1


Hi All

In this tutorial I will show you how to populate text boxes with a dataset by selecting an object from a Combo Box. With this method you will not have to write any code. I Have used VB.net Express in conjunction with MSSQL express 2005.

1. Open the VB express IDE and select File and New project. Select Windows forms Application and name it MyComboDB press OK. You should now have form1 in your IDE.

2. Next we have to add a database to the project. Right click on MyComboDB and select Add then New Item. Select Serviced Based Database and then Add. You should now have a database object in the solutions explorer and also the Database explorer window on the left hand side.

Note: The Data source window is activated at this point just press cancel

Attached Image

3. Expand the Database1.mdf and then right click the Tables folder, select Add new Table. Add this data

Column , Data Type, Allow Nulls

[ModelID], int, No, Set this as Primary Key
[Make], nchar(10), No
[EngineSize], nchar(10), No
[NumDoors], nchar(10), No

To set modelID as the primary key select the modelID row then press the small gold key on the main menu strip. To set the increments scroll down the column properties tabbed window till you see the identity Specification, expand this then click into the combo box next to Isidentity and select Yes, the identity seed and increment should be set to 1 which is ok. Save the table and call it Models.

Attached Image

4. Expand the table’s folder in the Database explorer window and right click on our Models table select show table data and add this information to the appropriate columns.

Note: The ModelID number will be automatically created

Make, EngineType , NumDoors

Ford, 2000, 5
Mustang, 4000, 2
Nissan, 1000, 4

Save and close.

Attached Image

5. We next have to add a dataset to the project in the solutions explorer. Right click on myComboDB and select Add then New Item. Select Dataset and then Add. You should now have a dataset in the solutions explorer and the Dataset1 tab will be open.

6. We need to add the Models table to the Dataset, we do this by selecting the Models table in the data connections window and drag the table onto the Dataset1 tabbed page. You should now have a Models Table Adaptor in the Dataset with an SQL fill query at the bottom of the table which has been automatically generated. Save the Dataset.


Attached Image

7. Select form1 and drag a Combo box onto it from the toolbox

8. We now need to add the Dataset1 to the combo box and the form. In the solutions explorer open the Data sources window. Expand the Dataset1 to show the Models table, highlight the Models table and from the drop down menu select Details. Now highlight the Models table and drag onto the form1 and drop it on to the Combo box be careful not drop it on the form.

Attached Image

8. Repeat this process only this time drop the Models table onto the form. You will see appear automatically textboxes and labels on the form. You now have a working database. Run the application and select a model from the combo box, the text boxes on the form should now update automatically. If you look at the form1 code you will see that only one line of code has been added. If you so wish you could add a button to this form linking to a second form (Form2). If you drag the Models dataset onto form2 this will give you the ability to add delete & save the records which will automatically add or delete records from the Combo box. If you do this you will need to add a refresh button to Form1 to update the data, but I will let you figure this all of this out.

Attached Image

Thanks for your time and I hope this tutorial has been of interest to you.

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Kiljoy
*



post 15 Jan, 2009 - 12:24 AM
Post #2
Hi Sharpy.

Is there anyway to modify the table, for instance add on a section called "fuel Capacity" withouth deleting the dataset and the models that get added to the "form1"


Kind Regards
Kiljoy
Go to the top of the page
+Quote Post

sharpy
Group Icon



post 18 Jan, 2009 - 07:18 AM
Post #3
QUOTE(Kiljoy @ 15 Jan, 2009 - 08:24 AM) *

Hi Sharpy.

Is there anyway to modify the table, for instance add on a section called "fuel Capacity" withouth deleting the dataset and the models that get added to the "form1"


Kind Regards
Kiljoy


Hi Kiljoy

Yes it is quite simple to do

Open up the database in the database explorer and expand the table folder. Select the table and right click. Select open table definition and add a new column then save.

Next

In the data sources window right click on the dataset and select configure dataset with wizard. Expand the dataset table and tick the added column and press finish.

Next

Select the models table, in the dropdown menu select details. Expand the table and drag the newly created column onto the form and that should be it

Hope it all makes sense if not I think you will get the idea

Sharpy
Go to the top of the page
+Quote Post

albeach
*



post 26 Mar, 2009 - 08:13 AM
Post #4
Thank you very much for this tutorial, it was exactly what I had been looking for, especially the autoloading of the items into the combobox list.

Following your procedure, modified it to work with Northwind.mdf for practice, & it was so easy to pick & choose fields from the DB.

Two questions though:

Why is it necessary to add new item [ service-based database] and then a separate dataset?

Where did you learn how to do this?

I plan to try a similar approach to an autocomplete textbox to see if this approach will work too.

Thanks again,
Albeach
Go to the top of the page
+Quote Post

njparton
*



post 9 Jul, 2009 - 03:14 AM
Post #5
This is just what I've been looking for too. However, I'd like to apply this method to a database that holds contact details. The combobox in my case will show available company names, and there will be several contacts at each company (equivalent to several makes for each modelID in the example above).

How to I go about showing a unique list of company names in the combobox (i.e. unique modelIDs), and then only the contact details of the people working for that company in the textboxes below (i.e. makes), along with a way to navigate through them (without impacting the combobox selection)?

Many thanks

This post has been edited by njparton: 9 Jul, 2009 - 03:16 AM
Go to the top of the page
+Quote Post

season
*



post 29 Aug, 2009 - 04:35 PM
Post #6
QUOTE(sharpy @ 2 Dec, 2008 - 08:23 AM) *

Hi All

In this tutorial I will show you how to populate text boxes with a dataset by selecting an object from a Combo Box. With this method you will not have to write any code. I Have used VB.net Express in conjunction with MSSQL express 2005.

1. Open the VB express IDE and select File and New project. Select Windows forms Application and name it MyComboDB press OK. You should now have form1 in your IDE.

2. Next we have to add a database to the project. Right click on MyComboDB and select Add then New Item. Select Serviced Based Database and then Add. You should now have a database object in the solutions explorer and also the Database explorer window on the left hand side.

Note: The Data source window is activated at this point just press cancel

Attached Image

3. Expand the Database1.mdf and then right click the Tables folder, select Add new Table. Add this data

Column , Data Type, Allow Nulls

[ModelID], int, No, Set this as Primary Key
[Make], nchar(10), No
[EngineSize], nchar(10), No
[NumDoors], nchar(10), No

To set modelID as the primary key select the modelID row then press the small gold key on the main menu strip. To set the increments scroll down the column properties tabbed window till you see the identity Specification, expand this then click into the combo box next to Isidentity and select Yes, the identity seed and increment should be set to 1 which is ok. Save the table and call it Models.

Attached Image

4. Expand the table’s folder in the Database explorer window and right click on our Models table select show table data and add this information to the appropriate columns.

Note: The ModelID number will be automatically created

Make, EngineType , NumDoors

Ford, 2000, 5
Mustang, 4000, 2
Nissan, 1000, 4

Save and close.

Attached Image

5. We next have to add a dataset to the project in the solutions explorer. Right click on myComboDB and select Add then New Item. Select Dataset and then Add. You should now have a dataset in the solutions explorer and the Dataset1 tab will be open.

6. We need to add the Models table to the Dataset, we do this by selecting the Models table in the data connections window and drag the table onto the Dataset1 tabbed page. You should now have a Models Table Adaptor in the Dataset with an SQL fill query at the bottom of the table which has been automatically generated. Save the Dataset.


Attached Image

7. Select form1 and drag a Combo box onto it from the toolbox

8. We now need to add the Dataset1 to the combo box and the form. In the solutions explorer open the Data sources window. Expand the Dataset1 to show the Models table, highlight the Models table and from the drop down menu select Details. Now highlight the Models table and drag onto the form1 and drop it on to the Combo box be careful not drop it on the form.

Attached Image

8. Repeat this process only this time drop the Models table onto the form. You will see appear automatically textboxes and labels on the form. You now have a working database. Run the application and select a model from the combo box, the text boxes on the form should now update automatically. If you look at the form1 code you will see that only one line of code has been added. If you so wish you could add a button to this form linking to a second form (Form2). If you drag the Models dataset onto form2 this will give you the ability to add delete & save the records which will automatically add or delete records from the Combo box. If you do this you will need to add a refresh button to Form1 to update the data, but I will let you figure this all of this out.

Attached Image

Thanks for your time and I hope this tutorial has been of interest to you.


This post has been edited by season: 29 Aug, 2009 - 04:46 PM
Go to the top of the page
+Quote Post

season
*



post 29 Aug, 2009 - 04:48 PM
Post #7
Hi Sharpy,
Thank you for your lesson. I am a beginner and followed your instructions and made the form working. Would you please give lessons for how to update the database with any changes. I made a small program to add , delete , update (Edit current Records) . But the problem is I couldn't update the database with the changes from the form filled by the combobox. How can I commit the changes to the database and refresh the dataset with the most current database data. I appreciate your reply.
Andrea
Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 29 Aug, 2009 - 05:03 PM
Post #8
Season,
I'm doing the preparation work for a tutorial covering just that (UPDATE, DELETE, INSERT, SELECT) hopefully it'll up in the next couple of days.
Go to the top of the page
+Quote Post

season
*



post 30 Aug, 2009 - 03:03 AM
Post #9
Thanks Adam for the quick response
Season
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 02:04AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month