Click here to Skip to main content
12,954,672 members (67,238 online)
Rate this:
Please Sign up or sign in to vote.
how to display automatically respective name in textbox2 when id is typed in the textbox1?????

where table name : Employee Details
columns : EmployeeID, EmployeeNAME

my windows form looks as follows

Employee ID : textBox1 textBox2

when i type Employee ID in textBox1, it should automatically display the respective name in textBox2 for the ID in typed textBox1.
Posted 18-Feb-13 1:09am
Updated 18-Feb-13 1:25am
Shubh Agrahari 18-Feb-13 7:40am
please more clarify that what actually you want to ask that can help us to help you...
Shubh Agrahari 18-Feb-13 7:42am
now okk by v2
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi. i suppose you use LINQ.
private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
           int id = Convert.ToInt32(TextBox1.Text);
           TextBox2.Text=//you must write :select Name from myTable where myTable.ID==id;

your question isn't obvious.
you must determine your source. SQL table or List<string> ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Your question is not clear but i supposed you are fetching data from Database against a specific Id. you can do this in textChanged Event of the text box.

 // here someFunctionWhichWillreturnNameAgainstId is a function which takes id as parameter and return the name.
private void textBoxId_TextChanged(object sender, EventArgs e)
            string id = textBoxId.Text;
            textName.Text =  someFunctionWhichWillreturnNameAgainstId(id);
    private string someFunctionWhichWillreturnNameAgainstId(string id) {
            string cm = "select EmployeeNAME from Employee Details where  EmployeeID=" + id;
            string constr = "you connection stirng";
            SqlConnection con = new SqlConnection(constr);
            SqlCommand sqlcmd = new SqlCommand(cm,con);
            object name   =  sqlcmd.ExecuteScalar();
            return name.ToString();
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

hi friend...

for this EmployeeID should be not duplicate(primary key in database)
and then the event of text chenge for textbox1 control you have to right the code...

private void textBoxId_TextChanged(object sender, EventArgs e)
      string empname="select EmployeeName from Employee_Details where   EmployeeID='"+textbox.text+"' ";
MySqlCommand cmd = new MySqlCommand(empname, con);
MySqlDataReader dr = cmd.ExecuteReader();

also use Namespace and connection String for SQL Server(if you using this database)
it is achiving that actually you want...
Harsha24 19-Feb-13 0:30am
got it..thank you very much.. How to erase the name when the id is erased?????
Devang Vaja 19-Feb-13 0:41am
on text change event of id if id is "" then name="" You can do like this...
Shubh Agrahari 19-Feb-13 1:04am
ya its ma pleasure and nice job Devang happy to help.........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100