how to fetch particular value from a table in sql .
I want to fetch Name when clicked on Employee Code .
for eg:
i have list of employee code in a combo Box . and when i select 101 code from list , then i should display Its name.
I
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Krishna GaradRank #191 · 12.8K reputationPosted Apr 26, 2011, 8:09 PM EDT
Select EmployeeName From Table Where EmpolyeeCode=Combobox1.SelectedValue;
Mayur GujrathiRank #666 · 3.6K reputationPosted Apr 26, 2011, 8:33 PM EDT
EmpolyeeCode=Combobox1.SelectedValue;
massagebox.show(ename);
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 26, 2011, 8:40 PM EDT
Generate SelectedValueChanged event for comboBox and then implement below code in this event,
Try this :
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
SqlDataReader dr;
SqlCommand cmd;
//replace your connection
SqlConnection con = new SqlConnection("Data Source=.//SQLEXPRESS;Initial Catalog=DBName;User ID=Admin;Password=Admin");
try
{
con.Open();
string qry = "Select EmpName from table where EmpCode =" + comboBox1.SelectedItem;
cmd = new SqlCommand(qry, con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
MessageBox.Show(dr.GetString(0)); //here u will get the employee name
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
Ankit NandekarRank #703 · 3.4K reputationPosted Apr 26, 2011, 8:55 PM EDT
on ComboBox selected index changed event write this code
SqlConnection con = new SqlConnection("Data Source=C3;Initial Catalog=Books;Persist Security Info=True;User ID=sa;Password=p@ssw0rd");
SqlCommand cmd = new SqlCommand("select EmpName from EmpInfo where EmpId='"+ComboBox1.SelectedItem+"'",con);
con.Open();
string str = Convert.ToString(cmd.ExecuteScalar());
con.Close();
Label1.Text = str;//Here In Label u will get Employee Name
Note:use Namespace using System.Data.SqlClient;