Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!!

I am getting this Error when trying to run my application.

String or binary data would be truncated. The Statement has been Terminated


this is the code

C#
protected void btnUpload_Click(object sender, EventArgs e)
    {
        con.Open();
        string filename = FileUpload1.PostedFile.FileName;
        string path = "Upload/" + filename;
        SqlCommand cmd = new SqlCommand("Insert into Zoom(ImageName,ImagePath) values ('"+ filename +"','"+ path +"')", con);
        cmd.ExecuteNonQuery();
        FileUpload1.PostedFile.SaveAs(Server.MapPath("Upload/") + filename.Trim());
        con.Close();

    }



it worked for the first run but after that it has been showing me error again and again.
Posted
Comments
Tim Groven 16-Dec-11 10:23am    
Have you checked to make sure you values you are inserting do not exceed the column length defined in the database?
AspDotNetDev 16-Dec-11 12:28pm    
You should be using SqlParameters rather than combining the parameters into the string used for the SqlCommand. You should familiarize yourself with Bobby Tables: http://xkcd.com/327/

Hi,

Debug your application, path is exceeding the your table field allowed characters.

hope increasing field allowed characters will resolve your issue

thanks
-Amit.
 
Share this answer
 
Comments
ujjwal uniyal 16-Dec-11 11:42am    
thanks for the answer amit. I solved it myself before your answer but u provided the right answer so 5 from me.

thnks again for the answer friend :)
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
con.Open();
string filename = FileUpload1.PostedFile.FileName;
string path = "Upload/" + filename;
SqlCommand cmd = new SqlCommand("Insert into Zoom(ImageName,ImagePath) values ('"+ filename +"','"+ path +"')", con);
cmd.ExecuteNonQuery();
FileUpload1.PostedFile.SaveAs(Server.MapPath("Upload/") + filename.Trim());
con.Close();
}
catch(Exception exp)
{
}

}


try this code, might work for you...
 
Share this answer
 
http://doitdotnet.wordpress.com/2011/12/16/uploading-a-file-using-fileuploadcontrol-to-sqlserver/[^]

check the above url.. It will solve your issue. You are trying to save the file in to a directory which doesn't exists in the server so you are getting error.
 
Share this answer
 
Comments
ujjwal uniyal 16-Dec-11 11:41am    
Directory exists my friend. 1st image is save in it. it was the second image that was causing problem but i found out the solution as provided by AmitGajjar.
doit4dotnet 16-Dec-11 11:47am    
ok fine...

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