Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you display data that is retrieved from database according to the current login user? I know that I should declare the id, but I dont really know how...

I have 2 tables in SQL. 

User: 
ID || Name || Last_Name || date ||
List
ID || Product || description || user_id


What I have tried:

<pre>private void BindGridView()
    {  
string constr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(constr))
            {

                using (SqlCommand cmd = new SqlCommand())
                { 
                    cmd.Connection = conn;
                    cmd.CommandText = @"SELECT * From List where ID = @ID";
                    cmd.Parameters.AddWithValue("@ID", ?);

                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        ad.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }     
        }
    }
Posted
Updated 22-Jan-17 17:30pm
Comments
[no name] 22-Jan-17 14:18pm    
Well, you would replace the "?" with whatever the ID is from wherever you are getting the ID from. We can't possibly tell you, it's your project and we don't know anything about it.

After successful login store the user id in in a session to know the logged user for you application.

ex: Session["UserID"]= UserID; (Here UserID is the logged user id for that session)

As per your question you want to retrieve the data from list by of a logged
cmd.CommandText = @"SELECT * From List where ID = @ID";
Parameters.AddWithValue("@ID", ?);

Instade of question mark pass the session value of logged user id.
Just like :
Parameters.AddWithValue("@ID", Session["UserID"]);

If UserID is an integer field then before pass as parameter convert it to integer field like. Parameters.AddWithValue("@ID", Convert.ToInt32(Session["UserID"]));

Try it. I think it will solve your problem.
 
Share this answer
 
We can't tell you explicitly what to do - we don't have any idea how the rest of your code works. But all you have to do is look at where you log the user in, and find where you store the ID. Then put that value in place of the question mark in this line:
cmd.Parameters.AddWithValue("@ID", ?);
 
Share this answer
 

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