【问题标题】:cant see gridview data that I want to see看不到我想看的 gridview 数据
【发布时间】: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


【解决方案1】:

你说

我需要该用户能看到该用户所在城市的车库。

看来你需要一个过滤器来选择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 

【讨论】:

  • 你好。我解释 - 用户插入他的城市和其他领域(电子邮件,汽车,......)也是车库用户。当用户输入 并选择类别时,他应该看到是城市的车库。所以我想也许可以过滤车库城市而不是用户。还是我错了?
  • 或者过滤用户和车库城市?
  • 我试试你写的,但我收到错误消息“必须声明标量变量“@userID”
  • 现在我得到“无法验证包含参数的查询”
  • 我认为你应该重写你的问题。现在我猜你正在创建一个CREATE USER 页面。在用户选择CITY 后,您想显示可用车库,所以用户选择一个?
猜你喜欢
  • 2019-03-06
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多