views:

12

answers:

1

This is the code i have written to display the details of employee selected from the drop down list into textboxes. I didn't get any error but the details are not getting displayed in the textboxes...

protected void  dlstemps_SelectedIndexChanged(object sender, EventArgs e)
{
    int empno=Convert.ToInt32(dlstemps.SelectedItem.Value);

    SqlConnection con = new SqlConnection();
    con.ConnectionString=constr;
    SqlCommand cmd=new SqlCommand();
    cmd.CommandText = "select * from emp where empno="+empno;
    cmd.Connection = con;
    SqlDataReader reader;
    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        reader.Read();
            txtempno.Text = reader["empno"].ToString();
            txtename.Text = reader["ename"].ToString();
            txtsal.Text = reader["sal"].ToString();
            txtdeptno.Text = reader["deptno"].ToString();
            txtadress.Text = reader["adress"].ToString();
       reader.Close();
    }
    catch(Exception er)
    {
        lblerror.Text=er.Message;
    }
    finally
    {
        con.Close();
    }
}

I don't understand what went wrong in this code.... Please help me to fix it.

A: 

Dropdownlist databinding

  • check whether databinding of dropdownlist employees dlstemps is done inside !Page.IsPostBack if block.
Sandy
@sandy i am able to get the names of employees in the drop down list,but when i select the name i am unable to get their details.if databinding is not done in if block i don think i can get the names of employes in the drop down list
sowmya
@sowmya - I think you should debug the code and place breakpoint where you selects dropdownlist value; then check whether empno is correct
Sandy
@sandy i dont understand how to do...i have set the break point at cmd.command text and debuged it. i am unable to get any value for empno during debuging. is this the way how to debug..?
sowmya
@sowmya - this is a serious problem! as an asp.net developer, you have to learn how to debug! these resources will help you to do that [debugging with breakpoints](http://www.beansoftware.com/ASP.NET-Tutorials/debuging-Breakpoints.aspx), [debuggin and tracing](http://www.asp.net/general/videos/lesson-5-debugging-and-tracing-your-website)
Sandy