-1

im new with sql and c# and im having an error in executenonquery commandtex. i dont know where the error is. can you guys help me out here?

private void submitBtn_Click(object sender, EventArgs e)
{
    con.Open();

    string a = "Accept";
    string b = "Reject";
    string queryUpdate1 = "";
    string queryUpdate2 = "";
    int row = DGVLeaves.CurrentCell.RowIndex;


    if (accptBtn.Checked)
    {
        if (type_rdonly.Text == "SL")
        {
            if (ifEmployeeExist(con, emptime_rdonly.Text))
            {
                queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
            }
            queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_SL = (L_SPENT_SL + 1), L_REM_SL = (L_REM_SL - 1)";
        }
        if (type_rdonly.Text == "VL")
        {
            if (ifEmployeeExist(con, emptime_rdonly.Text))
            {
                queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
            }
            queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_VL = (L_SPENT_VL + 1),L_REM_VL = (L_REM_VL - 1)";
        }
        SqlCommand cmd1 = new SqlCommand(queryUpdate1, con);
        SqlCommand cmd2 = new SqlCommand(queryUpdate2, con);
        cmd2.ExecuteNonQuery();
        cmd1.ExecuteNonQuery();
    }
    if (rejBtn.Checked)
    {
        if (ifEmployeeExist(con, emptime_rdonly.Text))
        {
            queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + b + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
        }
        SqlCommand cmd1 = new SqlCommand(queryUpdate1, con);
        cmd1.ExecuteNonQuery();
    }
    con.Close();
}
atline
  • 28,355
  • 16
  • 77
  • 113
  • Did you debug the code? Did you check what value you are getting in `type_rdonly.Text` ? – Chetan Jan 23 '19 at 02:41
  • @ChetanRanpariya hI! its SL and VL. the SL and VL came from a datagridview. i tried to debug the code by using if (!string.IsNullOrEmpty(queryUpdate1)) { cmd1.CommandText = queryUpdate1; cmd1.ExecuteNonQuery(); } con.Close(); but its not upadting sa sql – Pauline Agawin Jan 23 '19 at 02:47
  • Did you check what value is being returned from `ifEmployeeExist` method? If you have `SL` or `VL` in `type_rdonly.Text` then `ifEmployeeExist` is returning false and that causes `queryUpdate1` not to be initialized. Try debugging your code with `ifEmployeeExist` method, you should be able to figure out the issue. – Chetan Jan 23 '19 at 02:59

1 Answers1

0

I think there is too many if clause, you might miss something in the process. Try to debug and see if the variable queryUpdate1 or queryUpdate2 is empty before ExecuteNonQuery command is executed. If its empty, that should be the cause

Ive adjusted the code for you, hope this helps

private void submitBtn_Click(object sender, EventArgs e)
    {

        string a = "Accept";
        string b = "Reject";
        string queryUpdate1 = "";
        string queryUpdate2 = "";
        int row = DGVLeaves.CurrentCell.RowIndex;

        if (accptBtn.Checked)
        {
            if (type_rdonly.Text == "SL")
            {
                if (ifEmployeeExist(con, emptime_rdonly.Text))
                {
                    queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
                }
                queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_SL = (L_SPENT_SL + 1), L_REM_SL = (L_REM_SL - 1)";
            }
            if (type_rdonly.Text == "VL")
            {
                if (ifEmployeeExist(con, emptime_rdonly.Text))
                {
                    queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + a + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
                }
                queryUpdate2 = "UPDATE LEAVE_ADMIN SET L_SPENT_VL = (L_SPENT_VL + 1),L_REM_VL = (L_REM_VL - 1)";
            }
        }

        else if (rejBtn.Checked)
        {
            if (ifEmployeeExist(con, emptime_rdonly.Text))
            {
                queryUpdate1 = @"UPDATE [LEAVE_EMP] SET EMP_STATUS ='" + b + "'WHERE [EMP_TIME] ='" + emptime_rdonly.Text + "'";
            }
        }

        con.Open();

        SqlCommand cmd = new SqlCommand() { Connection = con, CommandType = System.Data.CommandType.Text };
        if (!string.IsNullOrEmpty(queryUpdate1)) {
            cmd.CommandText = queryUpdate1;
            cmd.ExecuteNonQuery();
        }

        if (!string.IsNullOrEmpty(queryUpdate2))
        {
            cmd.CommandText = queryUpdate2;
            cmd.ExecuteNonQuery();
        }

        if (string.IsNullOrEmpty(queryUpdate1) && string.IsNullOrEmpty(queryUpdate2))
        {
            MessageBox.Show("Empty query");
        }

        con.Close();
    }
mysayasan
  • 39
  • 7
  • hi! i tried to debug it by using if (!string.IsNullOrEmpty(queryUpdate1)) { cmd1.CommandText = queryUpdate1; cmd1.ExecuteNonQuery(); } con.Close(); because you're right, it might be of too many clause but i dont why would queryUpdate 1 and 2 is empty – Pauline Agawin Jan 23 '19 at 02:52
  • hi! thank you for adjusting my code but unfortunately, it didnt work. it was working yesterday and i checked this morning and then this happened. is there an incident where c# suddenly didnt work? – Pauline Agawin Jan 23 '19 at 04:14