2012-04-13 58 views
-3

好吧,我有整個事情工作,直到我休息後到達部分;此時,If表示檢測到無法訪問的代碼,並且if(Session [「UserType」] = 1)給出一個錯誤,表示不能將類型對象隱式轉換爲類型bool。對於如何解決這個問題,有任何的建議嗎?以下是整個代碼:C#隱含轉換問題

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 


public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    protected void // ERROR: Handles clauses are not supported in C# 
    btnSubmit_Click(object sender, System.EventArgs e) 
    { 
     if (((string.IsNullOrEmpty(txtUserName.Text)))) 
     { 
      lblErrorMessage.Text = "Username must be entered."; 
      txtUserName.Focus(); 
      return; 
     } 

     string connString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; 
     System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connString); 
     string sql = "Select * From TCustomers"; 
     System.Data.SqlClient.SqlDataReader objDR = default(System.Data.SqlClient.SqlDataReader); 
     System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand(sql, myConnection); 
     myConnection.Open(); 

     objDR = objCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 
     bool blnLogin = false; 
     string strPassword = null; 
     string strUserName = null; 
     strPassword = txtPassword.Text; 
     strPassword = strPassword.Trim(); 
     strUserName = txtUserName.Text; 
     strUserName = strUserName.Trim(); 

     while (objDR.Read()) 
     { 
      if (((objDR["strUserName"].ToString().Trim() == strUserName)) & ((objDR["strPassword"].ToString().Trim() == strPassword))) 
      { 
       blnLogin = true; 
       Session["CustomerID"] = objDR["intCustomerID"]; 
       Session["UserName"] = objDR["strUserName"]; 
       Session["FirstName"] = objDR["strFirstName"]; 
       Session["LastName"] = objDR["strLastName"]; 
       Session["Email"] = objDR["strEmailAddress"]; 
       Session["UserType"] = objDR["intUserTypeID"]; 
       break; 

       if ((blnLogin)) 
       { 
        if(Session["UserType"] = 1) 
        { 
         Response.Redirect("EditAccount.aspx"); 
        } 
        { 
         Session["UserType"] = 2; 
         Response.Redirect("AdminPanel.aspx"); 
        } 
        Response.End(); 
       } 
       else 
       { 
        lblErrorMessage.Text = "Username and/or password is incorrect."; 
       } 
      } 
     } 
    } 
} 
+3

請僅發佈相關代碼並刪除不必要的空行。 – 2012-04-13 16:25:51

+0

現在你刪除了太多:)前一行包含一個「break」。 – Vlad 2012-04-13 16:31:28

+0

另請注意,您可能會錯過'else'。 – 2012-04-13 16:31:31

回答

11

的問題是,你在下面

if(Session["UserType"] = 1) 
{ 
    Response.Redirect("EditAccount.aspx"); 
} 

做一個任務,而不是在代碼中的比較使用==,而不是=比較。

作業結果爲int,並且int不能在C#中隱式轉換爲bool。這是報告的錯誤。

如果更改===你會得到另一個錯誤,因爲你不能的Session["UserType"]值比較的int。要做到這一點,你需要將其轉換爲int這樣

​​

,但請記住,這是假定該值可以轉換爲int。如果不是這種情況,你會得到一個運行時錯誤。

代碼中可能還存在其他錯誤,但包含的代碼比我的心理編譯器可以處理的要多。

+2

他還需要將會話變量轉換或解析爲「int」,還是我誤會了? – emd 2012-04-13 16:28:59

+1

我不認爲甚至達到了代碼。那裏有一個時髦的「休息」。 – Mat 2012-04-13 16:30:16

+0

以及emd在你需要轉換之前說過,但也許你正在使用ENUM,那麼你需要一個不同的轉換。 – MUG4N 2012-04-13 16:31:35

6
if(Session["UserType"] = 1) 

...是一項任務,而不是比較;你可能想要的東西更接近:

if((int)Session["UserType"] == 1) 
1

你的if語句也許應該是一個比較不分配使用

if(Session["UserType"] == 1) 

你的代碼是因爲休息不可達。

0

break語句將退出while循環。它下面的代碼將不會執行。因此,該代碼無法訪問。