Click here to Skip to main content
15,914,014 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have successfully followed the instructions in "SQL Database Image Storage & Easy Thumbnails" and am uploading and displaying images to and from a database. I want to add additional information to the database such as image name, image description and image category. I am having difficulty finding out how I add these to the btnSubmit_Click event. I have two text boxes called 'txtItemName', 'txtItemDescription' and a dropdown list called 'DropDownList1' which has categories. The corresponding columns on my data table are: img_name, img_description and img_category.

The code is as follows (the second 'try' is directly from Easy Thumbs and works like a charm, I have added the first 'try' in an attempt to add the additional information):
protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string name = txtItemName.Text;
                string description = txtItemDescription.Text;
                string category = DropDownList2.SelectedValue;
                string connString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
                SqlConnection conn = null;
                conn = new SqlConnection(connString);
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "INSERT INTO tbl_image (name, description, category) Values (@img_name, @img_description, @img_category)";
                    cmd.Parameters.AddWithValue("@img_name", name);
                    cmd.Parameters.AddWithValue("@img_description", description);
                    cmd.Parameters.AddWithValue("img_category", category);
                    int rowsAffected = cmd.ExecuteNonQuery();
                    if (rowsAffected == 1)
                    {
                        //Success notification
                    }
                    else
                    {
                        //Error notification
                    }
                }
            }
            catch (Exception ex)
            {
                //log error
                //display friendly error to user
            }

            try
            {
                string imgContentType = UploadFile.PostedFile.ContentType;
                //check if an image
                if (imgContentType.ToLower().StartsWith("image"))
                {
                    //get the image from upload stream
                    System.Drawing.Bitmap b = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(UploadFile.PostedFile.InputStream);
                    //get pk, to allow image to be created on this page from the
                    //SQL server stores byte[] array
                    int img_pk = 0;
                    //store the image in database, and also ccheck to see if it was successful, and if so create
                    //a thumnail here of the stored image
                    int RowsAffected = dbAccess.SaveImageToDB(ref img_pk, BmpToBytes(b));
                    if (RowsAffected > 0)
                    {
                        createImageFromDBBytes(img_pk);
                    }
                    else
                    {
                        litException.Text = ("<br><p>ERROR saving image </p>");
                    }
                }
                else
                {        
                        litException.Text = ("<br><p>The file is not an image</p>");
                }
            }
            catch (Exception ex)
            {
                litException.Text = ("<br><p>" + ex.Message + "</p>");
            }
        }</br></br></br>
I get no errors when running. However the data isn't in the database - except for the image, so clearly I have gone wrong. I am fairly new at this and would appreciate any help. I am using asp.net 2.0 and C# (and have to use asp.net 2.0 for this).

Many Thanks
Posted
Updated 6-Jul-11 19:43pm
v3

1 solution

Try adding a closing bracket:
cmd.CommandText = "INSERT INTO tbl_image (name, description, category) Values (@img_name, @img_description, @img_category";
Becomes:
cmd.CommandText = "INSERT INTO tbl_image (name, description, category) Values (@img_name, @img_description, @img_category)";
 
Share this answer
 
Comments
Victoria Rowe 7-Jul-11 1:41am    
Thanks for looking. That was actually an error in my pasting the code into my question and putting all the " back in. The bracket is in the code.
Victoria Rowe 13-Sep-11 2:54am    
I have since abandoned this and found a solution with another method.

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