1

i use sql server in my database

and here is the code

private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                    //GlobalClass.dt.Rows[rowId].Delete();
                    //GlobalClass.adap.Update(GlobalClcass.dt);

                cDatabaseSQLServer.Delete("satuan", "WHERE id = " + rowId + "");
                    //this.Close();  
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

public bool Delete(String tableName, String where)
        {
            switch (sqlType)
            {
                case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_SQLITE:
                    return cSQLite.Delete(tableName, where);
                case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_MSSQL:
                    return cSQL.Delete(tableName, where);
            }
            return false;
        }

public bool Delete(String tableName, String where)
        {
            Boolean returnCode = true;
            try
            {
                this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
            }
            catch (Exception fail)
            {
                MessageBox.Show(fail.Message);
                returnCode = false;
            }
            return returnCode;
        }

when i debug the application , the delete is not working and the data still exist in datagridview, how to fix that?

4
  • 1
    Looks like you would end up with two WHERE in a row. Also use parameterised queries. Commented Jul 8, 2013 at 9:03
  • 3
    You've got quite a big security hole in the Delete function. What if I pass ; drop all as my where argument? Commented Jul 8, 2013 at 9:03
  • cDatabaseSQLServer.Delete("satuan", "WHERE id = " + rowId + ""); seems that WHERE keyword is getting sent in query 2 times.... remove the WHERE keyword Commented Jul 8, 2013 at 9:04
  • @all . i forgot to remove WHERE in my query. in delete method. @Jeff Foster . i try to pass drop all in my Where argument but not working. Commented Jul 8, 2013 at 9:19

1 Answer 1

2

Your query is wrong, you have 2 times where, either change your method call or query creator:

cDatabaseSQLServer.Delete("satuan", "id = " + rowId + ""); //remove where from here

as it is here:

this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
Sign up to request clarification or add additional context in comments.

Comments

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.