title: if statement on session and Id value in sql asp.net
body: <p>i want to compare my session value to my Id value using if statement.
  so far i got this:</p>
  
  <pre><code>string strConnString = "Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True";
      string str;
      SqlCommand com;
      protected void Page_Load(object sender, EventArgs e)
      {
          SqlConnection con = new SqlConnection(strConnString);
  
          con.Open();
          str = "select * from CustomerDetails Where CustomerName = '"+Session["New"].ToString()+"'";
          com = new SqlCommand(str, con);
          SqlDataReader reader = com.ExecuteReader();
          List&lt;string&gt; ListOfId = new List&lt;string&gt;();
          while (reader.Read())
          {
  
              ListOfId.Add(reader["Id"].ToString());
          }
          if (!IsPostBack)
          {
  
              if (Session["New"].ToString() == Request.QueryString["Id"])
              {
                  Success
              }
  else
  {
  error
  }
  </code></pre>
  
  <p>What i want to happen is when the session value ("username") has no equivalent value of id in sql, it will be redirected to an error page.</p>
  
  <p><a href="https://i.stack.imgur.com/j031C.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j031C.jpg" alt="enter image description here"></a></p>
  
  <p>Like in the picture above, these id values are from the username "faufao". <code>if session("faufao" = "13")</code>, it will continue. Otherwise if the username is faufao, and id value is 4, it will proceed to error page.</p>
  
  <p>On the picture below. i substituted the label values to the session and ID query string. faufao(session) has the 14(query) value in the database so it should proceed. same goes with 13 15 16 17 18 19 etc. Other values that are not in the faufao(session) username will redirect to error.</p>
  
  <p><a href="https://i.stack.imgur.com/YG4Kd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YG4Kd.jpg" alt="enter image description here"></a></p>
  
  <p>The code is working. It catches the session and the id value (tried it in labels). My problem now is how to compare them.</p>
  
  <p>My code:</p>
  
  <pre><code>if (Session["New"].ToString() == Request.QueryString["Id"])
              {
                  Proceed
              }
  else
  {
  error
  }
  </code></pre>
  
  <p>It seems like i am lacking something here. Because it always redirects me to error page even though session:faufao has the query string of 13. </p>
  
  <p>tried your code and clicked on ID 13</p>
  
  <p><a href="https://i.stack.imgur.com/7hpKR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7hpKR.jpg" alt="enter image description here"></a></p>
  
  <p>faufao has the id value of 13, but still redirects to error page.</p>