Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
This is the code that i have in my form where i want to display the image but i get an error in the code "storeimage.ExecuteNonQuery();" below that says "Incorrect syntax near image content"

protected void btnadd_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null
                && FileUpload1.PostedFile.FileName != "")
            {
                byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile Image = FileUpload1.PostedFile;
                Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
                SqlConnection myConnection = new SqlConnection("Data Source=MAC-PC\\SQLEXPRESS;Initial Catalog=Populate_Pictures;Integrated Security=True");
                SqlCommand storeimage = new SqlCommand("Insert_into_Populate_pictures"
                    + "(image_content,image_type,image_size)"
                    + "values(@image_content,@image_type,@image_size)", myConnection);
                storeimage.Parameters.Add("@image_content", SqlDbType.Image, myimage.Length).Value = myimage;
                storeimage.Parameters.Add("@imagtype", SqlDbType.VarChar, 100).Value
                    = FileUpload1.PostedFile.ContentType;
                storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 9999).Value
                    = FileUpload1.PostedFile.ContentLength;
                myConnection.Open();
                storeimage.ExecuteNonQuery();
                myConnection.Close();
                GridView1.DataSource = FetchAllImagesInfo();
                GridView1.DataBind();
            }
        }

        public DataTable FetchAllImagesInfo()
    {
        string sql="Select * from Candidate_Images";
        SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=MAC-PC\\SQLEXPRESS;Initial Catalog=Populate_Pictures;Integrated Security=True");
        DataTable dt= new DataTable ();
       da.Fill(dt);
       return dt;
    }
    }
Posted
Updated 13-Jul-11 6:57am
v2

1 solution

You need spaces as shown:

+ 

" (image_content,image_type,image_size) "
 ^                                     ^
 
Share this answer
 
v2
Comments
P.C Shabangu 14-Jul-11 5:47am    
I dont quite frankly understand.
GenJerDan 14-Jul-11 9:17am    
Never mind. I was thinking the sql string was getting managled from the ( being up against the rest of the text, but that's not the prob.
Is this code copied directly from your program? If so, @imagtype and @imagesize are incorrect, spelling-wise.
P.C Shabangu 14-Jul-11 12:18pm    
yes it is copied directly from my program.
GenJerDan 14-Jul-11 14:09pm    
well, then your added parameters don't match your declared parameters. Change them to @image_type and @image_size and see what happens.

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