Thursday 18 October 2012

How to Retrieve data from database to fill Textboxes when selected index is changed in Dropdownlist using Windows Form C#.Net.

To implement this concept you need to follow the below steps :

Step1 :
First you need to design a table in Ms Access 2007 to retrieve the data from database.

Step2:
On the File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step3:
Now open the Form.cs page and write the following source code.

 Form.cs Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;



namespace Sample
{
    public partial class Sample_Example : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\SampleDB.accdb; Jet OLEDB:Database Password=1234;Persist Security Info=False");

        public Sample_Example()
        {
            InitializeComponent();
            ClientBind();
        }

        private void DropdownlistBind()
        {
            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("Select ID, Client_Name from SampleDB_CreateClient", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataRow dr;
            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "---Select Client Name---" };
            dt.Rows.InsertAt(dr, 0);
            cmbClientName.DisplayMember = "Client_Name";
            cmbClientName.ValueMember = "ID";
            cmbClientName.DataSource = dt;
            con.Close();
        }
  

        private void Clear()
        {
            cmbClientName.SelectedValue = 0;
            txtClientName.Text = string.Empty;
            txtContactPerson.Text = string.Empty;
            txtAdress.Text = string.Empty;
            txtContactNo.Text = string.Empty;
            txtEmailId.Text = string.Empty;
        }

        private void cmbClientName_SelectedIndexChanged(object sender, EventArgs e)

        {


            if (cmbClientName.SelectedIndex > 0)
            {
                con.Open();
                int id = Convert.ToInt32(cmbClientName.SelectedValue);
                OleDbCommand cmd = new OleDbCommand("select * from SampleDB_CreateClient where ID = " + id, con);
                cmd.CommandType = CommandType.Text;
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                txtClientName.Text = dt.Rows[0].ItemArray[1].ToString();
                txtContactPerson.Text = dt.Rows[0].ItemArray[2].ToString();
                txtAdress.Text = dt.Rows[0].ItemArray[3].ToString();
                txtContactNo.Text = dt.Rows[0].ItemArray[4].ToString();
                txtEmailId.Text = dt.Rows[0].ItemArray[5].ToString();
                con.Close();
            }
            else
            {
                Clear();

            }
        }
     }
  }



Step4:
Now build the Solution and Debug it for the output.

 

No comments:

Post a Comment