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)
- {
-
-
- string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
-
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd = new SqlCommand("spRegisterUser", con);
- cmd.CommandType = CommandType.StoredProcedure;
- SqlParameter username = new SqlParameter("@UserName", txtUserName.Text);
-
- 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