0
votes

I have a function like this

/// /// This function binds the emplist drop down for mentor user. ///

    private void BindEmpDropDownForMentor()

    {

        string strSelectMentorQuery = "SELECT FIRST_NAME + ' ' + LAST_NAME AS NAME FROM M_USER_DETAILS MUD INNER JOIN M_LEADERLED MLL "
               + "ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString()
               + "' AND MUD.ACTIVE = 1 AND MLL.START_DATE <= Getdate() AND"
               + " MLL.END_DATE > Getdate()";

        OleDbConnection oleConnection = new OleDbConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
        OleDbCommand oleCommand = new OleDbCommand(strSelectMentorQuery, oleConnection);

        try
        {
            //Open Connection
            oleConnection.Open();

            //Set Datasource and close connection   
            cmbempList.DataSource = oleCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            cmbempList.DataValueField = "";
            cmbempList.DataTextField = "NAME";

            //Bind the Dropdown
            cmbempList.DataBind();

            //Add a new item 'ALL TEAM MEMBERS' to the member list
            cmbempList.Items.Insert(0, new ListItem("ALL TEAM MEMBERS", "0"));
            cmbempList.SelectedIndex = 0;

            GridViewDataShowBy = cmbempList.SelectedValue;

        }
        catch (Exception ex)
        {
            ExceptionLogger.LogException(ex);
        }
        finally
        {
            // Close the connection when done with it.
            oleConnection.Close();
        }

    }

But on selected change event of cmbempList, format exception error is being caught saying this that input string was not in correct form in the bold line below

protected void cmbempList_SelectedIndexChanged(object sender, EventArgs e)

    {

        gvLeaveList.CurrentPageIndex = 0;
        dgDaysAbsent.CurrentPageIndex = 0;

        **if (!(Convert.ToInt32(cmbempList.SelectedValue) > 0))
        {**
            if (this.Session["RoleID"].ToString() == "1")
            {
                cmbLeads.ClearSelection();
                cmbLeads.SelectedIndex = cmbLeads.Items.IndexOf(cmbLeads.Items.FindByValue(this.Session["UserID"].ToString()));
            }
        }

        GridViewDataShowBy = cmbempList.SelectedValue.ToString();

        if (cmbempList.SelectedValue != "0" && cmbempList.SelectedValue != "")
        {
            Page.Title = cmbempList.SelectedItem.Text + " | Leave List | "
                       + OrganizationManager.GetCurrentOrganizationName(Session["OrgID"]);
        }
        else
        {
            Page.Title = "Leave List | "
                       + OrganizationManager.GetCurrentOrganizationName(Session["OrgID"]);
        }

        PopulateLeaveList(GridViewDataShowBy, "0");
        BindLeaveListGrid(GridViewDataShowBy, cmbLeads.SelectedValue.ToString());
    }
2

2 Answers

2
votes

It is because cmbempList's DataValueField is being set to an empty string in the BindEmpDropDownForMentor method.

cmbempList.DataValueField = "";

This will cause cmbempList's values to be bound to the values in the DataTextField which are strings. When the SelectedIndexChange event is called it tries to parse the strings to an Int32 which is throwing the exception.

Convert.ToInt32(cmbempList.SelectedValue) > 0

To fix it you can add an aliased ID field in the SQL query and set the cmbempList.DataValueField to that ID name which is probably your intent.

For example in BindEmpDropDownForMentor make this edit to your query:

string strSelectMentorQuery = "SELECT FIRST_NAME + ' ' + LAST_NAME AS NAME, MLL.LED_ID AS ID FROM M_USER_DETAILS MUD INNER JOIN M_LEADERLED MLL "
               + "ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString()
               + "' AND MUD.ACTIVE = 1 AND MLL.START_DATE <= Getdate() AND"
               + " MLL.END_DATE > Getdate()";

And assign your DataValueField to this:

cmbempList.DataValueField = "ID";

0
votes

try this.

if it still fails look in the debugger what value cmbempList.SelectedValue contains.

protected void cmbempList_SelectedIndexChanged(object sender, EventArgs e)
{
    // ...
    object selectedValue = cmbempList.SelectedValue;
    if ((selectedValue != null) && (selectedValue != DBNull.Value) && (!(Convert.ToInt32(selectedValue) > 0))
    {
        // ...