Я делаю нашу систему для диссертации, и я пытаюсь сохранить учетную запись пользователей. Мое условие: если есть пустая форма, учетная запись не будет сохранена, и она будет отображать окно сообщения. Я попытался поместить его в элемент if
, чтобы "If aForm.Text.Trim ()! = Null" информация / учетная запись были сохранены в базе данных. Но когда я попытался оставить пустую форму и нажать «Сохранить», она все равно была сохранена в базе данных.
Вот код * ВСЕ КОЛОННЫ В SQL установлены на «НЕ ПУСТО»
private void aAddUserSave_Click(object sender, EventArgs e)
{
// it will save the data if all of the forms is not == to null
// or empty spaces
// and before the info will be saved
// the confirm password should be == to the password
if (aAddUserFirstName.Text.Trim() != null && aAddUserLastName.Text.Trim() != null && aAddUserAddress.Text.Trim() != null && aAddUserContact.Text.Trim() != null && aAddUserPass.Text.Trim() != null &&
aAddUserPassConfirm.Text.Trim() != null && aAddUserForgotAnswer.Text.Trim() != null && aAddUserPassConfirm.Text == aAddUserPass.Text)
{
// location and the command for the database
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\makoy2017\Documents\My files\School Files\Veron System\System Files\DO NOT DELETE\veronServer.mdf;Integrated Security=True;Connect Timeout=30";
string query = "INSERT INTO usersAccount (firstName, lastName, address, contactNo, position, password, forgotAnswer) values('" + this.aAddUserFirstName.Text + "', '" + this.aAddUserLastName.Text + "', '" + this.aAddUserAddress.Text +
"', '" + this.aAddUserContact.Text + "', '" + this.aAddUserPosition.SelectedIndex + "', '" + this.aAddUserPass.Text + "', '" + this.aAddUserForgotAnswer.Text + "');";
// connecting to the database
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
SqlDataReader sqlDataReader;
// try and catch for the saving and reading of the data
// so if there's a error the system will not close
// and show the message error
try
{
// open the database connectiont to the system
sqlConnection.Open();
// execute the sql command for the sql reader
sqlDataReader = sqlCommand.ExecuteReader();
// read all of the data
while (sqlDataReader.Read())
{
}
// show user saved
// when the data is saved successfully!
MessageBox.Show("User Saved!");
}
catch (Exception ex)
{
// this will show the message box
// what is the error of the data
// the data will not be saved in the database
MessageBox.Show(ex.Message);
}
}
else {
MessageBox.Show("Please check your confirm/password!");
}
}
Пожалуйста, помогите. Сделайте код простым, потому что я все еще начинающий. Заранее спасибо :)