Page 1 of 1

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 Rate Topic: -----

#1 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 396
  • Joined: 02-June 07

Post icon  Posted 02 December 2008 - 09: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.

Is This A Good Question/Topic? 0
  • +

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

#2 Kiljoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 08

Posted 15 January 2009 - 01: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
Was This Post Helpful? 0
  • +
  • -

#3 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 396
  • Joined: 02-June 07

Posted 18 January 2009 - 08:18 AM

View PostKiljoy, on 15 Jan, 2009 - 08:24 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#4 albeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-March 09

Posted 26 March 2009 - 09:13 AM

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
Was This Post Helpful? 0
  • +
  • -

#5 njparton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-July 09

Posted 09 July 2009 - 04:14 AM

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: 09 July 2009 - 04:16 AM

Was This Post Helpful? 0
  • +
  • -

#6 season  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-August 09

Posted 29 August 2009 - 05:35 PM

View Postsharpy, on 2 Dec, 2008 - 08:23 AM, said:

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

Attachment attachment

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.

Attachment attachment

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.

Attachment attachment

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.


Attachment attachment

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.

Attachment attachment

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.

Attachment attachment

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

This post has been edited by season: 29 August 2009 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 season  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-August 09

Posted 29 August 2009 - 05:48 PM

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
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Posted 29 August 2009 - 06:03 PM

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.
Was This Post Helpful? 0
  • +
  • -

#9 season  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-August 09

Posted 30 August 2009 - 04:03 AM

Thanks Adam for the quick response
Season
Was This Post Helpful? 0
  • +
  • -

#10 umepinkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-February 10

Posted 05 February 2010 - 08:55 AM

this is a very nice tutorial. However I have a question with the combo box. I tried all the step and it did show items from combo box to be matched with item in the textbox, but I couldn't select the item from the combo box. It didn't autofill the combo box.
Was This Post Helpful? 0
  • +
  • -

#11 rheign07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-July 07

Posted 27 June 2010 - 08:30 PM

hi sir, I have a question, since the topic is regarding the combo box..,
This is my question . I have populated my combo box through run time dataset code, I've assigned the .ValueMember to a specific database field, also I assigned combo box's .DisplayMember to a specific field also. Now my problem is I can't display the value of .Displaymember to a textbox whenever I choose any item in the said combo box.


To simply my question, I want to display to the textbox the value of combo box's .DisplayMember depending on what item that I choose from that combo box.


thanks very much, I hope you could help me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1