Click here to Skip to main content
13,006,180 members (66,726 online)
Rate this:
Please Sign up or sign in to vote.
See more:
First of all i created a db called travel_Direction and there is a table called myMapDB

so i created a column called image_Upload which holds binary (varbinary(max))

so i basically want to upload an image to that particular column

so someone help me to do the code.. i'm new to this field.

here's c# my code

protected void Button2_Click(object sender, EventArgs e)
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
            byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
               (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
            // Create SQL Connection 
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";

            SqlCommand DbPath = new SqlCommand();
            DbPath.CommandText = "INSERT INTO MyMapDb(image_Upload)" + " VALUES (@image_Upload)";
            DbPath.CommandType = CommandType.Text;
            DbPath.Connection = con;

            SqlParameter ImageUpload = new SqlParameter("@image_Upload", SqlDbType.Image, imageSize.Length);
            ImageUpload.Value = imageSize;
            ImageUpload.Value = imageSize;
                Image1.DataBind(); // image will load to this image controller


please be kind to help me..... big help
Posted 2-Jul-13 1:28am
ryanb31 2-Jul-13 7:54am
What doesn't work? There are lots of examples online and even on this site of how to save an image to binary into sql.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

you can try this out:

public void saveImageInDataBase(int imageID)
byte[] imageData = ReadImageFile(textBox1.Text); //This nethod returns image in byte array format.
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=aaaa;User ID=sa;Password=abcd;Initial Catalog=Workbook"; //provide connection string of your server.
con.Open(); //open connection to server.
string query = "insert into ImageTable values(@imageId,@imageData)"; //create a query variable.
SqlCommand cmd = new SqlCommand(query, con); //create a sqlcommand object.
cmd.Parameters.Add(new SqlParameter("@imageId", imageID)); //add first parameters.
cmd.Parameters.Add(new SqlParameter("@imageData", imageData)); //add second parameters.
int rows = cmd.ExecuteNonQuery(); //execute query.
if (rows > 0)
MessageBox.Show("Image saved.");
MessageBox.Show("Unable to save image.");

public byte[] ReadImageFile(string imageLocation)
byte[] imageData = null;
FileInfo fileInfo = new FileInfo(imageLocation);
long imageFileLength = fileInfo.Length;
FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imageData = br.ReadBytes((int)imageFileLength);
return imageData;
promod madushan 2-Jul-13 8:26am
thank you bro!!!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I would not personally recommend to use the BLOB to save image in the database. You can upload the file and save it in a folder on your server and save the path in the DB.

Then you can call the path in the image source property of the control or tag

Using BLOB to save images increases the Database size and thus the overhead.
srinvias kumar lumeris 2-Jul-13 8:20am
i will also suggest this mechanism, otherwise degrade in performance of the application
promod madushan 2-Jul-13 10:04am
thanks for your thoughts.......
Boipelo 2-Jul-13 14:25pm
...but when some idiot delete the folder, you are dead. You might have backups but new images will be gone. I will rather have another DB for images, if there will be too many images uploads.
promod madushan 2-Jul-13 22:27pm
taht's correct!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100