tags:

views:

19

answers:

2

Hello all,

I have a strange thing going on with the .net fileupload control. If I remote desktop to the server that houses the web app and db server, I am able to upload files and store them into a varbinary(max) column.

However, when clients connect to the web server from their desktop, then can do all the things they need to such as browsing web pages, fill out forms that store/save data to the database, etc.

However, when they try to upload a pdf to the sever, the following exception occurs: The web app uses .net 3.5, the db is sql 2005, and code is c#. And insights would be welcomed. Code and Exception below. Any insights will be welcomed.

protected void btnSave_Click(object sender, EventArgs e)
{

    int intDocLen = 0;
    string str = "";

    Stream objStream = default(Stream);
    SqlConnection Conn = default(SqlConnection);
    SqlCommand cmdUploadDoc = default(SqlCommand);
    string ConnString = null;
    lblMessage.Text  = "";
    try
    {
        if (FileUpload1.HasFile)
        {
            Guid NewDOCGUID = System.Guid.NewGuid();
            intDocLen = FileUpload1.PostedFile.ContentLength;
            byte[] Docbuffer = new byte[intDocLen];

            objStream = FileUpload1.PostedFile.InputStream;
            objStream.Read(Docbuffer, 0, intDocLen);

            ConnString = ConfigurationManager.ConnectionStrings["CFDConnectionString1"].ConnectionString;
            Conn = new SqlConnection(ConnString);

            if (!isMM)
            {
                string query = "INSERT INTO DisclosureFiles "
                    + "(DisclosuerSummaryId, FileName, Contents, DocGUID, DateModified) "
                    + "VALUES(@DisclosuerSummaryId, @FileName, @Contents, @DocGUID, @DateModified) ";
                cmdUploadDoc = new SqlCommand();
                cmdUploadDoc.CommandType = CommandType.Text;
                cmdUploadDoc.Connection = Conn;
                cmdUploadDoc.CommandText = query;

                cmdUploadDoc.Parameters.Add("@DisclosuerSummaryId",
                    SqlDbType.Int).Value = disclosureId;
                cmdUploadDoc.Parameters.Add("@FileName",
                    SqlDbType.VarChar).Value = FileUpload1.PostedFile.FileName;
                cmdUploadDoc.Parameters.Add("@Contents",
                    SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
                cmdUploadDoc.Parameters.Add("@DocGUID",
                    SqlDbType.UniqueIdentifier).Value = NewDOCGUID;
                cmdUploadDoc.Parameters.Add("@DateModified",
                    SqlDbType.DateTime).Value = DateTime.Now;
            }
            else
            {
                string query = "INSERT INTO DisclosureFiles "
                    + "(massMediaId, FileName, Contents, DocGUID, DateModified) "
                    + "VALUES(@massMediaId, @FileName, @Contents, @DocGUID, @DateModified) ";
                cmdUploadDoc = new SqlCommand();
                cmdUploadDoc.CommandType = CommandType.Text;
                cmdUploadDoc.Connection = Conn;
                cmdUploadDoc.CommandText = query;

                cmdUploadDoc.Parameters.Add("@massMediaId",
                    SqlDbType.Int).Value = disclosureId;
                cmdUploadDoc.Parameters.Add("@FileName",
                    SqlDbType.VarChar).Value = FileUpload1.PostedFile.FileName;
                cmdUploadDoc.Parameters.Add("@Contents",
                    SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
                cmdUploadDoc.Parameters.Add("@DocGUID",
                    SqlDbType.UniqueIdentifier).Value = NewDOCGUID;
                cmdUploadDoc.Parameters.Add("@DateModified",
                    SqlDbType.DateTime).Value = DateTime.Now;

            }
            Conn.Open();
            int result = cmdUploadDoc.ExecuteNonQuery(); //this is where it crashes
            Conn.Close();

            if (result > 0)
            {
                lblMessage.Text = "File Saved Successfully";
            }
        }
        else
        {
            lblMessage.Text = "Please Select a File to Upload";
        }
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Please report the following error: "
            + ex.Message;
    }
    finally
    {
        Conn.Close();
    }

}

Exception

System.Data.SqlClient.SqlException: String or binary data would be truncated. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at TestForm.btnSave_Click(Object sender, EventArgs e) in c:\inetpub\wwwdev\UploadPdf.aspx.cs:line 104

A: 

That error indicates you're trying to store too much data in a database field, like trying to store a filename of length 200 in a field that's varchar(100). What are the capacites for the other table columns and what are the lengths of the content you're trying to store in them?

lincolnk
Also good advice. I think I had a varchar column that did didn't set the size for.
Alex
A: 

Try commenting a column one by one and re-execute the code, it seems you have defined a column with less size, and trying to store data which is causing this error.

can you please see the column size defined and make sure all the columns are capable to store the data for the file?

lakhlaniprashant.blogspot.com
Hey, thanks much for the suggestion. I've started down that path and it looks like it will work. Thanks.
Alex