2014-04-24 60 views
0

檢索。 這裏是我的代碼,請幫我... MasterPage.master.cs會話名稱未在母版頁

protected void Page_Load(object sender, EventArgs e) 
     { 
     if (!IsPostBack) 
      { 
      if (Session["name"] == null) 
       { 
        Panel2.Visible = false; 
        Panel1.Visible = true; 
       } 
      else if (Session["name"] != null) 
        { 
         Panel1.Visible = false; 
         Panel2.Visible = true; 
         Label2.Text = "WELCOME | Mr." + Session["name"].ToString(); 
        } 
      } 
     } 
     protected void LoginStatus1_LoggedOut(object sender, EventArgs e) 
      { 
       Session.Clear(); 
       Session.Abandon(); 
      } 

homepage.aspx.cs

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string st="select Label4,Label3 FROM Register1_master WHERE Label4='" +  TextBox1.Text + "' and Label3='" + TextBox2.Text + "'"; 
     cmd = new SqlCommand(st, sqlcon); 
     cmd.Connection.Open(); 
     string result= null; 
     Object value=cmd.ExecuteScalar(); 
     if (value != null) 
     { 
      result = value.ToString(); 
      Session["name"] = TextBox1.Text; 
      Response.Redirect("Main.aspx"); 
     } 
     else 
      { 
      Label3.Text="Invalid username or password"; 
      } 
      cmd.Connection.Close(); 
    } 

從主頁,我會在登錄後請繼續Main.aspx page

my Main.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
     { 
     if (!IsPostBack) 
      { 
      HyperLink link = (HyperLink)Master.FindControl("HyperLink1"); 
      link.Visible = false; 
      HyperLink link1 = (HyperLink)Master.FindControl("HyperLink2"); 
      link1.Visible = true; 
      Label masterlbl = (Label)Master.FindControl("Label2"); 
      string login = Convert.ToString(Session["name"]); 
      Session["name"] = login; 

      } 

     } 
+0

F9 - >會話[ 「名稱」] = TextBox1.Text; F9也在 - > Session [「name」] = login; 你確定你打這個聲明?什麼是價值? – InferOn

+0

你確定你的項目中啓用了sessionstate嗎?請參閱:http://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.85).aspx – Matthijs

+0

請使用string st =「select * FROM Register1_master WHERE Label4 ='」+ TextBox1.Text +「 '和Label3 ='「+ TextBox2.Text +」'「; –

回答

0

你有沒有嘗試這樣的:

int result= null; 
    result= = Convert.ToInt32(cmd.ExecuteScalar()); 

使用ExecuteScalar方法來檢索單個值(例如一個聚合值)從數據庫中。與使用ExecuteReader方法相比,這需要更少的代碼,然後執行需要使用SqlDataReader返回的數據生成單個值的操作。

典型的ExecuteScalar查詢可以被格式化爲在下面的C#例如:

cmd.CommandText = "SELECT COUNT(*) FROM dbo.region"; 
Int32 count = (Int32) cmd.ExecuteScalar(); 

請參考上MSDN