How to Get Value Member in a ListBox Using C# and SQL Server

How to Get Value Member in a ListBox Using C# and SQL Server

valuememberLISTboxFig.3

In this tutorial, I’m going to teach you how to get the value member in a Listbox using C# and SQL Server. Listbox is a control window that contained a list of items where it allows the user to choose one or more items. For instance, if you want to display the list of Books in the Listbox from the database but it’s the id that you only want to display. It’s the time to use the value member for getting the id of the employees in the ListBox.

 

Database : Click here for the database of this project.

Let’s begin:

 

Open Microsoft Visual Studio and create a new windows application for C#. Then, do the form just like this.

valuememberLISTboxFig.1

After setting up the form, go to the solution explorer and hit “code view” to fire the code editor.

ValueMemberSQLfig.2

In the code editor, declare all the classes and variable that are needed.

Note: Put “using System.Data.SqlClient;” above the namespace to access sql server library.

 //initializes new instance 
 SqlConnection con = new SqlConnection();
 SqlDataAdapter da = new SqlDataAdapter();
 SqlCommand cmd = new SqlCommand();
 DataTable dt = new DataTable();
 //declare a variable
 string query;

After that, create a method for filling data in the list control of a ListBox.

 //a method for filling data in the ListBox
 private void LoadBooks(string query, ListBox lst)
 {
 //opening connection
 con.Open();
 try
 {
 //initialize a new instance of sqlcommand
 cmd = new SqlCommand();
 //set a connection used by this instance of sqlcommand
 cmd.Connection = con;
 //set the sql statement to execute at the data source
 cmd.CommandText = query;
 //initialize a new instance of sqlDataAdapter
 da = new SqlDataAdapter();
 //set the sql statement or stored procedure to execute at the data source
 da.SelectCommand = cmd;
 //initialize a new instance of DataTable
 dt = new DataTable();
 //add or resfresh rows in the certain range in the datatable to match those in the data source.
 da.Fill(dt);

 //Get and set the data source of a comboBox
 lst.DataSource = dt;
 //set the field of a table to display in the list control of a combo box
 lst.DisplayMember = "BooksName";
 //'SET THE ACTUAL VALUE OF THE 
 //' ITEMS IN THE LISTBOX
 lst.ValueMember = "ID";
 
 }
 catch (Exception ex)
 {
 //catching error 
 MessageBox.Show(ex.Message);
 }
 //release all resources used by the component
 da.Dispose();
 //closing connection
 con.Close();
 }

Go back to the design view, double-click the form and do the following codes to establish the connection between SQL Server 2005 Express Edition and C#.net.

  //set a connection between SQL server and Visual C#
 con.ConnectionString = "Data Source=.\\SQLEXPRESS;Database=dbbooks;trusted_connection=true;";
 //set up a retrieve query
 query = "Select * From tblbooks";
 LoadBooks(query, listBox1 );//call a load method

Go back to the design view again, click a ListBox and go to properties. In the properties, hit the events, just like a lightning symbol and select click for the events.

valuememberLISTboxFig.2

after that, do the following codes for getting the value member of a ListBox when selected.

 MessageBox.Show("Book Id : " + listBox1.SelectedValue.ToString());

Output:

valuememberLISTboxFig.3

For all students who need a programmer for your thesis system or anyone who needs a source code in any a programming languages. You can contact me @ :

 

 

Email – [email protected]
Mobile No. – 09305235027 – TNT

 

ABOUT PROJECTPROJECT DETAILS
Project Name : How to Get Value Member in a ListBox Using C# and SQL Server
Project Platform :C#
Programming Language Used:C# Programming Language
Developer Name :itsourcecode.com
IDE Tool (Recommended):Visual Studio 2019
Project Type :Desktop Application
Database:MySQL Database
Upload Date and Time:July 4, 2016- 9:35 am

Leave a Comment