我知道这是一个新问题,但我需要你的帮助。
我已经运行了这个网站很长时间,当我安装VS2013并重新打开这个解决方案时,我发现了这几个错误。我无法再登录了。
错误:
' System.Web.UI.Page.User'是一个属性'但是像''
一样使用名称' ConnectionClass'在当前上下文中不存在
这是我的Login.cs文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
User user = ConnectionClass.LoginUser(txtLogin.Text, txtPassword.Text);
if (user != null)
{
//Store login variables in session
Session["login"] = user.Username;
Session["type"] = user.Usertype;
Response.Redirect("~/Home.aspx");
}
else
{
lblError.Text = "Login failed";
}
}
}
我的ConnectionClass.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
public class ConnectionClass
{
private static SqlConnection conn;
private static SqlCommand command;
public static ConnectionClass()
{
string connectionString= ConfigurationManager.ConnectionStrings["CISConnectionString"].ToString();
conn = new SqlConnection(connectionString);
command = new SqlCommand("", conn);
}
public static ArrayList GetPOByType(string Customertype)
{
ArrayList list = new ArrayList();
string query = string.Format("SELECT * FROM PO WHERE Customertype LIKE '{0}'", Customertype);
try
{
conn.Open();
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string name = reader.GetString(1);
string type = reader.GetString(2);
string image = reader.GetString(3);
string review = reader.GetString(4);
PO PO = new PO(id, name, type, image, review);
list.Add(PO);
}
}
finally
{
conn.Close();
}
return list;
}
public static User LoginUser(string name, string password)
{
//Check if user exists
string query = string.Format("SELECT COUNT(*) FROM Users WHERE Username = '{0}'", name);
command.CommandText = query;
try
{
conn.Open();
int amountOfUsers = (int)command.ExecuteScalar();
if (amountOfUsers == 1)
{
//User exists, check if the passwords match
query = string.Format("SELECT Password FROM Users WHERE Username = '{0}'", name);
command.CommandText = query; string dbPassword = command.ExecuteScalar()。ToString();
if (dbPassword == password)
{
//Passwords match. Login and password data are known to us.
//Retrieve further user data from the database
query = string.Format("SELECT Email, Usertype FROM Users WHERE Username = '{0}'", name);
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
User user = null;
while (reader.Read())
{
string email = reader.GetString(0);
string type = reader.GetString(1);
user = new User(name, password, email, type);
}
return user;
}
else
{
//Passwords do not match
return null;
}
}
else
{
//User does not exist
return null;
}
}
finally
{
conn.Close();
}
}
public static string RegisterUser(User user)
{
//Check if user exists
string query = string.Format("SELECT COUNT(*) FROM Users WHERE Username = '{0}'", user.Username);
command.CommandText = query;
try
{
conn.Open();
int amountOfUsers = (int)command.ExecuteScalar();
if (amountOfUsers < 1)
{
//User does not exist, create a new user
query = string.Format("INSERT INTO users VALUES ('{0}', '{1}', '{2}', '{3}')", user.Username, user.Password,
user.Email, user.Usertype);
command.CommandText = query;
command.ExecuteNonQuery();
return "User registered!";
}
else
{
//User exists
return "A user with this name already exists";
}
}
finally
{
conn.Close();
}
}
}
我真的需要你的帮助。
答案 0 :(得分:0)
ConnectionClass
很可能在您尝试从(Login.cs)访问它的命名空间中不可用。没有引用ConnectionClass
。
您需要添加名称空间并确保它们相同或添加对ConnectionClass所在的名称空间的引用(例如,使用MySolution.Connectivity
)。