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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.