Click here to Skip to main content
13,632,483 members
Rate this:
Please Sign up or sign in to vote.
When i start my code with file upload control it's all over, but i have to fight with my exception
with exception message of string or binary data would be truncated.I cannot come to know which binary data would be truncated
and also i had created a table of name INFORMATION with columns id,name,photo
query=create table information (id int,name varchar(size),photo varbinary(max))
id=some number
name=name of the uploaded file
photo=content of uploaded file(eg:->JPEG OR PPG..ETC)
and also i had taken one textbox to enter id no and one fileuploadcontrol and one button(upload)
here is my front end code
protected void btnupload_Click(object sender, EventArgs e)
    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[] bt = br.ReadBytes((int)fs.Length);
         string constr = ConfigurationManager.ConnectionStrings["naveen"].ToString();
            using (SqlConnection con=new SqlConnection(constr))
                string query = "insert into information values(@id,@name,@photo)";
                using (SqlCommand cmd=new SqlCommand(query))
                    cmd.Connection = con;
                    cmd.CommandText = query;
                    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@name", filename);
                    cmd.Parameters.AddWithValue("@photo", contenttype);
                    int i=cmd.ExecuteNonQuery();----->Here is the statement which i have got exception
                    if (i>0)
                        Label1.Text = "File uploaded succesfully";
                        Label1.Text = "failed to upload";


and how do i resolve this exception.... :)
Posted 26-Feb-14 22:42pm
Updated 26-Feb-14 23:13pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

photo=content of uploaded file(eg:->JPEG OR PPG..ETC)
Alter DataType of this Column to image not varbinary(max).
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

OK - the error is coming from SQL, and is telling you that one or more of the data elements you have tried to insert into the "information" table does not fit in the space provided.
Which one, we can't tell from here, because we don't have access to your DB or data.

So, look at your DB design, check teh field sizes you have allowed, and compare them against the length of the data you have provided. It could be that your user typed an "id" that is too long, or the file name doesn't fit, or...

BTW: why are you doing that?
1) The user specifies the ID? What if it exists in the DB already? they waste the upload and your app crashes.
2) Why are you saving the content type, but not the actual data you took such care to read into an array of bytes?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.180712.1 | Last Updated 27 Feb 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100