ARTICLE

How to show related Data in Textbox using C#

Posted by Satyapriya Nayak Articles | Windows Phone 8 August 01, 2011
Here I will show you how to Show Related Data In Textbox When We Click Related Item In Combobox In Csharp.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
post comment
     

in string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];what is dsn???

Posted by namelyia shamshul Dec 25, 2012

Thanks

Posted by Satyapriya Nayak Aug 01, 2011

Useful Article.......

Posted by Angelina Erin Aug 01, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter