好吧,我有整個事情工作,直到我休息後到達部分;此時,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.";
}
}
}
}
}
請僅發佈相關代碼並刪除不必要的空行。 – 2012-04-13 16:25:51
現在你刪除了太多:)前一行包含一個「break」。 – Vlad 2012-04-13 16:31:28
另請注意,您可能會錯過'else'。 – 2012-04-13 16:31:31