0
protected void loadCandidate()
{
    con.Open();
    MySqlCommand cmd = new MySqlCommand("select * from voter where studentID ='" + User.Identity.Name + "'", con);
    MySqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows == true)
    {
        GridView1.DataSource = dr;
        GridView1.DataBind();
    }
 }

 //This is my grid view
 <asp:GridView ID="GridView1" runat="server"></asp:GridView>

I'd like to let users see only their own data in the voter table.

For example, the user's id is 923WRC123, then the users can only see their own data which is the data belong to 923WRC123 without showing other users data in the grid view1.

I created a session to store the user ID when they login into their account.

There is no error message, but the grid view is empty, it should display the data belong to the users 923WRC123

Maelig
  • 2,046
  • 4
  • 24
  • 49
Kekw Yc
  • 5
  • 4
  • Why do you filter data by user name instead of studentId, there is problem, your query should be like "select * from voter where studentID ='" + User.Identity.Id + "'" – TJacken Oct 12 '20 at 06:59
  • Hi, when I want to change it from name to Id, there is an error message shown. 'IIdentity' does not contain a definition for 'Id' and no accessible extension method 'Id' accepting a first argument of type 'IIdentity' could be found (are you missing a using directive or an assembly reference?) – Kekw Yc Oct 12 '20 at 07:10
  • Try this: User.Identity.GetUserId, if you got null Id, see this answer https://stackoverflow.com/questions/33951881/user-identity-getuserid-returns-null-after-successful-login – TJacken Oct 12 '20 at 07:16
  • hi, I still get the same error message saying the identity does not contain a definition for "GetUserId" and not accessible extension method 'GetUserId' – Kekw Yc Oct 12 '20 at 07:23
  • Try to add this reference: using Microsoft.AspNet.Identity; – TJacken Oct 12 '20 at 07:24
  • Hi, I added the using Microsoft.AspNet.Identity; I still get the same error message. How about using the session? Is it possible to sue the session since I created a session to store the userID when they login – Kekw Yc Oct 12 '20 at 07:34
  • If you using ASP.NET Core add this reference Microsoft.AspNetCore.Identity – TJacken Oct 12 '20 at 07:39

0 Answers0