TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
jitendra lad
NA
51
10.3k
Page.IsValid not working
Oct 27 2017 5:12 AM
hello,
i m validating page on button_click event and transferinf data to database. Even though valueus in text box is null(blank) Page.IsValid property returns true and blank data loaded to database. i tried CausesValidation ="true" also. but does not work.please resolve .
CODE behind is
protected
void
btnRegister_Click(
object
sender, EventArgs e)
{
if
(Page.IsValid)
{
// Read the connection string from web.config.
// ConfigurationManager class is in System.Configuration namespace
string
CS = ConfigurationManager.ConnectionStrings[
"DBCS"
].ConnectionString;
// SqlConnection is in System.Data.SqlClient namespace
using
(SqlConnection con =
new
SqlConnection(CS))
{
SqlCommand cmd =
new
SqlCommand(
"spRegisterUser"
, con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter username =
new
SqlParameter(
"@UserName"
, txtUserName.Text);
// FormsAuthentication calss is in System.Web.Security namespace
string
encryptedPassword = FormsAuthentication.
HashPasswordForStoringInConfigFile(txtPassword.Text,
"SHA1"
);
SqlParameter password =
new
SqlParameter(
"@Password"
, encryptedPassword);
SqlParameter email =
new
SqlParameter(
"@Email"
, txtEmail.Text);
cmd.Parameters.Add(username);
cmd.Parameters.Add(password);
cmd.Parameters.Add(email);
con.Open();
int
ReturnCode = (
int
)cmd.ExecuteScalar();
if
(ReturnCode == -1)
{
lblMessage.Text =
"User Name already in use, please choose another user name"
;
lblMessage.ForeColor = System.Drawing.Color.Red;
}
else
{
Response.Redirect(
"~/Login.aspx"
);
}
}
}
}
SQL Table and Stored Procedure
Create table tblUser
(
[Id]
int
Primary Key Identity not
null
,
[UserName] nvarchar(100) not
null
,
[Mobile No]
int
not
null
,
[Email] nvarchar (100) not
null
,
[Password] nvarchar(max) not
null
)
Select * From tblUser
CREATE PROC [dbo].[spRegisterUser]
@UserName NVARCHAR(100),
@MobileNo
int
,
@Email NVARCHAR(100),
@Password nvarchar (max)
AS
BEGIN
DECLARE @Count INT
DECLARE @ReturnCode INT
SELECT @Count = COUNT(userName)
FROM tblUser
WHERE userName = @UserName
IF @Count > 0
BEGIN
SET @ReturnCode = - 1
END
ELSE
BEGIN
SET @ReturnCode = 1
INSERT INTO tblUser VALUES
(@UserName,@MobileNo,@Email,@Password)
END
SELECT @ReturnCode AS ReturnValue
END
Reply
Answers (
4
)
adding virtual printer using c#
Crystal report not works proper