Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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:

C#
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();
                    }
                }
            }
        }
Posted
Updated 18-Nov-16 1:53am
Comments
Jawad Ahmed Tanoli 18-Nov-16 7:53am    
you try to debug the code ? try to check Records in DataTable dt? and and also check SessionHelper.User.ID value
Karthik_Mahalingam 18-Nov-16 10:41am    
does the query returns data in sql studio ?

1 solution

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

The above command adds the parameter to your SQL but not the value. You should use SqlParameterCollection.AddWithValue Method (String, Object) (System.Data.SqlClient)[^].
 
Share this answer
 
Comments
Member 12802669 18-Nov-16 7:59am    
Tried. Still does not work. Just an empty page....
Richard MacCutchan 18-Nov-16 8:31am    
Then you need to do some debugging to find out what information is returned from the SQL command. We cannot guess what happens.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900