C#:返回[exit]方法有问题

时间:2015-08-17 08:43:48

标签: c#

当我运行我的代码时,根据我的代码,它不是退出(return)方法。我使用两个不同的类,当我单击按钮时,它从一个方法运行到另一个方法,它不会退出(返回)该方法。

class FormAction
{

DBConnection dbCon = new DBConnection();

public void txtBoxValidate(Control txtBoxName, string msg, Control frmName)
{
        if (String.IsNullOrEmpty(txtBoxName))
        {
            MessageBox.Show(msg, MsgBoxStyle.Exclamation, frmName.Text);
            txtBoxName.Focus();
             return;
        }
}

 public void ValChkDubANDexe(string sqlSTR_Chk, string sqlSTR_exe, Control frmName)
{
        dbCon.ExecuteSQLQuery(sqlSTR_Chk);

        if (dbCon.sqlDT.Rows.Count > 0)
        {
           MessageBox.Show("Item Already Exist in Database", frmName.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        dbCon.ExecuteSQLQuery(sqlSTR_exe);
        MessageBox.Show("Data Has Been Saved.", frmName.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

}

FormReset frmReset = new FormReset();
FormAction frmAct = new FormAction();

private void btnSave_Click(object sender, EventArgs e)
 {

    string sqlSTR_Chk;
    string sqlSTR_exe;

    frmAct.txtBoxValidate(TextBox1, "Please Enter The Age.", this);

    sqlSTR_Chk = "SELECT * FROM Table1 WHERE Field1 = '" + TextBox1 .Text + "'"; //Dublicate Check
    sqlSTR_exe = "INSERT INTO Table1(Field1) VALUES('" + TextBox1 .Text + "')";
    frmAct.ValChkDubANDexe(sqlSTR_Chk, sqlSTR_exe, this);

    frmReset.ResetAllControls(this);

}

如果我在TextBox1为空时单击按钮,我会按预期从txtBoxValidate收到错误消息,但代码不会返回(退出),并继续执行查询。

并且如果TextBox1不为空,并且如果我尝试执行具有重复值的查询,则它捕获副本并且它将用于下一个方法(重置表单)。

所以,如果TextBox1为空,请帮我停止执行查询。如果查询执行发现重复,则停止表单重置。

2 个答案:

答案 0 :(得分:0)

Ypu不会检查你的方法是返回true还是false,因为它的返回类型是void它只返回并执行下一个命令你应该更改validate方法的返回类型并检查返回的值。您应该使用参数化查询来防止SQL注入。 See this for reference

  class FormAction
{

 DBConnection dbCon = new DBConnection();

 public bool txtBoxValidate(Control txtBoxName, string msg, Control frmName)
    {
        if (String.IsNullOrEmpty(txtBoxName))
        {
            MessageBox.Show(msg, MsgBoxStyle.Exclamation, frmName.Text);
            txtBoxName.Focus();
             return false;
        }
        else
        {
         return true;
        }
     }

public void ValChkDubANDexe(string sqlSTR_Chk, string sqlSTR_exe, Control frmName)
    {
        dbCon.ExecuteSQLQuery(sqlSTR_Chk);

    if (dbCon.sqlDT.Rows.Count > 0)
    {
       MessageBox.Show("Item Already Exist in Database", frmName.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
    }

    dbCon.ExecuteSQLQuery(sqlSTR_exe);
    MessageBox.Show("Data Has Been Saved.", frmName.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

} 
 FormReset frmReset = new FormReset();
 FormAction frmAct = new FormAction();

 private void btnSave_Click(object sender, EventArgs e)
    {

string sqlSTR_Chk;
string sqlSTR_exe;

bool HasText = frmAct.txtBoxValidate(TextBox1, "Please Enter The Age.", this);
if(HasText)
{
sqlSTR_Chk = "SELECT * FROM Table1 WHERE Field1 = '" + TextBox1 .Text + "'"; //Dublicate Check
sqlSTR_exe = "INSERT INTO Table1(Field1) VALUES('" + TextBox1 .Text + "')";
frmAct.ValChkDubANDexe(sqlSTR_Chk, sqlSTR_exe, this);
}
frmReset.ResetAllControls(this);
}

答案 1 :(得分:0)

txtBoxValidate方法返回类型更改为bool,如果验证失败则返回false。

public bool txtBoxValidate(Control txtBoxName, string msg, Control frmName)
    {
        if (String.IsNullOrEmpty(txtBoxName))
        {
            MessageBox.Show(msg, MsgBoxStyle.Exclamation, frmName.Text);
            txtBoxName.Focus();
             return false;
        }
         return true;
     }

更新此部分:

 frmAct.txtBoxValidate(TextBox1, "Please Enter The Age.", this);

    sqlSTR_Chk = "SELECT * FROM Table1 WHERE Field1 = '" + TextBox1 .Text + "'"; //Dublicate Check
    sqlSTR_exe = "INSERT INTO Table1(Field1) VALUES('" + TextBox1 .Text + "')";
    frmAct.ValChkDubANDexe(sqlSTR_Chk, sqlSTR_exe, this);

    frmReset.ResetAllControls(this);
在您的btnSave_Click方法中

 var isValid = frmAct.txtBoxValidate(TextBox1, "Please Enter The Age.", this);

 if(isValid)
 {

    sqlSTR_Chk = "SELECT * FROM Table1 WHERE Field1 = '" + TextBox1 .Text + "'"; //Dublicate Check
    sqlSTR_exe = "INSERT INTO Table1(Field1) VALUES('" + TextBox1 .Text + "')";
    frmAct.ValChkDubANDexe(sqlSTR_Chk, sqlSTR_exe, this);

    frmReset.ResetAllControls(this);
 }