使用where子句绑定gridview不起作用 [英] Binding gridview with where clause does not work

查看:73
本文介绍了使用where子句绑定gridview不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a gridview filled with product information. SQL table looks like this:
ID||UserID||Name||Price||
and I want that the ID of the product matches with the id of the Users (User_ID). (I have a own SQL table with User Information).
I have this Code, but the gridview does not show something. Its just empty...





我尝试过:





What I have tried:

protected void BindGrid()

        { string constr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(constr))
            {

                using (SqlCommand cmd = new SqlCommand())
                { //Land ergänzen
                    cmd.Connection = conn;
                    cmd.CommandText = "SELECT * From Product WHERE UserID=@UserID";
                    cmd.Parameters.Add("@UserID", SessionHelper.User.ID);
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        ad.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }

推荐答案

cmd.Parameters.Add("@UserID", SessionHelper.User.ID);



上面的命令将参数添加到SQL中,但不是值。您应该使用 SqlParameterCollection.AddWithValue Method(String ,Object)(System.Data.SqlClient) [ ^ ]。


这篇关于使用where子句绑定gridview不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆