【发布时间】:2015-07-19 13:52:29
【问题描述】:
在我的项目中,我需要该用户能够看到用户所在城市的车库。
我创建了 users 表,其中包括 - Id,UserName,Password,Email,CarModel,Year,City,
另一个表是GarageUsers,其中包括-Id,UserName,Password,Email,Address,City,GarageName。
在Configure Data Source 我插入了这段代码:
SELECT GargeUsers.GarageName, GargeUsers.Address,GargeUsers.City,GargeUsers.Email
FROM GargeUsers
INNER JOIN GarageuserCategory ON GargeUsers.Id = GarageuserCategory.UserId
I
WHERE (GarageuserCategory.CategoryId = 1) AND (GargeUsers.City LIKE Users.City)
(GarageUserCategory 是显示当前类别中的数据 - 可以忽略它)。
在这段代码中,我看到了所有的车库。
我添加了用户登录时保存用户城市的会话。 但是我在gridview中看不到我想要的东西。我需要知道如何将会话(USerCity)等同于车库城市。
protected void LogIn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from Users where UserName= '" + UserName.Text + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
conn.Close();
if (temp == 1)
{
conn.Open();
string checkPasswordQuery = "select Password from Users where UserName= '" + UserName.Text + "'";
SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
string password = passComm.ExecuteScalar().ToString().Replace(" ", "");
string UserCityQuery = "select City from Users where UserName= '" + UserName.Text + "'";
SqlCommand cityComm = new SqlCommand(UserCityQuery, conn);
string UserCity = cityComm.ExecuteScalar().ToString().Replace(" ", "");
if (password == Password.Text)
{
Session["UserCity"] = UserCity;
Session["New"] = UserName.Text;
Response.Write("Password is correct");
Response.Redirect("HomePage.aspx");
}
else
{
Response.Write("Password is not correct");
}
}
else
{
Response.Write("User Name is not correct");
}
}
}
}
【问题讨论】:
-
您能提供样本数据和预期结果吗?
-
我必须知道一些,也许你可以帮助我。我必须用英文插入数据还是没关系?
-
尝试创建一个SQL FIDDLE 以便我们可以看到数据。查询看起来没问题。
标签: sql asp.net sql-server gridview webforms