开发者

sd display the details of selected item in the dropdown list from database

开发者 https://www.devze.com 2023-01-13 14:49 出处:网络
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 开发者_JAVA技巧

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 开发者_JAVA技巧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.


Dropdownlist databinding

  • check whether databinding of dropdownlist employees dlstemps is done inside !Page.IsPostBack if block.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号