I am trying to validate fields so user can’t have entered data with empty fields. The problem is that I am still adding empty fields, so my validation isn’t working.
First, I am checking is type is selected from a combo box. Then I am checking if which type user picked, and then depending on the type I am checking a fields for empty string before entering a data.
Did I miss something here?
private void btnAddEntry_Click(object sender, EventArgs e)
{
// Multiple level field validations.
if (cmbType.SelectedIndex != -1)
{
if (cmbType.SelectedIndex == 0 &&
(txtUserName.Text != string.Empty ||
txtPassword.Text != string.Empty))
{
string SQL =
"INSERT INTO PersonalData([Type], [UserName], [Password]) " +
"VALUES(@Type, @UserName, @Password)";
InsertData(SQL);
}
else if (cmbType.SelectedIndex == 1 &&
(txtURL.Text != string.Empty ||
txtUserName.Text != string.Empty ||
txtPassword.Text != string.Empty))
{
// Creating SQL string. Using [] will prevent any erros
// that might occur if any other names will be reserved words.
string SQL =
"INSERT INTO PersonalData([Type], [URL], [UserName], [Password]) " +
"VALUES(@Type, @URL, @UserName, @Password)";
InsertData(SQL);
}
else if (cmbType.SelectedIndex == 2 &&
(txtSoftwareName.Text != string.Empty ||
txtSerialCode.Text != string.Empty))
{
// Creating SQL string. Using [] will prevent any erros
// that might occur if any other names will be reserved words.
string SQL =
"INSERT INTO PersonalData([Type], [SoftwareName], [SerialCode]) " +
"VALUES(@Type, @SoftwareName, @SerialCode)";
InsertData(SQL);
}
else
{
lblMessage.Text = "Please fill out all required fields!";
}
}
else
{
lblMessage.Text = "Please select a type first!";
}
}
Are you sure you want to or the potential fields and not and them?
For instance,
Will only allow you to insert the data if the url, username and password have a value – using the or’s only one of those values needs to be filled in before the insertion is called, meaning you could have a valid url but no password and username entered.