Click here to Skip to main content
15,886,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 18-Feb-13 1:25am
v2
Comments
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

Hi. i suppose you use LINQ.
C#
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> ?
 
Share this answer
 
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.



C#
 // 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();

            
        }
 
Share this answer
 
v2
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...

C#
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
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900