Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'm trying to save documents into my sql database using SqlFileStream. the database filestream setup seems to be working ok and when i run a query on to add a document it seems to be saving it but when i try saving a document through vb.net i'm getting this error

"An uncommittable transaction was detected at the beginning of the batch. The transaction was rolled back. This was caused by an error that occurred during the processing of a FILESTREAM request in the context of this transaction."

here is my vb.net code

VB
'Declare Database objects
           Dim SQLCon As SqlConnection
           Dim objThisTransaction As SqlTransaction
           Dim objThisCommand As New SqlCommand


           Dim strPath As String
           Dim objPatIdParam As SqlParameter
           Dim objFileTypeParam As SqlParameter
           Dim objFilePathParam As SqlParameter
           Dim objActionTakenByParam As SqlParameter

           Dim objDataAdapter As New SqlDataAdapter


           Dim buffer As Byte() = New Byte(CInt(FileUpload1.FileContent.Length) - 1) {}
           FileUpload1.FileContent.Read(buffer, 0, buffer.Length)
           If FileUpload1.FileContent.Length > 0 Then

               Dim conn As String = "Data Source=.;Initial Catalog=Review;Integrated Security=true;Trusted_Connection=Yes;"
               SQLCon = New SqlConnection(conn)
               SQLCon.Open()

               objThisTransaction = SQLCon.BeginTransaction()

               ' Specify the details of the Stored Procedure to execute
               objThisCommand = New SqlCommand("proc_Review_put_StreamFiles", SQLCon, objThisTransaction)
               objThisCommand.CommandType = CommandType.StoredProcedure

               ' Clear the parameters list
               objThisCommand.Parameters.Clear()

               objPatIdParam = objThisCommand.Parameters.Add("@PatId", SqlDbType.VarChar, 75)
               objPatIdParam.Value = Session("PatId").ToString
               objFileTypeParam = objThisCommand.Parameters.Add("@FileType", SqlDbType.VarChar, 5)
               objFileTypeParam.Value = System.IO.Path.GetExtension(FileUpload1.FileName)
               objFilePathParam = objThisCommand.Parameters.Add("@FilePath", SqlDbType.VarChar, -1)
               objFilePathParam.Direction = ParameterDirection.Output
               objActionTakenByParam = objThisCommand.Parameters.Add("@ActionTakenBy", SqlDbType.VarChar, 50)
               objActionTakenByParam.Value = Session("UserId").ToString()
               ' Now execute the stored procedure
               objThisCommand.ExecuteNonQuery()

               'file path of the document
               strPath = CStr(objFilePathParam.Value)

               objThisCommand = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", SQLCon, objThisTransaction)
               Dim objContext As Byte() = DirectCast(objThisCommand.ExecuteScalar(), Byte())


               Dim objSqlFileStream As New SqlFileStream(strPath, objContext, FileAccess.Write)

               objSqlFileStream.Write(Buffer, 0, Buffer.Length)
               objSqlFileStream.Close()

               objThisTransaction.Commit()
               SQLCon.Close()
               'imp.ImpersonateStop()


           End If



the code seems to fail on "
VB
objThisTransaction.Commit()

"
Posted

1 solution

Does your query return a large amount of data? if so this article[^] state

"When your query returns a large amount of data and calls BeginTransaction, a SqlException is thrown because SQL Server 2005 does not allow parallel transactions when using MARS. To avoid this problem, always associate a transaction with the command, the connection, or both before any readers are open."
 
Share this answer
 
Comments
Dylan Morley 20-Mar-12 7:59am    
Comment from OP:

not really, the files are not that big but i'll retest the codes using big and small files to see if it makes any difference. thanks for the suggestion. i spent the whole day rattling my brains out but couldnt figure out what's wrong with the code.
Dylan Morley 20-Mar-12 8:00am    
Comment from OP:

i'm still getting the same error even if i use big or small files. Do you think it's to do with "Integrated Security=true"? Maybe i miss some setup at the backend?

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