无法看到我想看到的gridview数据

时间:2015-07-19 13:52:30

标签: sql asp.net sql-server gridview webforms

在我的项目中,我需要用户看到同一城市用户的车库。

我创建了包含 - 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");

        }

    }


}

}

1 个答案:

答案 0 :(得分:0)

你说

  

我需要用户看到同一城市用户的车库。

您似乎需要一个过滤器来选择USER

你也说

  

(GarageUserCategory将显示当前类别中的数据 - 它可以忽略它。)

所以我删除它。下次尝试只使用您遇到的问题简化查询

SELECT 
    GargeUsers.GarageName, 
    GargeUsers.Address,
    GargeUsers.City,
    GargeUsers.Email 
FROM 
    GargeUsers INNER JOIN         
    Users ON GargeUsers.City = Users.City 
WHERE 
    Users.UserID = @userID <-- ADD THIS ONE

要在SQL中处理变量,请检查SELECT @local_variable (Transact-SQL)

用户选择城市车库在页面中选择城市

SELECT 
    GargeUsers.GarageName, 
    GargeUsers.Address,
    GargeUsers.City,
    GargeUsers.Email 
FROM 
    GargeUsers 
WHERE 
    GargeUsers.City = @cityID