Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Upload files data and Info to the database

Posted on 2012-04-11
3
Medium Priority
?
353 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
[X]
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
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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