Display Database Records in ListView using C#
Submitted by donbermoy on Tuesday, November 17, 2015 - 10:33.
In this tutorial, I will teach you how to create a program that will load records to a combobox from a SQL Server 2008 database using c#. This will be very helpful in making your systems or thesis.
So, now let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application.
2. Add only one ListView in your Form. In its Properties, have its Full Row Select and GridLines to True, the View into Detals, and Add labels such as ID, Username, and Password into the Column Names.
3. We will first create a database named sampleDatabase using the SQL Server 2008 database.
Create a table named tblLogin and have an entity of ID as int datatype, and username and password having varchar datatype.
Then put records inside the table and we will retrieve and retrieve this later in our ListView.
4. Now, let's do the coding.
We will first import the SQL namespace as we will have the SQL database.
Have the connection string of your sql database, the command, connection, and the data reader.
And then call this viewList() function inside the Form_Load with the connection of your database to open it.
.
Output:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware



- using System.Data.SqlClient;
Create a function named viewList() that will load the records from the database to the combobox. Use the table named tblLogin.
- void viewList()
- {
- listView1.Items.Clear();
- string sql = "select * from tblLogin";
- dr = cm.ExecuteReader();
- while (dr.Read())
- {
- list = listView1.Items.Add(dr.GetValue(0).ToString());
- list.SubItems.Add(dr.GetValue(2).ToString());
- list.SubItems.Add(dr.GetValue(1).ToString());
- }
- dr.Close();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- cn.Open();
- viewList();
- }

Add new comment
- 403 views