Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to delete the records in InvoiceItems and TAXINVOICE tables.But there is some query distraction occuring while writing to Ole db statement.I am trying like this.The exact tables in MS access are InvoiceItems and TAXINVOICE. The InvoiceItems has the following fields as InvoiceItem,InvoiceNumber,ProductId,Quantity,UOM,UnitPrice,GrossAmount,VAT,VatAmount,
Total
and TAXINVOICE has fields of INVOICENO,MOBILENUMBER,CUSTOMERNAME,ADDRESS1,ADDRESS2,TINNO,PONO,TRANSPORT,BILLDATE,
InvoiceType,PODATE,totalGrossAmt,totalVatAmt,BILLAMT,TRANSPORTAMT,OTHERAMT,FINALAMT,
ROUNDOFF and GRANDTOTAL

What I have tried:

cmd = new OleDbCommand("DELETE InvoiceNumber,TAXINVOICE FROM InvoiceItems INNER JOIN TAXINVOICE WHERE InvoiceItems.InvoiceNumber=TAXINVOICE.INVOICENO and InvoiceNumber=" + txtInvoice.ToString() , conn);
Posted
Updated 17-Oct-16 4:22am

If you set the appropriate foreign keys, you could delete from one table, and the database will delete related rows in the other table, and you then you won't have to use overly complex queries.
 
Share this answer
 
If you need to delete rows from both tables, try to do it separately. For example
C#
"DELETE FROM InvoiceItems 
WHERE InvoiceNumber=TAXINVOICE.INVOICENO 
and InvoiceNumber=" + txtInvoice.ToString() 

"DELETE FROM TaxInvoice
WHERE InvoiceNo=" + txtInvoice.ToString()

However, in order to be safe from SQL injections you really should use OleDbParameter Class (System.Data.OleDb)[^]. In other words
C#
"DELETE FROM InvoiceItems 
WHERE InvoiceNumber=TAXINVOICE.INVOICENO 
and InvoiceNumber=?"

"DELETE FROM TaxInvoice
WHERE InvoiceNo=?"

And as stated in solution 1 the easiest way would be to define a foreign key and let the database handle the child rows. Have a look at CONSTRAINT Clause (Microsoft Access SQL)[^]
 
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