C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Base Blockchain
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Vibe Coding
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
3
Answers
Help in the login
Jes Sie
7y
941
1
Reply
I have a private method below
private
void
Login()
{
int
UserId;
UsersDataModel usersDataModel =
new
UsersDataModel();
using
(SqlConnection con = DBCS.GetConString())
{
string
UserName = username.Value.Trim();
string
Password = password.Value.Trim();
string
SchoolCode = lblSchoolCode.Text.Trim();
string
SchoolName = lblSchoolName.Text.Trim();
string
LastLogin = lblLastLoginDate.Text.Trim();
//UserId = Convert.ToInt32(hfUserID.Value.Trim());
con.Open();
SqlCommand cmd =
new
SqlCommand(
"Validate_User"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"@Username"
, UserName);
cmd.Parameters.AddWithValue(
"@Password"
, Password);
SqlDataAdapter da =
new
SqlDataAdapter(cmd);
DataTable dt =
new
DataTable();
da.Fill(dt);
RowCount = dt.Rows.Count;
for
(
int
i = 0; i < RowCount; i++)
{
UserName = dt.Rows[i][
"Username"
].ToString();
Password = dt.Rows[i][
"Password"
].ToString();
SchoolCode = dt.Rows[i][
"SchoolCode"
].ToString();
SchoolName = dt.Rows[i][
"SchoolName"
].ToString();
LastLogin = dt.Rows[i][
"LastLoginDate"
].ToString();
UserId = Convert.ToInt32(dt.Rows[i][
""
]);
if
(UserName == username.Value && Password == password.Value)
{
Session[
"NickName"
] = UserName;
Session[
"SchoolCode"
] = SchoolCode;
Session[
"SchoolName"
] = SchoolName;
Session[
"LastLoginDate"
] = LastLogin;
if
(UserId == -1)
{
lblMessage.Text =
"Username and/or password is incorrect."
;
}
else
if
(UserId == -2)
{
lblMessage.Text =
"Account has not been activated."
;
}
else
{
WebMessageBox.Show(
"logged in"
);
}
}
else
{
lblMessage.Text =
"Invalid User Name or Password! Please try again!"
;
}
}
}
}
But this function gives me this error
Column 'Username' does not belong to table .
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.
Exception Details: System.ArgumentException: Column 'Username' does not belong to table .
Source Error:
Line 46: for (int
i
=
0
; i
<
RowCount
; i++)
Line 47: {
Line 48:
UserName
=
dt
.Rows[i]["Username"].ToString();
Line 49:
Password
=
dt
.Rows[i]["Password"].ToString();
Line 50:
SchoolCode
=
dt
.Rows[i]["SchoolCode"].ToString();
Below is my stored procedure:
USE [SchoolMIS]
GO
/****** Object: StoredProcedure [dbo].[Validate_User] Script
Date
: 12/11/2017 7:05:30 PM ******/
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
ALTER
PROCEDURE
[dbo].[Validate_User]
@Username NVARCHAR(20),
@
Password
NVARCHAR(20)
AS
BEGIN
SET
NOCOUNT
ON
;
DECLARE
@UserId
INT
,
@LastLoginDate DATETIME
SELECT
@UserId = UserId,
@LastLoginDate = LastLoginDate
FROM
Users
WHERE
Username = @Username
AND
[
Password
] = @
Password
IF @UserId
IS
NOT
NULL
BEGIN
IF
NOT
EXISTS(
SELECT
UserId
FROM
UserActivation
WHERE
UserId = @UserId)
BEGIN
UPDATE
Users
SET
LastLoginDate = GETDATE()
WHERE
UserId = @UserId
SELECT
@UserId [UserId]
-- User Valid
END
ELSE
BEGIN
SELECT
-2
-- User not activated.
END
END
ELSE
BEGIN
SELECT
-1
-- User invalid.
END
END
Hope someone can give me an assistance.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
in Asp.net --- When page load automatic system date
How to bind dropdownlist when updating data from to db in as