Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am sujata.I am going develop web application in that in want save pdf ,doc/docs,xls/xlsx and image file into sql server 2008.when i am going to save Pdf file then it gives above exeption

What I have tried:

Following is my sample code..


1) Design page
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
  <asp:Button ID="btnUpload" runat="server" Text="Upload"
      onclick="btnUpload_Click" />


  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

  <asp:Label ID="Label2" runat="server" Text="Enter id"></asp:Label>

  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="btnRetrive" runat="server" Text="Retrive" Height="31px"
      onclick="btnRetrive_Click" style="margin-top: 80px" />
  <asp:Button ID="btnUpload0" runat="server" Text="PDFUpload"
      Height="21px"
      style="margin-top: 64px; margin-bottom: 33px" onclick="btnUpload0_Click" />




2).operation on btnUload_click
C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filepath = FileUpload1.PostedFile.FileName;
            string fileName = Path.GetFileName(filepath);
            string ext = Path.GetExtension(fileName);
            string contentType = string.Empty;

            switch (ext)
            {
                case ".doc":
                    contentType = "application/vnd.ms-word";
                    break;
                case ".docx":
                    contentType = "application/vnd.ms-word";
                    break;
                case ".xls":
                    contentType = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    contentType = "application/vnd.ms-excel";
                    break;
                case ".jpg":
                    contentType = "image/jpg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".pdf":
                    contentType = "application/pdf";
                    break;

            }


            if (contentType != string.Empty)
            {
                string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string contenttype = FileUpload1.PostedFile.ContentType;
                using (Stream fs = FileUpload1.PostedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        //string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString)) ;
                        {
                            string query = "insert into ImageTable values (@Name, @ContentType, @Data)";
                            using (SqlCommand cmd = new SqlCommand(query))
                            {
                                cmd.Connection = con;

                                cmd.Parameters.AddWithValue("@Name", filename);
                                cmd.Parameters.AddWithValue("@ContentType", contenttype);
                                cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value=bytes;
                                con.Open();
                                cmd.ExecuteNonQuery();
                              //  con.Close();
                            }
                        }
                    }
                    Label1.Text = "Upload Succesfully";
                    con.Close();
                }
            }
        }


when i am going to save image,doc file it works properly,but when i am going to save pdf file some pdf files are save successfully and some raise following exception


String or binary data would be truncated.
The statement has been terminated.</pre>
Please help me to solve this exception
Posted
Updated 15-Dec-16 22:44pm
v5

The field you are saving the file into is too small for the data you are saving so you'll need to change its type.

types - SQL Server: How to store binary data (e.g. Word file)? - Stack Overflow[^]
 
Share this answer
 
Comments
SujataJK 16-Dec-16 4:28am    
thanks for quick reply.
Here i have 4 columns for my ImageTable as
id int not nill,
Name varchar(25)null,
ContentType varchar(25)null,
and
Data varBinary(MAX)
You should be using the Blob data type as suggested at Varbinary Data Type[^].
 
Share this answer
 
Comments
SujataJK 16-Dec-16 4:57am    
thanks Richard.
but i can not find BLOB datatype in sql server 2008 R2
Richard MacCutchan 16-Dec-16 5:11am    
Well it seems that varbinary(max) should do the job, see Working with Binary Large Objects (BLOBs) Using SQL Server and ADO.NET - Developer.com[^]. So you need to do further investigation as to why you are getting this error.
Richard MacCutchan 16-Dec-16 5:16am    
You may also need to specify the size of the data as shown at SqlParameterCollection.Add Method (String, SqlDbType, Int32) (System.Data.SqlClient)[^].
SujataJK 16-Dec-16 5:34am    
@Richard

sir i did that like,
cmd.Parameters.Add("@Data", SqlDbType.VarBinary,8000).Value=bytes;

but still its not working
Richard MacCutchan 16-Dec-16 5:40am    
Why do you use 8000 as the file length?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900