【问题标题】:Format Exception error格式异常错误
【发布时间】:2011-05-17 03:20:56
【问题描述】:

我有这样的功能

/// /// 此函数绑定导师用户的 emplist 下拉列表。 ///

    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();
        }

    }

但是在 cmbempList 的选定更改事件中,捕获到格式异常错误,在下面的粗体行中说明输入字符串的格式不正确

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());
    }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    这是因为 cmbempList 的 DataValueField 在 BindEmpDropDownForMentor 方法中被设置为空字符串。

    cmbempList.DataValueField = "";
    

    这将导致 cmbempList 的值绑定到 DataTextField 中的字符串值。当调用 SelectedIndexChange 事件时,它会尝试将字符串解析为引发异常的 Int32。

    Convert.ToInt32(cmbempList.SelectedValue) > 0
    

    要解决此问题,您可以在 SQL 查询中添加别名 ID 字段,并将 cmbempList.DataValueField 设置为可能是您的意图的 ID 名称。

    例如,在 BindEmpDropDownForMentor 中对您的查询进行此编辑:

    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()";
    

    并将您的 DataValueField 分配给此:

    cmbempList.DataValueField = "ID";

    【讨论】:

      【解决方案2】:

      试试这个。

      如果仍然失败,请在调试器中查看 cmbempList.SelectedValue 包含的值。

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

      【讨论】:

        猜你喜欢
        • 2017-01-01
        • 2014-08-16
        • 1970-01-01
        • 2017-08-05
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 2012-02-22
        • 2014-07-06
        相关资源
        最近更新 更多