わかりました。この時点で、到達不能コードが検出され、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.";
}
}
}
}
}
関連コードのみを投稿し、不要な空白行を削除してください。 –
これであまりにも多くを削除しました:)前の行には 'break'が含まれていました。 – Vlad
また、 'else'が見つからない可能性があります。 –