Solved

Upload files data and Info to the database

Posted on 2012-04-11
3
342 Views
Last Modified: 2012-04-29
I have a method in my repository that uses the command object to add files data and Info to the database. I have 2 pages that uses this method. One page works every time 100% of the time. The second location never works. I have being dealing with this for a c full 24 hours now. The script fails when I attempt the retrieval of the output ID (fileId). I get the “Object reference not set to an instance of an object.” What I don’t understand is why it works from one page and not the other. I could really use your insight

Thank you

uploadsql.txt
0
Comment
Question by:ruffone
  • 2
3 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37836266
i would suggest two things here:-

1. i could see transaction code with only commit, there is no rollback option.
2. Why would you need to use sqluploadstream, you can write your binary/buffer of bytes directly to sql server using ExecuteNonQuery. It will be much easier and straight forward.

Also please ensure that you are closing the connection after using, and every DB Write is opening up new connection. This exception generally happens when you try to use disposed object.
0
 
LVL 4

Accepted Solution

by:
ruffone earned 0 total points
ID: 37889297
Adding the line "file.fileData.Seek(0, SeekOrigin.Begin)" fixed the issue
 
        Using conn1 As SqlConnection = GetConnection()
            Using trn As SqlTransaction = conn1.BeginTransaction()
                Dim cmdInsert As New SqlCommand(SQL_INSERT, conn1, trn)
                cmdInsert.Parameters.Add("@fileData", SqlDbType.VarBinary, -1)
                cmdInsert.Parameters.Add("@fileGuid", SqlDbType.UniqueIdentifier)
                cmdInsert.Parameters("@fileGuid").Value = file.fileGuid
                cmdInsert.Parameters.Add("@fileId", SqlDbType.BigInt)
                cmdInsert.Parameters("@fileId").Direction = ParameterDirection.Output
                Dim cmdUpdate As New SqlCommand(SQL_UPDATE, conn1, trn)
                cmdUpdate.Parameters.Add("@fileData", SqlDbType.VarBinary, -1)
                cmdUpdate.Parameters.Add("@fileGuid", SqlDbType.UniqueIdentifier)
                cmdUpdate.Parameters("@fileGuid").Value = file.fileGuid
                Using uploadStream As Stream = _
                    New BufferedStream(
                    New SqlStreamUpload() _
                    With {.InsertCommand = cmdInsert,
                          .UpdateCommand = cmdUpdate,
                          .InsertDataParam = cmdInsert.Parameters("@fileData"),
                          .UpdateDataParam = cmdUpdate.Parameters("@fileData")
                         }, 8040)
                    file.fileData.CopyTo(uploadStream)
                    file.fileData.Seek(0, SeekOrigin.Begin)
                End Using
                trn.Commit()
                ret = Convert.ToInt64(cmdInsert.Parameters("@fileId").Value.ToString())
            End Using

Open in new window

0
 
LVL 4

Author Closing Comment

by:ruffone
ID: 37907552
Thank you
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now