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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ankit NandekarPosted Apr 27, 2011, 12:55 AM
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;
Soft CornerPosted Apr 27, 2011, 12:40 AM
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();
}
}
Mayur GujrathiPosted Apr 27, 2011, 12:33 AM
EmpolyeeCode=Combobox1.SelectedValue;
massagebox.show(ename);
Krishna GaradPosted Apr 27, 2011, 12:09 AM
Select EmployeeName From Table Where EmpolyeeCode=Combobox1.SelectedValue;