Results 1 to 3 of 3

Thread: Transactions in ADO.NET

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182

    Transactions in ADO.NET

    I am trying to use transaction to archive a data, but I receive such error:
    ‘Connection property has not been initialized’.
    It seems that the line:
    cmd.Transaction = trn

    did not work. What needs to be changed?


    Dim cnn As SqlClient.SqlConnection = _
    New SqlClient.SqlConnection(SQLCONNECTIONSTRING)
    cnn.Open()

    Dim trn As SqlClient.SqlTransaction = cnn.BeginTransaction
    Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
    Dim prm As SqlClient.SqlParameter

    Try
    ' Start transaction
    cmd.Transaction = trn
    cmd.CommandText = "procArhivingPurchases"
    cmd.CommandType = CommandType.StoredProcedure
    prm = cmd.Parameters.Add("@Date", _
    SqlDbType.VarChar, 25)
    prm.Value = ConvertDateTime(GetYearToArchive(), GetMonthToArchive())

    cmd.ExecuteNonQuery()


    trn.Commit()
    MsgBox("Successfully archived all data", MsgBoxStyle.Information)
    Catch ex As Exception
    trn.Rollback()
    MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "Failed to archive the data")
    Finally

    If cnn.State <> ConnectionState.Closed Then
    cnn.Close()
    End If

    cnn = Nothing
    trn = Nothing
    cmd = Nothing
    prm = Nothing
    End Try

  2. #2
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    You haven't told the command object about the connection object.
    Insert the following:
    cmd.Connection = cnn
    Wind and waves resolves all problems.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182
    Thanks,
    I will try it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width