2010-11-29 90 views
0

我有这样格式异常错误

/// ///这个函数的函数结合的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的选择更改事件,被捕捞说这是输入字符串的不正确的形式在下面

粗线保护无效cmbempList_SelectedIndexChanged(对象发件人格式异常错误,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

这是因为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";

0

试试这个。

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

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