Click here to Skip to main content
11,804,008 members (78,548 online)
Rate this: bad
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
Edited 18-Feb-13 1:25am
Shubh Agrahari at 18-Feb-13 7:40am
please more clarify that what actually you want to ask that can help us to help you...
Shubh Agrahari at 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 ?
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 at 19-Feb-13 0:30am
got it..thank you very much.. How to erase the name when the id is erased?????
Devang Vaja at 19-Feb-13 0:41am
on text change event of id if id is "" then name="" You can do like this...
Shubh Agrahari at 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
0 OriginalGriff 410
1 CPallini 310
2 Sergey Alexandrovich Kryukov 234
3 CHill60 230
4 Suvendu Shekhar Giri 200
0 OriginalGriff 3,140
1 Maciej Los 1,910
2 KrunalRohit 1,907
3 CPallini 1,845
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
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