Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

Can someone help me with the code below. I want to delete a tuble in the database by selecting a date.

Code:
C#
public string DeleteWatchReceipt(DateTime date) 
        {
            OleDbConnection conn = new OleDbConnection("My DB path"));

            
            string sqlinsert = @"DELETE WatchreceiptID FROM Watchreceipt WHERE Dato = " + date;
            conn.Open();
            OleDbTransaction sqltrans = conn.BeginTransaction();
            
            string result = null;

            try
            {
                OleDbCommand cmdinsert = conn.CreateCommand();
                cmdinsert.CommandText = sqlinsert;
                cmdinsert.Transaction = sqltrans;

                cmdinsert.ExecuteNonQuery();
                sqltrans.Commit();

                result = "Ok";
            }
            catch (OleDbException odbe)
            {
                sqltrans.Rollback();
                System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return result;
        }

Thanks
Posted
Updated 28-Jul-12 0:37am
v2
Comments
AshishChaudha 28-Jul-12 6:39am    
and whats the issue??? Where you got stuck???

Date should be quoted :
C#
string sqlinsert = @"DELETE WatchreceiptID FROM Watchreceipt WHERE Dato = '" + date + "'";
// preferably : date.ToString("yyyy-MM-dd");
 
Share this answer
 
Thanks

The code should be like that

public string DeleteWatchReceipt(DateTime date)
{
OleDbConnection conn = new OleDbConnection("My DB path"));


string sqlinsert = @"DELETE WatchreceiptID FROM Watchreceipt WHERE Dato = @date";
conn.Open();
OleDbTransaction sqltrans = conn.BeginTransaction();
OleDbCommand cmdinsert = conn.CreateCommand();

cmdinsert.Parameters.AddWithValue("@date", date);

string result = null;

try
{

cmdinsert.CommandText = sqlinsert;
cmdinsert.Transaction = sqltrans;

cmdinsert.ExecuteNonQuery();
sqltrans.Commit();

result = "Ok";
}
catch (OleDbException odbe)
{
sqltrans.Rollback();
System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
}
finally
{
conn.Close();
}

return result;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900