C#隐式转换问题

Kei*_*ery -3 c#

好的,我有整个工作,直到我在休息后到达部分; 此时,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.";
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*sen 11

问题是你在下面的代码中进行了一次分配而不是比较

if(Session["UserType"] = 1)
{
    Response.Redirect("EditAccount.aspx");
}
Run Code Online (Sandbox Code Playgroud)

使用==而不是=比较.

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

如果你改变===你会得到另一个错误,因为你不能比较的价值Session["UserType"]int.要做到这一点,你需要把它投射到int这样

if((int)Session["UserType"] == 1)
{
    Response.Redirect("EditAccount.aspx");
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 他还需要将会话变量转换或解析为`int`或者我错了? (2认同)

Row*_*haw 6

if(Session["UserType"] = 1)
Run Code Online (Sandbox Code Playgroud)

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

if((int)Session["UserType"] == 1)
Run Code Online (Sandbox Code Playgroud)