Im trying to make an if statement for the login enterance for my website at ASP.NET.
time after time, it just ignore the fact that its NOT suppose to enter the statement if something is null.
can you tell me where I got wrong ?
connection.Open();
//FirstName*****
string firstName = FirstNameTextBox.Text;
string sqlquery = (“INSERT INTO Users (FirstName,LastName,Username,Password) VALUES (@FirstName,@LastName,@Username,@Password) “);
SqlCommand command = new SqlCommand(sqlquery , connection);
command.Parameters.AddWithValue("FirstName", firstName);
//LastName************
string lastName = LastNameTextBox.Text;
command.Parameters.AddWithValue("LastName", lastName);
//Username*************
string username = UsernameTextBox.Text;
command.Parameters.AddWithValue("UserName", username);
//Password*************
string password = PasswordTextBox.Text;
command.Parameters.AddWithValue("Password", password);
if (lastName != null || username != null || firstName != null || password != null)
{
if (PasswordTextBox.Text == ReTypePassword.Text)
{
Session["UserEnter"] = FirstNameTextBox.Text;
command.ExecuteNonQuery();
Response.Redirect("HomeAfter.aspx");
}
else if (PasswordTextBox.Text != ReTypePassword.Text)
{
ErrorLabel.Text = "Sorry, You didnt typed your password correctly. Please type again.";
}
else
{
ErrorLabel.Text = "Some Error has accured.";
}
}
else
{
ErrorLabel.Text = "Please fill all of the fields.";
}
connection.Close();
}
Textbox will not return nulls. It will return empty string so you should make your if statement with
!string.IsNullOrEmpty(string)with AND statement. Solution looks like this: