  | 
              
	              
	                
  
    |   | 
   
  
    
        
        สอบถามเรื่องการกำหนดสิทธิการเข้าใช้งานตอน Login โดยใช้ Session, if หน่อยครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                ผมได้สร้างตารางไว้ 2 ตารางคือ ตาราง member กับ ตาราง userID  
โดยตาราง member ใช้เก็บข้อมูลผู้ใช้ เช่น username, password, name, roleID 
ส่วนตาราง userID ใช้เก็บข้อมูลสิทะฺการใช้งาน เช่น roleID, roleName โดยกำหนด admin = 1 , user = 2 
คืออยากให้ตอนที่ login ผ่าน โดยถ้าเป็น admin ให้เด้งไปที่หน้า admin.aspx แล้วถ้าเป็น user ก็ให้เด้งไปหน้า user.aspx 
แล้วจะต้องเขียนโค้ดยังไงบ้างครับ ขอตัวอย่างโค้ดด้วยก็ดีนะครับ 
ส่วนนี่คือโค้ด login 
 
Code (C#) 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace AspNetMember
{
    public partial class frmLogin : System.Web.UI.Page
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection objConn = null;
            string strConnString = null;
            StringBuilder strSQL = default(StringBuilder);
            SqlCommand objCmd = null;
            int intCount = 0;
            //*** Open Connection ***'
            strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
            objConn = new SqlConnection();
            objConn.ConnectionString = strConnString;
            objConn.Open();
            //*** Check Login ***'
            strSQL = new StringBuilder();
            strSQL.Append(" SELECT COUNT(*) FROM member ");
            strSQL.Append(" WHERE Username = @sUsername ");
            strSQL.Append(" AND Password = @sPassword ");
            objCmd = new SqlCommand(strSQL.ToString(), objConn);
            objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
            objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = this.txtPassword.Text;
            intCount = (int)objCmd.ExecuteScalar();
            objConn.Close();
            objConn = null;
            if (intCount <= 0)
            {
                this.lblStatus.ForeColor = System.Drawing.Color.Red;
                this.lblStatus.Text = "Username or Password wrong!";
            }
            else
            {
                Session["strUsername"] = this.txtUsername.Text;
                Response.Redirect("frmHomeProfile.aspx");
            }
        }
    }
}
 
 
  Tag : .NET, Ms SQL Server 2005, Web (ASP.NET), WebService, C#, VS 2008 (.NET 3.x)               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2012-07-06 17:19:46 | 
                      By :
                          panpoo | 
                      View :
                          1710 | 
                      Reply :
                          10 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (C#) 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace AspNetMember
{
    public partial class frmLogin : System.Web.UI.Page
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection objConn = null;
            string strConnString = null;
            StringBuilder strSQL = default(StringBuilder);
            SqlCommand objCmd = null;
            int intCount = 0;
            //*** Open Connection ***'
            strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
            objConn = new SqlConnection();
            objConn.ConnectionString = strConnString;
            objConn.Open();
            //*** Check Login ***'
            strSQL = new StringBuilder();
            strSQL.Append(" SELECT roleID FROM member ");
            strSQL.Append(" WHERE Username = @sUsername ");
            strSQL.Append(" AND Password = @sPassword ");
            objCmd = new SqlCommand(strSQL.ToString(), objConn);
            objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
            objCmd.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = this.txtPassword.Text;
            intCount = (int)objCmd.ExecuteScalar();
            objConn.Close();
            objConn = null;
            if (intCount <= 0)
            {
                this.lblStatus.ForeColor = System.Drawing.Color.Red;
                this.lblStatus.Text = "Username or Password wrong!";
            }
            else if (intCount == 1)
            {
                Session["strUsername"] = this.txtUsername.Text;
                Response.Redirect("Admin.aspx");
            }
   else if (intCount ==2)
            {
                Session["strUsername"] = this.txtUsername.Text;
                Response.Redirect("User.aspx");
            }
        }
    }
}
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2012-07-06 23:57:50              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-06 23:45:23 | 
                        By :
                            bangbang111 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มันไม่ได้อ่ะครับ มันเด้งไปแต่หน้า Admin.aspx อย่างเดียวเลย                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 07:53:10 | 
                        By :
                            panpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ถ้า Roleid 1 ได้ Roleid 2 ก็ต้องได้ค่ะ ถ้าคุณ login ด้วย user ที่ Roleid=2 จิงๆ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 11:03:47 | 
                        By :
                            bangbang111 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองทำแล้วไม่ได้จริงๆครับ ค่าก็ตามนั้นเลย แต่ก็เด้งไปตามค่าด้านบนเท่านั้น ค่าด้านล่างไม่มีผลเลย 
 
else if (intCount == 1)  มันเด้งเข้าแต่ค่านี้ 
 
else if (intCount ==2)  แต่ค่านี้มันไม่เด้งเข้ามาเลย                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 11:37:01 | 
                        By :
                            panpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลอง debug ค่าดูอ่ะค่ะว่าค่า intCount  เท่ากับ 2 จิงหรือปล่าว 
 ไม่งั้นลองเอาโค้ดมาดูหน่อยค่ะ                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2012-07-07 13:35:00              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 13:32:15 | 
                        By :
                            bangbang111 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลอง debug แต่ else if (intCount ==2) ดูแล้ว ปรากฎว่าค่าไม่ออก ออกแต่ if (intCount ==1) ส่วนโค้ดก็ตามที่ให้มานั่นล่ะครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 13:52:29 | 
                        By :
                            panpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แก้ตรงนี้แล้วใช่ป่าวค่ะ 
 
Code (C#) 
strSQL.Append(" SELECT roleID FROM member ");
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 13:55:34 | 
                        By :
                            bangbang111 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 อ๋อ ลืมแก้ตรงนี้นี่เอง  
 
ตอนนี้ได้แล้วครับ ขอบคุณมากครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-07 15:19:17 | 
                        By :
                            panpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มีปัญหาครับ 
 
Code (C#) 
if (intCount <= 0)           
{             
this.lblStatus.ForeColor = System.Drawing.Color.Red;               
this.lblStatus.Text = "Username or Password wrong!";          
}
 
 
โค้ดนี้ไม่ทำงานครับ พอกรอก Username & Password ผิด มัน Error ทันที 
แต่ถ้า Username & Password ถูกก็ปกติ ไม่ Error                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2012-07-13 23:33:15              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-12 02:31:15 | 
                        By :
                            panpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                                            
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-07-13 23:34:36 | 
                        By :
                            panpoo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |