ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Load Data in the ListView Using C# and SQL Server

loadataListviewSQLFig.3

In this tutorial, I will teach you how to load data in the ListView using C# and SQL Server 2005. Displaying data in the ListView is a little bit the same with the DataGridView. The only difference is, you need to add items in displyaing data in the ListView, while in the DataGridView, you have to set the data source of it.

 

So, lets get started:

 

Create a database and name it “testdb”.

After creating database, do the following query for creating a table in the database that you have created.

/****** Object: Table [dbo].[tblperson] Script Date: 06/14/2016 15:29:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblperson](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Name] [nvarchar](50) NULL,
 [Address] [nvarchar](50) NULL,
 [Contact] [nvarchar](50) NULL,
 [Emailadd] [nvarchar](50) NULL,
 CONSTRAINT [PK_tblemployee] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

 

Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. Then do the following design of a Form as shown below.

loadataListviewSQLFig.1

 

Go to the Solution Explorer, double click the “View Code”  to display the code editor.

loadataListviewSQLFig.2

 

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

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

 //initialize all classes
 //initializes new instance 
 SqlConnection con = new SqlConnection();
 SqlDataAdapter da = new SqlDataAdapter();
 SqlCommand cmd = new SqlCommand(); 
 DataTable dt = new DataTable();

 

After declaring the classes, go back to the design view double click the form and establish a connection between SQL server and C#.net.

private void Form1_Load(object sender, EventArgs e)
 {
 //set a connection between SQL server and Visual C#
 con.ConnectionString = "Data Source=.\\SQLEXPRESS;Database=testdb;trusted_connection=true;";
 }

After establishing the connection, go back to the design view double click the button and do the following codes for retrieving data in the database that will display in the ListView.

private void button1_Click(object sender, EventArgs e)
 { 
 
 //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 = "Select * FROM tblperson"; 
 
 //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);
 //set the data source to display the data in the listview
 listView1.Items.Clear();
 foreach (DataRow r in dt.Rows)
 {
 var list = listView1.Items.Add(r.Field <int>(0).ToString());
 list.SubItems.Add(r.Field<string >(1));
 list.SubItems.Add(r.Field<string >(2));
 list.SubItems.Add(r.Field<string >(3));
 list.SubItems.Add(r.Field<string >(4));
 }
 
 }
 catch (Exception ex)
 {
 //catching error 
 MessageBox.Show(ex.Message);
 }
 //release all resources used by the component
 da.Dispose();
 //dr.Close();
 //clossing connection
 con.Close();
 }

 

Output:

loadataListviewSQLFig.3

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

 
Email – jannopalacios@gmail.com
Mobile No. – 09305235027 – tnt

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!