Reputation: 1070
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();
}
Upvotes: 0
Views: 1025
Reputation: 3154
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:
if (!string.IsNullorEmpty(lastName) && !string.IsNullorEmpty(username) && !string.IsNullorEmpty(firstName) && !string.IsNullorEmpty(password))
{
//code here
}
else
{
ErrorLabel.Text = "Please fill all of the fields.";
}
Upvotes: 1
Reputation: 58723
The Value of empty Textboxes will be an empty string, not null.
Upvotes: 0
Reputation: 471
Textbox.Text doesnt return null value, try to doit with String.IsNullOrEmpty();
Upvotes: 0
Reputation: 25435
Your if should use AND
s instead of OR
s
if (lastName != null && username != null && firstName != null && password != null)
Upvotes: 2