How to show related Data in Textbox using C#


Here I will show you how to Show Related Data In Textbox When We Click Related Item In Combobox In Csharp.

Create a table named employee having fileds empid Text, empname Text, sal Number , empaddress Text , phoneno Number in Ms-access.

Method 1:

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 combobox_textbox_data
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        OleDbDataAdapter oledbda;
        DataSet ds;
        string str;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           OleDbConnection con = new OleDbConnection(ConnectionString);
            str = "select * from employee";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            ds = new DataSet();
            oledbda.Fill(ds, "employee");
            comboBox1.DataSource = ds.Tables["employee"];
            comboBox1.DisplayMember = "empid";
            comboBox1.Text = "select";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee where empid='" +  comboBox1.Text.Trim() + "'";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();

            if (reader.Read())
            {
                textBox1.Text = reader["empname"].ToString();
                textBox2.Text = reader["sal"].ToString();
                textBox3.Text = reader["empaddress"].ToString();
                textBox4.Text = reader["phoneno"].ToString();
            }
 
            con.Close();
            reader.Close();
      
           
 
        }
    }
}


Method 2:

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 combobox_textbox_data
{
    public partial class Form2 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        OleDbDataAdapter oledbda;
        DataSet ds;
        string str;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                comboBox1.Items.Add(reader["empid"]);
            }
            reader.Close();
            con.Close();
            comboBox1.Text = "select";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee where empid='" + comboBox1.Text.Trim() + "'";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();

            if (reader.Read())
            {
                textBox1.Text = reader["empname"].ToString();
                textBox2.Text = reader["sal"].ToString();
                textBox3.Text = reader["empaddress"].ToString();
                textBox4.Text = reader["phoneno"].ToString();
            }

            con.Close();
            reader.Close();

        }
    }
}


Method 3:

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 combobox_textbox_data
{
    public partial class Form3 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        OleDbDataAdapter oledbda;
        DataSet ds;
        string str;
        DataTable dt;
        public Form3()
        {
            InitializeComponent();
        } 

        private void Form3_Load(object sender, EventArgs e)
        {
           OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            ds = new DataSet();
            oledbda.Fill(ds, "employee");
            dt = ds.Tables["employee"];
            int i;
            for (i = 0; i <= dt.Rows.Count - 1; i++)
            {
                comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);
            }
            con.Close();
            comboBox1.Text = "select";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from employee where empid='" + comboBox1.Text.Trim() + "'";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();

            if (reader.Read())
            {
                textBox1.Text = reader["empname"].ToString();
                textBox2.Text = reader["sal"].ToString();
                textBox3.Text = reader["empaddress"].ToString();
                textBox4.Text = reader["phoneno"].ToString();
            }

            con.Close();
            reader.Close();
        }
    }
}


Running the program

After running the program when you select any empid from the combobox its corresponding values will be shown.

how to Show Related Data In Textbox

Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all