I would like to know, what is the correct way to bind data on a drop down list from a database using stored procedure and C#. I have a drop down list ddlEmployees and an Employee table which has a column of empFname aswell as a stored procedure selectEmployeePaycheck which has a parameter of @fname, that matches the value in the Employee table which has a column of empFname. I'm using the code in the bottom to bind the data from database, but unfortunately it doesn't work.
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter adr;
DataTable dt;
string conStr;
conStr = ConfigurationManager.ConnectionStrings["payRollConnection"].ConnectionString;
con = new SqlConnection(conStr);
con.Open();
cmd = new SqlCommand("selectEmployeePayCheck", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fname", ddlEmployees.SelectedValue);
adr = new SqlAdapter(cmd);
dt = new DataTable();
adr.Fill(dt);
cmd.ExecuteReader();
ddlEmployees.DataTextField = "empFname";
ddlEmployees.DataValueField = "empFname";
ddlEmployees.DataSource = dt;
ddlEmployees.DataBind();
cmd.Dispose();
con.Close();
Any suggestions, corrections, answers are very well accepted.
cmd.Parameters.AddWithValue("@fname", ddlEmployees.SelectedValue);You're passing FName as unique value before binding any data toddlEmployees?