0

I have table named tbl_Employee with columns Employee_name,Emp_ID,Emp_Salary,Emp_State,Emp_Mobile Number.

I have an aspx page where I used a dropdownlist and binded with the dtabase which when we click we get all Employee name from that dropdownlist.

Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT EmpId, Name FROM tblEmployee"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddlEmployee.DataSource = cmd.ExecuteReader();
                ddlEmployee.DataTextField = "Name";
                ddlEmployee.DataValueField = "EmpId";
                ddlEmployee.DataBind();
                con.Close();

            }
        }
        ddlEmployee.Items.Insert(0, new ListItem("--Select Employee--", "0"));
    }
}

On the same page I have 3 textbox named txtname, txtMobile, and txtState. So I want to bind the value from database to those textbox on selection of particular employee to that drop downlist.

For Ex. If have selected Empname 'ABC' those 3 textboxes should be bind with the value of ABC name, ABC mobile and his State. How do I do that? Do I have to use Jquery or javascript?

1
  • 1
    @James Z Thanks for correcting!!! Commented Jan 13, 2018 at 12:33

1 Answer 1

1

You want to show employee details while ddlEmployee select change, so suppose you have ddlEmployee like :

<asp:DropDownList ID="ddlEmployee" runat="server" AutoPostBack="True" 
                onselectedindexchanged="ddlEmployee_SelectedIndexChanged">
 </asp:DropDownList> 

So it be done in onselectedindexchanged of your DropDown Like:

protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Mobile,Name,State FROM tblEmployee where EmpId= @empId"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                cmd.Parameters.AddWithValue("@empId", ddlEmployee.SelectedValue);
                SqlDataReader reader = cmd.ExecuteReader();
                if(reader.Read())
                {
                    lblName.Text = reader["Name"].ToString();
                    lblEmail.Text = reader["Mobile"].ToString();
                    lblState.Text = reader["State"].ToString();
                }
                con.Close();
            }
        }
    }
    catch (Exception s)
    {
        HttpContext.Current.Response.Write("Error Occured " + s.Message);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.