I am supposing;
1. You are loading leave types in a leave management system.
2. The leave types are stored in a look up table in the database
3. For example, when your ApplyForLeave.aspx file or any other file loads up in the browser, you would like to populate the leave types.
For this purpose, the ideal scenario would be
Put a IsPostBack check on page load and then call the function
page Load()
{
if (!isPostBack)
{
bindLeaveStatus();
}
}
private void bindLeaveStatus()
{
try
{
MySqlConnection con = new MySqlConnection(conn);
con.Open();
MySqlDataAdapter daLeaveType = new MySqlDataAdapter("select leave_type_id,leave_type from leave_type", con);
DataTable dtLeavetype = new DataTable();
daLeaveType.Fill(dtLeavetype);
ddlLeaveType.DataTextField = "leave_type";
ddlLeaveType.DataValueField = "leave_type_id";
ddlLeaveType.DataSource = dtLeavetype;
ddlLeaveType.DataBind();
con.Close();
}
catch (Exception ex)
{
throw (ex);
}
}
This will correctly load your drop down list with the leave types every time.
Let me know if this confuses you.
Regards,
Nayan