-3

好的,我已经完成了整个工作,直到休息后才开始工作;此时 If 表示检测到无法访问的代码,并且 if(Session["UserType"] = 1) 给出了一个错误,指出无法将类型对象隐式转换为 bool 类型。对于如何解决这个问题,有任何的建议吗?以下是整个代码:

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.";
                }
            }
        }
    }
}
4

4 回答 4

11

问题是您正在执行分配而不是下面代码中的比较

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

使用==而不是=进行比较。

赋值的结果是int,并且int不能bool在 C# 中隐式转换为。这就是报告的错误。

如果您更改=为,==您将收到另一个错误,因为您无法将 的值Session["UserType"]int. 为此,您需要将其转换为int这样

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

但请记住,这假定该值可以转换为int. 如果不是这种情况,您将收到运行时错误。

代码中可能还有其他错误,但是您包含的代码比我的心理编译器可以处理的要多。

于 2012-04-13T16:28:12.383 回答
6
if(Session["UserType"] = 1)

...是一个任务,而不是一个比较;你可能想要更接近的东西:

if((int)Session["UserType"] == 1)
于 2012-04-13T16:29:04.053 回答
1

您的 if 语句可能应该是比较而不是赋值使用

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

由于中断,您的代码无法访问。

于 2012-04-13T16:30:14.220 回答
0

break 语句将退出 while 循环。它下面的代码将不会执行。因此,该代码是无法访问的。

于 2012-04-13T16:31:11.663 回答