Puja Surya
Puja Surya

Reputation: 1287

delete query not working in c#

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?

Upvotes: 1

Views: 171

Answers (1)

gzaxx
gzaxx

Reputation: 17590

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

Upvotes: 2

Related Questions