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.

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
eRFAN MD
  • 3
  • 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 – BugFinder Aug 17 '15 at 08:50
  • On a side note, consider using sql parameters. – Bauss Aug 17 '15 at 09:04

2 Answers2

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);
}
Community
  • 1
  • 1
Kryptonian
  • 860
  • 3
  • 10
  • 26
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);
 }
Oluwafemi
  • 14,243
  • 11
  • 43
  • 59