Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I know how to upload and insert images using FileUpload ToolBox control.
But this time I have to insert selected gridView item(an image) to mysql using asp.net. So First I select an image from gridview then I insert that image to 'image' table. Problem is Mysql Workbench can't open the inserted image. I predict the inserted data is not a blob data.
And I think I'm missing some basic concepts of image insert.
My codes are given below.
Image data type is blob.
How can I fix it?
Examples will be much appriciated.
Thanks.

C#
protected void ImageInsert()
    {
        {
            MySqlConnection con = new MySqlConnection(constr);
            MySqlCommand cmd = new MySqlCommand("INSERT INTO Images(Image) VALUES ('"+CheckBox().ToString()+"')", con);
            con.Open();
            int s1 = cmd.ExecuteNonQuery();
            if (s1 > 0)
            {
                imgUp1.Text = "Image Uploaded succesfully!";
            }
            con.Close();
        }
    }
private string CheckBox()
    {
        string url=null;
        foreach (GridViewRow row in gvImages.Rows)
        {
            CheckBox chkBox = row.FindControl("chkRow") as CheckBox;
            if (chkBox !=null && chkBox.Checked)
            {
                Image img = (Image)row.Cells[1].Controls[1];
                url = img.ImageUrl;
            }
        }
        return url;
    }</pre>
Posted
Updated 2-Oct-15 20:20pm
v4
Comments
Richard Deeming 30-Sep-15 11:58am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Also, you've asked "how can I fix it", but you haven't told us what the problem is.

Use the "Improve question" to update your question with the missing details.
phil.o 30-Sep-15 14:55pm    
Have you tried a debug session?

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