Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server-2008
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 2:09am
Edited 18-Feb-13 2:25am
v2
Comments
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
good
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 ?
  Permalink  
Rate this: bad
good
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();
            con.Close();
            return name.ToString();
 
            
        }
  Permalink  
v2
Rate this: bad
good
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+"' ";
con.open()
MySqlCommand cmd = new MySqlCommand(empname, con);
MySqlDataReader dr = cmd.ExecuteReader();
if(dr.read)
{
textbox2.text=dr["EmployeeName"].tostring;
}
con.close();
        }
 
also use Namespace and connection String for SQL Server(if you using this database)
it is achiving that actually you want...
thanks
  Permalink  
Comments
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 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


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