1

I am trying to store images in sql server with data type image now the problem is getting stored into two rows, i am using FileUpload control to upload image, my code is as follows

byte[] imagedata = ImageUpload.FileBytes;
con.Open();
SqlCommand insertImageCmd = new SqlCommand("insert into Images(ImageName,Image) values (@name, @image)", con);
insertImageCmd.Parameters.AddWithValue("@name", imageNameTextBox.Text);
insertImageCmd.Parameters.AddWithValue("@image", imagedata );
insertImageCmd.ExecuteNonQuery();
con.Close();

is this the correct way store images? please help!

2 Answers 2

2

If its getting stored as two rows, using the code you've posted, I think that you might somehow be double posting your page.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is what I would do:

byte[] image = File.ReadAllBytes(path)

Then you can insert it in your database.

tip:

When you use a SqlConnection, surround it with the using keyword:

using (var connection = new SqlConnection(connectionString))
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.