更新时间:2021-12-30 21:35:14
RequiredFieldValidator[^], perhaps.
由于无效使用而插入的布尔值返回true或false,所以这是怎么回事
Insted of void use return a boolean that is true or false so here is how it goes
private bool checkError()
{
if (txtName.Text == string.Empty)
{
WebMsgBox.Show("Please Enter User Name");
txtName.Focus();
return false;
}
if (txtUserId.Text.Trim() == string.Empty)
{
WebMsgBox.Show("Please Enter UserId");
txtUserId.Focus();
return false;
}
if (txtPasword.Text.Trim() == string.Empty)
{
WebMsgBox.Show("Please Enter Password");
txtPasword.Focus();
return false;
}
else return true;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if(checkError())
{
SqlConnection con = database.GetConnection();
SqlCommand com = new SqlCommand("spRegistration", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Name", txtName.Text);
com.Parameters.AddWithValue("@UserId", txtUserId.Text);
com.Parameters.AddWithValue("@Password", txtPasword.Text);
com.Parameters.AddWithValue("@Date", txtDate.Text);
com.ExecuteNonQuery();
WebMsgBox.Show("Accounts Created Successfully");
clearFields();
}
}
catch (Exception ex)
{
WebMsgBox.Show("Error Message:" + ex.Message);
}
}
更改错误例程以返回布尔值:
Change your error routine to return a bool:
private bool checkError()
{
if (txtName.Text == string.Empty)
{
WebMsgBox.Show("Please Enter User Name");
txtName.Focus();
return true;
}
if (txtUserId.Text.Trim() == string.Empty)
{
WebMsgBox.Show("Please Enter UserId");
txtUserId.Focus();
return true;
}
if (txtPasword.Text.Trim() == string.Empty)
{
WebMsgBox.Show("Please Enter Password");
txtPasword.Focus();
return true;
}
return false;
}
然后只需检查返回代码:
Then just check the return code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (!checkError())
{
SqlConnection con = database.GetConnection();
....
clearFields();
}
}
catch (Exception ex)
{
WebMsgBox.Show("Error Message:" + ex.Message);
}
}