title: How to Display only certain rows in gridview based on which user(session) have login
body: <p>I have two tables called appointment and patient with relationship with each other using patientID. What I want to achieve is user/patient who login will only get to see their own appointment instead of all appointment/allRows in the gridview. Etc, at the first attached image, since I am login as adam, I only want to view the row with appointmentID 2 only since in the appointment table in attached image 2, patientID number 3 belongs to Adam in the patient table. By the way I did stored the patient ID in a session after logging in. The codes for login and appointment page are below. Any help?</p>
  
  <p><strong>View Appointment Page</strong>
  <img src="https://i.stack.imgur.com/ZcWMx.jpg" alt="View Appointment Page"></p>
  
  <p><strong>Patient and Appointment Tables</strong>
  <img src="https://i.stack.imgur.com/xHEPI.jpg" alt="Patient and Appointment Tables"></p>
  
  <p><strong>//LOGIN FORM CODES</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  using System.Web.Security;
  using System.Globalization;
  using System.Text;
  using System.Threading;
  
  public partial class login : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void btnLogin_Click(object sender, EventArgs e)
      {
          string user = txtUserName.Text.Trim();
          string pwd = txtPassword.Text;
          DataTable dt = new DataTable();
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
          {
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
  
              // Another method --&gt; cmd.CommandText = "SELECT patientID, pUsername, pPassword FROM patient WHERE pUsername='" + user + "' AND pPassword='" + pwd + "'  ";
              cmd.CommandText = "SELECT patientID, pUsername, pPassword FROM patient WHERE pUsername= @user and pPassword= @password;";
              cmd.Parameters.Add("@user", SqlDbType.NVarChar, 16).Value = user;
              cmd.Parameters.Add("@password", SqlDbType.NVarChar, 200).Value = pwd;
              con.Open();
              SqlDataAdapter da = new SqlDataAdapter();
              da.SelectCommand = cmd;
              da.Fill(dt);
  
              int id = (cmd.ExecuteScalar() != null) ? Convert.ToInt32(cmd.ExecuteScalar()) : 0;
              if (id &gt; 0)
              {
                  Session.Add("ID", id);
                  Session.Add("Username", txtUserName.Text);
                  Session.Add("Password", txtPassword.Text);
                  FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
                  Response.Redirect("index.aspx");
              }
              else
              {
                  lblErrorMessage.Text = "Invalid Username or Password";
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>//APPOINTMENT FORM CODES</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  
  public partial class member_viewappointment : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (Page.IsPostBack == false)
          {
              // call BindGridView
              bindGridView();
  
          }
      }
  
      private void bindGridView()
      {
          //get connection string from web.config
          string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
          SqlConnection myConnect = new SqlConnection(strConnectionString);
  
          string strCommandText = "SELECT * from appointment";
          try
          {
              SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
  
              myConnect.Open();
              SqlDataReader reader = cmd.ExecuteReader();
  
              DataTable dt = new DataTable();
              dt.Load(reader);
              grdViewAppointment.DataSource = dt;
              grdViewAppointment.DataBind();
              lblResult.Text = "";
  
              reader.Close();
          }
          catch (SqlException ex)
          {
              lblResult.Text = "Error:" + ex.Message.ToString();
          }
          finally
          {
              myConnect.Close();
          }
  
      }
  }
  </code></pre>