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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Database ADO.net » Data Fill ListView 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.