Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends i want to upload image in images folder and store image path in database.
by using repeater i want to show the uploaded images

i found error as
String or binary data would be truncated. The statement has been terminated

my code is
protected void  BtnSave_Click(object sender, EventArgs e)
{
    if(Fileup.HasFile)
    {
        try
        {
            string path=Server.MapPath("~/images//"+Fileup.FileName);
            Fileup.SaveAs(path);
            Label3.Text = "Upload succesfully";
            con.Open();
            string command1 = "insert into gallery (galleryimage) values ('" + path + "')";
            SqlCommand cmd1 = new SqlCommand(command1, con);
            cmd1.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            Label3.Text="error"+ex.Message.ToString();
        }
    }
}
public void bind2()
{
    con.Open();
    SqlDataAdapter da1 = new SqlDataAdapter("select * from gallery", con);
    DataTable dt1 = new DataTable();
    da1.Fill(dt1);
    Rp1.DataSource = dt1;
    Rp1.DataBind();
}
protected void Viewfile_Click(object sender, EventArgs e)
{
    bind2();
}


find the solution...
Posted
Comments
Ankit Rajput 12-May-11 3:38am    
Check the path length and Database field length
beginner in C#.net 12-May-11 3:39am    
ya thats right i found solution
Tarun.K.S 12-May-11 3:41am    
Good catch.
beginner in C#.net 12-May-11 3:39am    
i found the answer... in database i m using varchar(50) but the path is more than 50 after i use varchar(MAX) its working
Tarun.K.S 12-May-11 3:41am    
Good job.

Hi,
you get String or binary data would be truncated message when you try to insert a string (or binary) with more characters than the column can maximal accommodate. Try changing the length of the column in you SQL database to VARCHAR(MAX) or to a length that you find suitable.
Regards
 
Share this answer
 
v2
Hi,

this problem occurs when you are trying to insert data into a varchar field and data length is greater than Field Length.

So please check the path length and Database field length.

Regards
AR
 
Share this answer
 
v2
This error comes when u r inserted string length is greater than ur specified length in database.
for example..

IF u have column
TITLE with varchar(5)

If in this column ur inserting
'codeprojectforums'
then u will get ur specified error as ur
inserting more than 5 characters
in your field.
 
Share this answer
 
i found the answer... in database i m using varchar(50) but the path is more than 50 after i use varchar(MAX) its working
 
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