ASp.net

Thursday, September 10, 2015

How to Link List Box with Database in C#

1 comment:

  1. using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace WindowsFormsApplication2
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    try
    {
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=I:\C3 soft\WindowsFormsApplication2\WindowsFormsApplication2\MTechSqlDatabase.mdf;Integrated Security=True;Connect Timeout=30");
    con.Open();
    string qry = string.Format("Select UnitType From UnitType"); // create Query
    SqlCommand cmd = new SqlCommand(qry , con);
    SqlDataReader MyReader;
    MyReader = cmd.ExecuteReader();
    while (MyReader.Read())
    {
    listBox1.Items.Add(MyReader[0]);
    }
    }
    catch (Exception ex)
    {

    MessageBox.Show(ex.Message);
    }
    }
    }
    }

    ReplyDelete

MM Online Express | Best product Review and Online Marketing site

MM Online Express | Best product Review and Online Marketing site