I need help. I have a search button and 2 textboxes.Textbox1 should diplay the employee id when the search button is clicked (and it works fine).But I need to automatically see the employee name in textbox2, based on the employee id on textbox1.I'd really appreciate your help. Below is my code:

conn = New SqlConnection("Data Source=MAPOUNDS\SQLEXPRESS;Initial Catalog=HRManagementSystem;Integrated Security=True")
conn.Open()
selectQuery = "SELECT * FROM EmployeeRegistration WHERE EmpID Like '" + txtEmpId.Text + "' AND FullName = '" & txtEmpName.Text & "'"
cmd = New SqlCommand(selectQuery, conn)
dr = cmd.ExecuteReader
If dr.Read Then
    txtEmpName.Text = txtEmpId.SelectedText("textbox2 should display employee name based on emp id entered on textbox1").

You already have the employee ID in txtEmpId and the employee name in txtEmpName before you click SEARCH. What am I missing?

When i put the employee id and click on the search button, txtEmpid shows the emp id in the textbox. What i need is for txtEmpName to automatically show the Name of the matching EmpId in txtEmpName whenever the txtEmpId is shown.

Your query is

"SELECT * FROM EmployeeRegistration WHERE EmpID Like '" + txtEmpId.Text + "' AND FullName = '" & txtEmpName.Text & "'"

so you are going to select based on an employee ID and full name that you already have displayed. If you already have the full name why do you need to redisplay it from the database?

Ok i modified the code and its like:
selectQuery = "SELECT EmpID FROM EmployeeRegistration WHERE EmpID Like '" + txtEmpId.Text + "'"
txtEmpId should return correct id from database and txtEmpName should automatically show the matching Employee Name.

In that case try

If dr.Read Then
    txtEmpName.Text = dr("FullName")

Keep in mind that because you are doing a "Like" in the select query you may end up with more than one matching record (depending on whether txtEmpId.Text contains wildcards)

I tried the code and it only displays "FullName" in a dialog box where else i need the name in a textbox

Sorry. Didn't check the modified query. You have to also select Fullname, not just EmpID. You can only reference columns that you have selected.

selectQuery = "SELECT EmpID, FullName...

I still can't populate txtFullName, im not sure whether im doing something wrong

Have you tried stepping through the code in the debugger?

Textbox.ExecuteScalar works
Thnks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.