我正在尝试验证字段,以便用户不能输入带有空字段的数据。问题是我仍在添加空字段,因此我的验证无效。
首先,我要检查是否从组合框中选择了类型。然后我检查用户是否选择了哪种类型,然后根据类型我在输入数据之前检查字段中是否有空字符串。
我是不是漏掉了什么?
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!";
}
}
您确定要或潜在字段而不是它们吗?例如,
else if (cmbType.SelectedIndex == 1 &&
(txtURL.Text != string.Empty &&
txtUserName.Text != string.Empty &&
txtPassword.Text != string.Empty))
仅当 url、用户名和密码有值时才允许您插入数据 - 使用 or 时,在调用插入之前只需要填写其中一个值,这意味着您可以有一个有效的 url 但没有输入密码和用户名。
我是一名优秀的程序员,十分优秀!