Fill Data from database table to ListView : Data Fill ListView « Database ADO.net « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Database ADO.net » Data Fill ListViewScreenshots 
Fill Data from database table to ListView

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class Form1 : System.Windows.Forms.Form {
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Button cmdExecute;
   private System.Windows.Forms.TextBox txtSql;
   private System.Windows.Forms.ListView lvwResult;
   private System.ComponentModel.Container components = null;

   public Form1() {
      InitializeComponent();
   }

   private void InitializeComponent() {
      this.label1 = new System.Windows.Forms.Label();
      this.cmdExecute = new System.Windows.Forms.Button();
      this.txtSql = new System.Windows.Forms.TextBox();
      this.lvwResult = new System.Windows.Forms.ListView();
      this.SuspendLayout();

      this.label1.Location = new System.Drawing.Point(00);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(29616);
      this.label1.TabIndex = 0;
      this.label1.Text = "Enter a SQL query or statement and click Execute.";

      this.cmdExecute.Location = new System.Drawing.Point(227224);
      this.cmdExecute.Name = "cmdExecute";
      this.cmdExecute.TabIndex = 1;
      this.cmdExecute.Text = "Execute";
      this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);

      this.txtSql.Location = new System.Drawing.Point(016);
      this.txtSql.Multiline = true;
      this.txtSql.Name = "txtSql";
      this.txtSql.Size = new System.Drawing.Size(528200);
      this.txtSql.TabIndex = 2;
      this.txtSql.Text = "";

      this.lvwResult.GridLines = true;
      this.lvwResult.Location = new System.Drawing.Point(0256);
      this.lvwResult.Name = "lvwResult";
      this.lvwResult.Size = new System.Drawing.Size(528200);
      this.lvwResult.TabIndex = 3;
      this.lvwResult.View = System.Windows.Forms.View.Details;

      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(528452);
      this.Controls.Add(this.lvwResult);
      this.Controls.Add(this.txtSql);
      this.Controls.Add(this.cmdExecute);
      this.Controls.Add(this.label1);
      this.Name = "Form1";
      this.Text = "Query Processor";
      this.ResumeLayout(false);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void cmdExecute_Click(object sender, System.EventArgs e) {
     SqlConnection conn = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");

     try {
       lvwResult.Columns.Clear() ;
       lvwResult.Items.Clear();

       conn.Open();
       txtSql.Text ="select * from Employee";

       SqlCommand cmd = conn.CreateCommand();
       cmd.CommandText = txtSql.Text;

       SqlDataReader dr = cmd.ExecuteReader();

       for (int i = 0; i< dr.FieldCount; i++) {
         ColumnHeader ch = new ColumnHeader();
         ch.Text=dr.GetName(i);
         lvwResult.Columns.Add(ch);
       }

       ListViewItem itmX; 

       while (dr.Read()) {
         itmX=new ListViewItem()
         itmX.Text= dr.GetValue(0).ToString();

         for (int i=; i< dr.FieldCount; i++) {
            itmX.SubItems.Add(dr.GetValue(i).ToString());
         }
         lvwResult.Items.Add(itmX);
       }
       dr.Close();
    catch System.Data.SqlClient.SqlException  ex) {
       Console.WriteLine("There was an error in executing the SQL." +
               "\nError Message:" + ex.Message, "SQL");
    finally {
       conn.Close();
    }
  }
}


           
       
Related examples in the same category
ww_w.__j_a_v___a__2___s_.co_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.