Click here to Skip to main content
16,011,949 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi
I am using the code given below and what happens is when I run the application I didnt get any value but when i restart i am getting the values correctly and below is the code for binding the dropdown list what happens is when dtLeavetype has values but it is not getting populated correctly

C#
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);
      }
  }
Posted
Updated 18-Jan-12 7:15am
v2

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

C#
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
 
Share this answer
 
I think what you should examine is the difference in process between what happens when you start your application as opposed to when you think you trigger the process from an already running process. It may be that you aren't even trying to retrieve the data from the internal process while the fresh launch does.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900