2010-11-29 87 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)) 
    { 
     // ...