Jes Sie

Jes Sie

  • 708
  • 1.2k
  • 264.2k

Help in the login

Dec 11 2017 7:13 AM
I have a private method below
  1. private void Login()  
  2.         {  
  3.             int UserId;  
  4.             UsersDataModel usersDataModel = new UsersDataModel();  
  5.             using (SqlConnection con = DBCS.GetConString())  
  6.             {  
  7.                 string UserName = username.Value.Trim();  
  8.                 string Password = password.Value.Trim();  
  9.                 string SchoolCode = lblSchoolCode.Text.Trim();  
  10.                 string SchoolName = lblSchoolName.Text.Trim();  
  11.                 string LastLogin = lblLastLoginDate.Text.Trim();  
  12.                 //UserId = Convert.ToInt32(hfUserID.Value.Trim());  
  13.   
  14.                 con.Open();  
  15.                 SqlCommand cmd = new SqlCommand("Validate_User", con);  
  16.                 cmd.CommandType = CommandType.StoredProcedure;  
  17.                 cmd.Parameters.AddWithValue("@Username", UserName);  
  18.                 cmd.Parameters.AddWithValue("@Password", Password);  
  19.   
  20.                 SqlDataAdapter da = new SqlDataAdapter(cmd);  
  21.                 DataTable dt = new DataTable();  
  22.                 da.Fill(dt);  
  23.                 RowCount = dt.Rows.Count;  
  24.                 for (int i = 0; i < RowCount; i++)  
  25.                 {  
  26.                     UserName = dt.Rows[i]["Username"].ToString();  
  27.                     Password = dt.Rows[i]["Password"].ToString();  
  28.                     SchoolCode = dt.Rows[i]["SchoolCode"].ToString();  
  29.                     SchoolName = dt.Rows[i]["SchoolName"].ToString();  
  30.                     LastLogin = dt.Rows[i]["LastLoginDate"].ToString();  
  31.                     UserId = Convert.ToInt32(dt.Rows[i][""]);  
  32.   
  33.                     if (UserName == username.Value && Password == password.Value)  
  34.                     {  
  35.                         Session["NickName"] = UserName;  
  36.                         Session["SchoolCode"] = SchoolCode;  
  37.                         Session["SchoolName"] = SchoolName;  
  38.                         Session["LastLoginDate"] = LastLogin;  
  39.   
  40.                         if (UserId == -1)  
  41.                         {  
  42.                             lblMessage.Text = "Username and/or password is incorrect.";  
  43.                         }  
  44.                         else if (UserId == -2)  
  45.                         {  
  46.                             lblMessage.Text = "Account has not been activated.";  
  47.                         }  
  48.                         else  
  49.                         {  
  50.                             WebMessageBox.Show("logged in");  
  51.                         }  
  52.                     }  
  53.                     else  
  54.                     {  
  55.                         lblMessage.Text = "Invalid User Name or Password! Please try again!";  
  56.                     }  
  57.                 }  
  58.             }  
  59.         }  
But this function gives me this error
  1. Column 'Username' does not belong to table .  
  2. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.   
  3.   
  4. Exception Details: System.ArgumentException: Column 'Username' does not belong to table .  
  5.   
  6. Source Error:   
  7.   
  8.   
  9. Line 46:                 for (int i = 0; i < RowCount; i++)  
  10. Line 47:                 {  
  11. Line 48:                     UserName = dt.Rows[i]["Username"].ToString();  
  12. Line 49:                     Password = dt.Rows[i]["Password"].ToString();  
  13. Line 50:                     SchoolCode = dt.Rows[i]["SchoolCode"].ToString();  
Below is my stored procedure:
  1. USE [SchoolMIS]  
  2. GO  
  3. /****** Object:  StoredProcedure [dbo].[Validate_User]    Script Date: 12/11/2017 7:05:30 PM ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. ALTER PROCEDURE [dbo].[Validate_User]  
  9.       @Username NVARCHAR(20),  
  10.       @Password NVARCHAR(20)  
  11. AS  
  12. BEGIN  
  13.       SET NOCOUNT ON;  
  14.       DECLARE @UserId INT,   
  15.               @LastLoginDate DATETIME  
  16.         
  17.       SELECT @UserId = UserId,   
  18.              @LastLoginDate = LastLoginDate   
  19.       FROM Users   
  20.           
  21.       WHERE Username = @Username AND [Password] = @Password  
  22.         
  23.       IF @UserId IS NOT NULL  
  24.       BEGIN  
  25.             IF NOT EXISTS(SELECT UserId FROM UserActivation WHERE UserId = @UserId)  
  26.             BEGIN  
  27.                   UPDATE Users  
  28.                   SET LastLoginDate = GETDATE()  
  29.                   WHERE UserId = @UserId  
  30.                   SELECT @UserId [UserId] -- User Valid  
  31.             END  
  32.             ELSE  
  33.             BEGIN  
  34.                   SELECT -2 -- User not activated.  
  35.             END  
  36.       END  
  37.       ELSE  
  38.       BEGIN  
  39.             SELECT -1 -- User invalid.  
  40.       END  
  41. END  
Hope someone can give me an assistance. 

Answers (3)