Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Upload files data and Info to the database

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 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