Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 850875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:20:12+00:00 2026-05-15T07:20:12+00:00

I have some code that allows users to upload file attachments into a varbinary(max)

  • 0

I have some code that allows users to upload file attachments into a varbinary(max) column in SQL Server from their web browser. It has been working perfectly fine for almost two years, but all of a sudden it stopped working. And it stopped working on only the production database server — it still works fine on the development server.

I can only conclude that the code is fine and there is something up with the instance of SQL Server itself. But I have no idea how to isolate the problem.

I insert a record into the ATTACHMENT table, only inserting non-binary data like the title and the content type, and then chunk-upload the uploaded file using the following code:

// get the file stream

 System.IO.Stream fileStream = postedFile.InputStream;

// make an upload buffer

byte[] fileBuffer;
fileBuffer = new byte[1024];

// make an update command

SqlCommand fileUpdateCommand = new SqlCommand("update ATTACHMENT set ATTACHMENT_DATA.WRITE(@Data, NULL, NULL) where ATTACHMENT_ID = @ATTACHMENT_ID", sqlConnection, sqlTransaction);

fileUpdateCommand.Parameters.Add("@Data", SqlDbType.Binary);
fileUpdateCommand.Parameters.AddWithValue("@ATTACHMENT_ID", newId);

while (fileStream.Read(fileBuffer, 0, fileBuffer.Length) > 0)
{

     fileUpdateCommand.Parameters["@Data"].Value = fileBuffer;
     fileUpdateCommand.ExecuteNonQuery(); <------ FAILS HERE

}

fileUpdateCommand.Dispose();
fileStream.Close();

Where it says “FAILS HERE”, it sits for a while and then I get a SQL Server timeout error on the very first iteration through the loop. If I connect to the development database instead, everything works fine (it runs through the loop many, many times and the commit is successful).

Both servers are identical (SQL Server 9.0.3042) and the schemas are identical as well.

When I open Activity Monitor right after the timeout to see what’s going it, it says the last command is

(@Data binary(1024),@ATTACHMENT_ID decimal(4,0))update ATTACHMENT set ATTACHMENT_DATA.WRITE(@Data, NULL, NULL) where ATTACHMENT_ID = @ATTACHMENT_ID

which I would expect but it also says it has a status of “Suspended” and a wait type of “PAGEIOLATCH_SH”. I looked this up and it seems to be a bad thing but I can’t find anything specific to my stuation.

Ideas?

OK, here’s the output from sys.dm_exec_requests:

session_id request_id  start_time              status                         command          sql_handle                                                                                                                         statement_start_offset statement_end_offset plan_handle                                                                                                                        database_id user_id     connection_id                        blocking_session_id wait_type                                                    wait_time   last_wait_type                                               wait_resource                                                                                                                                                                                                                                                    open_transaction_count open_resultset_count transaction_id       context_info                                                                                                                                                                                                                                                       percent_complete estimated_completion_time cpu_time    total_elapsed_time scheduler_id task_address       reads                writes               logical_reads        text_size   language                                                                                                                         date_format date_first quoted_identifier arithabort ansi_null_dflt_on ansi_defaults ansi_warnings ansi_padding ansi_nulls concat_null_yields_null transaction_isolation_level lock_timeout deadlock_priority row_count            prev_error  nest_level  granted_query_memory executing_managed_code
---------- ----------- ----------------------- ------------------------------ ---------------- ---------------------------------------------------------------------------------------------------------------------------------- ---------------------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------- ----------- ----------- ------------------------------------ ------------------- ------------------------------------------------------------ ----------- ------------------------------------------------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------- -------------------- -------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------- ------------------------- ----------- ------------------ ------------ ------------------ -------------------- -------------------- -------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ----------- ---------- ----------------- ---------- ----------------- ------------- ------------- ------------ ---------- ----------------------- --------------------------- ------------ ----------------- -------------------- ----------- ----------- -------------------- ----------------------
59         0           2010-06-16 10:49:26.130 suspended                      UPDATE           0x020000003499A22E28282311CAF8CE0730402C45DCD89961                                                                                 0                      0                    0x06000E003499A22EB881FD73000000000000000000000000                                                                                 14          5           4AB28A88-B4B9-4B54-B4F4-311D42C5EA29 0                   PAGEIOLATCH_SH                                               0           PAGEIOLATCH_SH                                               14:1:547920                                                                                                                                                                                                                                                      1                      1                    226115095            0x                                                                                                                                                                                                                                                                 0                0                         187         3687               0            0x0088B3D8         921                  0                    240166               2147483647  us_english                                                                                                                       mdy         7          1                 1          1                 0             1             1            1          1                       2                           -1           0                 1                    0           3           490                  0

(1 row(s) affected)

EDIT:

OK, I rebooted, no dice (needed to install some patches anyway). Deleted and rebuilt the PK on ATTACHMENT_ID, no dice. Then I rebuilt the statistics for the table and now it seems happy. I can only guess there was something off in the statistics that was causing the query optimizer to freak out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:20:13+00:00Added an answer on May 15, 2026 at 7:20 am

    From the symptoms it looks like the query times out due to having to perform a table scan. Does the table ATTACHMENT has an index on ATTACHMENT_ID on both servers? Can you double check? If the schemas are identical but both are missing the index, do the tables have comparable size? Could be that the scan on test machine lasts less than 30 seconds, while the one on production lasts enough to time out. In which case you need and index on ATTACHMENT_ID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that loads a default configuration file and then allows users
I have some code that uses the SQL Server 2005 SMO objects to backup
I have some code that allows administrators to manage their organization's users/groups etc and
I have made a page that allows users to upload files to the server
I have a winform program that allows users to upload Shockwave Flash movies to
I need some code that allows users to add their own markers to my
I have an application that allows users to write their own code in a
I have a web form that allows users to upload files while opening a
I have a working code which allows users to insert url's into a database,
I have a form that takes some user details, and allows them to upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.