Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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);
                    con.Open();
                    int i=cmd.ExecuteNonQuery();----->Here is the statement which i have got exception
                    con.Close();
                    if (i>0)
                    {
                        Label1.Text = "File uploaded succesfully";
                    }
                    else
                    {
                        Label1.Text = "failed to upload";
                    }
                }
            }

        }
    }
}

and how do i resolve this exception.... :)
Posted
v3

Quote:
photo=content of uploaded file(eg:->JPEG OR PPG..ETC)
Alter DataType of this Column to image not varbinary(max).
 
Share this answer
 
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?
 
Share this answer
 

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