2016-03-23 37 views
0

Kullanacağım çalışan kimliğini saklayan empid adlı bir kurabim var. Ancak, herhangi bir çerezin değerini almaya çalıştığımda, değeri otomatik olarak sıfırlanır ve bu kod, sayfa tarafından miras alınan kısmi bir sınıftan gelir. Ne ben tüm veritabanı ile ilgili kod içeren bir sınıf oluşturmak ve Response nesneden everypageÇerezler otomatik olarak boşaltılıyor

protected void Page_Load(object sender, EventArgs e) 
{ 
    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString())) 
    { 

     using (SqlCommand cmd = new SqlCommand()) 
     { 
      try 
      { 

       cn.Open(); 
       SqlDataReader conReader; 
       conReader = null; 
       mycomment.Text = Response.Cookies["empid"].Value; 
       // cmd.CommandText = "Select * from comments where c_from = " + Response.Cookies["cid"] + " and c_to = "+ Response.Cookies["empid"].ToString(); 
       cmd.Connection = cn; 
       mycomment.Text = cmd.CommandText; 
       cmd.CommandType = CommandType.Text; 
       conReader = cmd.ExecuteReader(); 

       while (conReader.Read()) 
       { 
        mycomment.Text += conReader[3].ToString(); 

       } 

      } 

      catch (Exception ex) 
      { 
       Console.Write(ex); 
      } 
      finally 
      { 
       cn.Close(); 
      } 

     } 
    } 
} 

cevap

0

Sen alıyoruz Çerezler kullanmak istediğiniz istiyorsanız - Giden çerezler set değil, Request nesne müşteriden gelen çerezler kümesidir.

mycomment.Text = this.Request.Cookies["empid"].Value olarak değiştirin.

Ayrıca herhangi bir sanitasyon olmaksızın Dize Birleştirme'yi kullanarak SQL komutları oluşturduğunuzu görüyorum. BU OLARAK YAPMAYIN. Bunun yerine, aşağıdaki parametreleri kullanın:

cmd.CommandText = "SELECT * FROM comments WHERE c_from = @from AND c_to = @to"; 
cmd.Parameters.AddWithValue("@from", this.Request.Cookies["cid"].Value); 
cmd.Parameters.AddWithValue("@to" , this.Request.Cookies["empid"].Value); 
İlgili konular