Cannot access a disposed object - SqlServerBlobStream

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Cannot access a disposed object - SqlServerBlobStream

Loc Pham
Hi,

I'm trying to use SqlServerUploadStorageProvider in order to steam my upload directly to the DB. I tried follow the manual but I'm getting the following error:

Cannot access a disposed object.
Object name: 'SqlServerBlobStream'.

Here is my aspx file:
-------------------
    <cc1:SqlServerInputFile ID="SqlServerInputFile1" runat="server" />   
    <asp:Button ID="Button1" runat="server" Text="Upload" />

Here is my code behind:
--------------------

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
       
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += new System.EventHandler(this.Button_Clicked);
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            if (!this.IsValid)
            {                
                return;
            }
           
            if (SqlServerInputFile1.HasFile)
            {                  
                System.IO.Stream content = SqlServerInputFile1.FileContent;
                int bytesToRead = (int)SqlServerInputFile1.ContentLength;
                byte[] buf = new byte[4096];
                while (bytesToRead > 0)
                {
                    bytesToRead -= content.Read(buf, 0, Math.Min(bytesToRead, buf.Length));
                }

                content.Close();
                SqlServerInputFile1.Verify();
            }          
        }

The error indicates that 'content' Stream may have been referenced after it was disposed.  Below is the stack strace:

Exception Details: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SqlServerBlobStream'.

Source Error:

Line 49:                 while (bytesToRead > 0)
Line 50:                 {
Line 51:                     bytesToRead -= content.Read(buf, 0, Math.Min(bytesToRead, buf.Length));
Line 52:                 }
Line 53:

Source File: F:\Loc\Misc\UploaderToSql\UploaderToSql\Default.aspx.cs    Line: 51

Stack Trace:

[ObjectDisposedException: Cannot access a disposed object.
Object name: 'SqlServerBlobStream'.]
   Hitone.Web.SqlServerUploader.SqlServerBlobStream.Read(Byte[] buffer, Int32 offset, Int32 count) +134
   UploaderToSql._Default.Button_Clicked(Object sender, EventArgs e) in F:\Loc\Misc\UploaderToSql\UploaderToSql\Default.aspx.cs:51
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563


Please advice what may have caused this?
Thanks,
-Loc