Hi Gerdus, thank you for the link, but it still can solve my problem.
So here is my code in a static class called ARHelper:
Public NotInheritable Class ARHelper
Public Shared session As NHibernate.ISession
Public Shared Function ExecSPNonQueryForTransaction(ByVal SPName
As String, _
ByVal pSession
As NHibernate.ISession, _
ByVal
ParamArray SPParameters() As IDataParameter) As Integer
Dim count As Integer = 0
session = pSession
Dim command As IDbCommand = session.Connection.CreateCommand()
command.CommandText = SPName
command.CommandType = CommandType.StoredProcedure
For Each param As IDataParameter In SPParameters
command.Parameters.Add(param)
Next
count = command.ExecuteNonQuery()
Return count
End Function
End Class
Now in one of my aspx page, I write something like this:
Protected Sub btnCreateDinner_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles btnCreateDinner.Click
Dim transaction As New Castle.ActiveRecord.TransactionScope()
Try
Dim dinner As New Dinner()
dinner.Title = txtTitle.Text
dinner.EventDate = Now.AddDays(7)
dinner.Description = txtDescription.Text
dinner.HostedBy = "Castle.Dinner"
dinner.ContactPhone = txtContactPhone.Text
dinner.Address = txtAddress.Text
dinner.Country = txtCountry.Text
dinner.Latitude = 10
dinner.Longitude = 100
' Didn't need to write this.
' Provided by ActiveRecordBase
dinner.Save()
Dim pDinnerID As New SqlClient.SqlParameter()
pDinnerID.ParameterName = "@DinnerID"
pDinnerID.Value = 1
Dim pAttendeeName As New SqlClient.SqlParameter()
pDinnerID.ParameterName = "@AttendeeName"
pDinnerID.Value = "UNLUCKY.GUY"
ARHelper.ExecSPNonQueryForTransaction("spr_InsertRSVP",
transaction.GetSession(transaction), pDinnerID, pAttendeeName)
Catch ex As Exception
transaction.VoteRollBack()
Throw
Finally
transaction.Dispose()
End Try
End Sub
When I run my application, it will result in an exception at
transaction.GetSession(transaction), I really confuse what to put as
the parameter for GetSession method.
Anybody please help me.
Thanks a lot.
Regards.
On 2 Sep, 20:21, Gerdus van Zyl <[email protected]> wrote:
> The code I gave is probably wrong since it does not use the current
> session I think. (I have moved on from using activerecord sessions and
> now just use AR for mapping,config and handle nhibernate sessions
> directly)
>
> Anyway see this blog post I just
> found:http://www.kenegozi.com/Blog/2008/02/28/executing-plain-ol-sql-in-act...
>
> ~G
>
>
>
> On Wed, Sep 2, 2009 at 12:36 PM, the.wizard<[email protected]> wrote:
>
> > Hello Gerdus,
> > Thank you for replying my post. I still don't understand what you are
> > explain in your post, please explain it more detail.
> > My goal is to save a data using active record object and execute a dml
> > stored procedure in one db transaction.
> > I have create a method that accept ISession object from parameter,
> > then retrieve the command object from the ISession object. After that
> > the command object will be use with ADO.NET to execute a dml stored
> > procedure (using executenonquery method).
> > Then I create a transactionscope just like you explain, but then I can
> > get the session object from this transactionscope object. It has a
> > GetSession method, but it need one parameter, a key that is an object
> > type which I don't know what should I put for get it working. Have
> > tried a lot but all failed getting an exception.
> > Please help me, thanks a lot.
> > Regards,
> > the.wizard
>
> > On 2 Sep, 13:50, Gerdus van Zyl <[email protected]> wrote:
> >> I think you need to create a transactionscope yourself like:
> >> TransactionScope transaction = new TransactionScope();
> >> then you can use and pass it around as you like. Or what am I missing?
>
> >> And then you need a function like this to get the database connection
> >> and enlist it in the transaction:
> >> Public Function getSQLCommand() As IDbCommand
> >> Dim sess As ISession
> >> 'activerecord kry session wat connection bevat
> >> sess =
> >> ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(GetType(ActiveRecordBase))
>
> >> 'kry connnection vanaf activerecord session en create command
> >> Dim com As IDbCommand = sess.Connection.CreateCommand()
> >> If sess.Transaction.IsActive Then
> >> sess.Transaction.Enlist(com)
> >> End If
>
> >> Return com
> >> End Function
>
> >> ~Gerdus
>
> >> On Wed, Sep 2, 2009 at 3:46 AM, the.wizard<[email protected]> wrote:
>
> >> > Hi everyone,
> >> > Does anyone know how to get the session object in castle active record
> >> > when doing a transaction? I want to perform a transaction that consist
> >> > of a direct save using Castle Active Record object, and using a stored
> >> > procedure, and I need the session object to pass it to my method that
> >> > execute my stored procedure.
> >> > Please help me, I have already frustated with this problem, have been
> >> > looking all over internet, but can't find one to solve this problem.
> >> > Really thanks a lot.
>
> >> > Regards.- Sembunyikan teks kutipan -
>
> >> - Perlihatkan teks kutipan -- Sembunyikan teks kutipan -
>
> - Perlihatkan teks kutipan -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---