Click here to Skip to main content
13,405,825 members (55,847 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 19 Oct 2004

Populate data from database in a ComboBox

, 19 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Populate data from database in a ComboBox.


This article is the second way (version) of "how to populate data in a ComboBox" program. For doing that, I make use of 'TableMapping' and 'DataViewManager' that I'll explain later in "Code and How it works".

The application allows you to select a StudentID in a ComboBox and displays the columns StudentID, Student Subject and Student Name from the table in the three TextBoxes.

It shows also how to get connected to a MS Access database which you can also find in the project included (sudentDB.mdb). I chose MS Access database because not many people have SQL-Server running.


Code and How it works

Before starting, a short explanation of what TableMapping and DataViewManager means:

TableMapping is the process that controls how data adapters copy tables and columns of data from a physical data source to ADO.NET in-memory objects. When a data adapter reads data from a data source, it determines where to put the data in the corresponding DataSet table (or tables) using a table mapping. If you create a mapping in a data adapter, it allows you to establish a correspondence between columns in the data source and columns in a DataSet table.

A DataAdapter contains a collection of DataTableMapping objects in its TableMappings property. You can pass the DataTableMapping name in place of the DataTable name to the Fill method of the DataAdapter.

The following example creates a DataTableMapping named "MyStudentMappings" for the table "studentTable".

dAdapter.TableMappings.Add("MyStudentMappings", "studentTable");

When you call the Fill method of the DataAdapter and do not specify a TableName or DataTableMapping name, the DataAdapter looks for a DataTableMapping called "Table". If you leave out that DataTableMapping, TableName of the DataTable will be "Table". This is the default DataTableMapping.

For example:

dAdapter.TableMappings.Add("Table", "studentTable");

If the SELECT command creates a result set with a default name of Table, then its contents go into a new or existing DataTable object called "studentTable". The DataTableMapping object describes a mapped relationship between a SQL-result set and a DataTable object in a DataSet.

The DataViewManager represents a view onto an entire DataSet and represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation, whereas the DataView class acts as a view onto a single DataTable.

A DataViewManager is an object that contains a collection of data views. The DataViewManager returned by the DefaultViewManager property allows you to create custom settings for each DataTable in the DataSet. When you want to bind a control to more than one table of a DataSet, binding to a DataViewManager is the ideal choice. When only a single DataTable, then use DataView. When binding a DataSet, .NET automatically uses the corresponding DataViewManager provided through the DataSet.DefaultViewManager property:

For example:


Here is the code for the method fnGetConnectedToDatabase():

private void fnGetConnectedToDatabase() 
  //Connection string
  string conStr = 
    "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=..\\..\\studentDB.mdb";
    //Instantiate OleDbConnection and open the connection to the database
    myConn = new OleDbConnection(conStr);
  catch(OleDbException ex)
    //get the error message if connection failed
    MessageBox.Show("Error in connection ..."+ex.Message);

  string sqlStr ="SELECT * FROM studentTable;";
  //Instantiate a DataAdapter by passing the sqlStr and myConn.
  //now data is in raw form
  dAdapter = new OleDbDataAdapter(sqlStr,myConn);
  //Instantiate a DataSet
  dset = new DataSet();
  //Gets a collection that provides the master mapping
  // between a source table and a DataTable
  dAdapter.TableMappings.Add("Table", "studentTable");

  //A data adapter object utilizes the Fill method to populate 
  //a DataSet or a DataTable object with
  //data retrieved by a SELECT command.
  //When binding a DataSet, .NET automatically uses the corresponding
  //DataViewManager provided through the DataSet.DefaultViewManager property
  //display "studentTable.StudentID" in the ComboBox
  //DataBinding for the TextBox controls
  this.textBox1.DataBindings.Add("Text", this.dviewmanager, 
  this.textBox2.DataBindings.Add("Text", this.dviewmanager, 
  this.textBox3.DataBindings.Add("Text", this.dviewmanager, 
  // Close the connection to the database.

As soon as you select a StudentID from the ComboBox, the columns StudentID, StudentSubject and StudentName in the table "studentTable" are displayed in the three TextBoxes.


There are of course many ways to get data into a ComboBox control. I just tried to show a different way.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Huseyin Altindag
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionBind combobox with data from table in mdb database Pin
Francis Donald Dowuona21-Oct-16 18:28
memberFrancis Donald Dowuona21-Oct-16 18:28 
Question[My vote of 2] Good but the sample is incomplete... Pin
Matjaž Grahek1-Sep-12 1:14
memberMatjaž Grahek1-Sep-12 1:14 
QuestionHave error message "Child list for field Client_table cannot be created" Pin
Member 331042518-Aug-12 1:51
memberMember 331042518-Aug-12 1:51 
AnswerRe: Have error message "Child list for field Client_table cannot be created" Pin
Huseyin Altindag19-Aug-12 2:43
memberHuseyin Altindag19-Aug-12 2:43 
GeneralRe: Have error message "Child list for field Client_table cannot be created" Pin
Member 331042520-Aug-12 4:21
memberMember 331042520-Aug-12 4:21 
Questioniam geeting error on dviewmanager??? Pin
vinayak chavan12-Jun-12 21:19
membervinayak chavan12-Jun-12 21:19 
AnswerRe: iam geeting error on dviewmanager??? Pin
Huseyin Altindag13-Jun-12 0:18
memberHuseyin Altindag13-Jun-12 0:18 
GeneralRe: iam geeting error on dviewmanager??? Pin
vinayak chavan13-Jun-12 0:21
membervinayak chavan13-Jun-12 0:21 
GeneralRe: iam geeting error on dviewmanager??? Pin
Huseyin Altindag13-Jun-12 1:02
memberHuseyin Altindag13-Jun-12 1:02 
GeneralRe: iam geeting error on dviewmanager??? Pin
vinayak chavan14-Jun-12 0:12
membervinayak chavan14-Jun-12 0:12 
GeneralMy vote of 5 Pin
jerry terragnoli3-Dec-10 4:47
memberjerry terragnoli3-Dec-10 4:47 
QuestionVisual C# 2008 express edition and SQL Server 2005 Pin
aceraspire4736z28-Sep-10 19:40
memberaceraspire4736z28-Sep-10 19:40 
AnswerRe: Visual C# 2008 express edition and SQL Server 2005 Pin
Huseyin Altindag29-Sep-10 11:03
memberHuseyin Altindag29-Sep-10 11:03 
GeneralCan't update changes to database Pin
dragon_n_me2-Jan-09 1:25
memberdragon_n_me2-Jan-09 1:25 
GeneralProblems with populating from combo box! [modified] Pin
Nataliya5-Apr-08 1:13
memberNataliya5-Apr-08 1:13 
GeneralRe: Problems with populating from combo box! Pin
Huseyin Altindag5-Apr-08 4:27
memberHuseyin Altindag5-Apr-08 4:27 
GeneralRe: Problems with populating from combo box! Pin
Nataliya5-Apr-08 7:47
memberNataliya5-Apr-08 7:47 
Generalinsert "Select a StudentID" row Pin
[SK]SpooK12-Mar-08 22:29
member[SK]SpooK12-Mar-08 22:29 
Generalmultiple columns combobox Pin
AnailizeR5-May-06 3:10
memberAnailizeR5-May-06 3:10 
GeneralComments on Huseyin Altindag article Pin
padmavijay1-Feb-05 0:22
susspadmavijay1-Feb-05 0:22 
GeneralRe: Comments on Huseyin Altindag article Pin
Huseyin Altindag5-Feb-05 1:45
memberHuseyin Altindag5-Feb-05 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 19 Oct 2004
Article Copyright 2004 by Huseyin Altindag
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid