Database not getting updated
Hello everybody in the problem solving world !!!!.........
Well I have come across a unique problem and am not able to find any logic behind that.
My vb.net application runs on a sql server 2000 and it is about making bills in a fast running gift counter with 5 to 6 client and server is placed at a distance of 200 meters. The store sells maximum of 50-60 gifts. The user does all the transaction through clicks.
Two tables are involved for making a transaction. One is mastertable and detailtable.
In the mastertable my application is inserting the total of the bill, date, amount and the counter personís code. It then generates an auto number which I picks up after the data is inserted in the mastertable and then uses the same in the detailtable.
The detailtable holds fields like item, qty, rate,id of mastertable etc.
My application is running smooth, but recently I noticed that say one in 500th time, row is not added to mastertable while it is present in detailtable with the id of mastertable. I have binded the sql with commit. I have digged my code but am not finding any logic behind this error.
Please help. Thanks in advance.
Just to clarify, you have your inserts (master and detail) wrapped in a transaction and you still have orphan detail records (detail records with no matching master). This occurs occasionally (1 in 500).
Do the users have the ability to delete records? If so are you cascading deletes to remove the detail records also?
Well, thanks for the reply...
Yes you try understood the problem.
But as you said that it happens 1 in 500...
1. Why is it so?
2. What preventions to take?
3. Lastly how to solve it out as we just cannot afford a single miss also.
As for deletion, the user doesnt have any option to delete.
Regards and thanks once again.
Obviously, if you've wrapped the inserts inside a transaction correctly it shouldn't be happening. Are you sure your trapping any insert error and rolling back the transaction if they occur? Can you create a log file to track the inserts? Without actually seeing an example of the code your using it would be hard to identify just where your problem might be. These type of issues can be prevented through code as well as correctly setting up relationships/constraints on your database tables. If your inserts are occuring without error, then something has to be deleting the master record after the fact.
Here is the code:
con = connect()
'transaction begins here-------
trans = con.BeginTransaction()
'Data is inserted into mastertable---------
cmd = New SqlClient.SqlCommand("INSERT INTO mastertable(mbno,mbdate,mbamount,mbcperson) VALUES(" & counter_bill_no & ",'" & server_date & "'," & billvalue & "," & countercode & ")", con, trans)
cmd = New SqlClient.SqlCommand("select mbid from mastertable WHERE mbno=" & counter_bill_no & " and mbcperson=" & countercode & " ", con, trans)
mb_id = cmd.ExecuteScalar
'Data inserted into detailtable-----------
i = 0
For i = 0 To row_no - 1
selling_rate = DataGridView1.Item(2, i).Value
If selling_rate <> 0 Then
cmd = New SqlClient.SqlCommand("INSERT INTO detailtable(dbno,dbdate,dbicode,biquantity,dbamount) VALUES(" & mb_id & ",'" & server_date & "'," & DataGridView1.Item(4, i).Value & "," & DataGridView1.Item(1, i).Value & "," & DataGridView1.Item(3, i).Value & ")", con, trans)
Catch ex As Exception
MsgBox("Bill could not be generated, Please try again.")
Since your retrieving the ID from the master table after the insert and before the insert of the detail records. It must have been inserted correctly. If it's missing at a later date, it was deleted somehow. Do you have cascading deletes setup on the tables? If so the orphan detail records should be deleted also.
Originally Posted by vsc33
Have you considered retrieving the database-generated identity value with "Select Scope_Identity()" instead of the select statement that you are currently using?
By binnymann in forum Database
Last Post: 04-13-2008, 01:23 AM
By kamlesh_sharma in forum Database
Last Post: 08-23-2006, 12:21 AM
Last Post: 06-05-2002, 09:07 AM
By Russ in forum Database
Last Post: 06-04-2002, 12:57 AM
By Michael Tzoanos in forum Database
Last Post: 04-12-2002, 11:19 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL