0

When I run my code it is not exit(return) method according to my code. I am using two different classes and when I click on the button, it is running from one method to another method and it does not exit(return) the method.

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);

}

If I click on the button while TextBox1 is empty I'm getting error message from txtBoxValidate as expected but the code isn't return(exit) and it is continues to execute the query.

and also if TextBox1 is not empty and also if I try to execute the query with duplicate value it is catching the duplicate and it is going for next method(reset form).

So, Please help me to stop execute the query if the TextBox1 is empty. and Stop the to form reset if query execute found a duplicate.

2
  • the returns in your functions just exit the current function while you call the validate function, you do nothing with it to prevent further execution Commented Aug 17, 2015 at 8:50
  • On a side note, consider using sql parameters. Commented Aug 17, 2015 at 9:04

2 Answers 2

0

Change txtBoxValidate method return type to bool and return false if the validation fails.

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;
     }

update this section:

 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);

to this in your btnSave_Click method:

 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);
 }
Sign up to request clarification or add additional context in comments.

2 Comments

getting an error -- Cannot assign void to an implicitly-typed local variable" --
have you updated the return type for txtBoxValidate method from void to bool?
0

Ypu are not checking if your method returns true or false, since its return type is void it just returns and next command is executed you should change the return type of validate method and check the returned value. you should use parameterized queries to prevent SQL injections. 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);
}

2 Comments

getting an error -- Cannot implicitly convert type 'void' to 'bool' --
Have you changed the return type of method , public bool txtBoxValidate ? (as shown in code) and don't forget to add return values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.