Saturday, November 19, 2011

Transaction handling in .net

Transaction is a unit of manipulation on the basis of all .This is recommended when the manips are interrelated
Ado .net is providing transaction class and begin transaction method with connection class to implement transaction handling.

I have given a simple example on transaction handling .For this i have used three text boxes and  one button in windows application.

Transaction.aspx.cs :
Dim con As New SqlConnection("userid=sa;passed=;databse=invetary")
Dim cmd As New SqlCommand()
Dim t As SqlTransaction

Try
con.open()
t=con.BeginTransaction()
cmd.connection=con
cmd.Transaction=t
cmd.commandText="insert into trans values("&t1.text&","&t2.text&","&t3.text&")"
cmd.excutionQuery()
cmd.CommandText="update item set Qoh=qoh="&t3.text&" where itemno="&t2.text""
Dim n As byte
n=cmd. excutenonQuery()
if n=0 then
Throw New invalidTransException("item not found")
End if
t.Commit()
msgbox("Transaction Successfully")

Catch ex As Exception
t.rollback()
msgbox("Trans failed"&ex.message)

Finally
con.close()

EndTrancation
End Sub
End Class

No comments:

Bel