Solved

Upload files data and Info to the database

Posted on 2012-04-11
3
343 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

25 Experts available now in Live!

Get 1:1 Help Now