Click here to Skip to main content
13,005,991 members (69,660 online)
Rate this:
Please Sign up or sign in to vote.
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace image
   public partial class Form1 : Form
        public Form1()
        private void button1_Click(object sender, EventArgs e)
            //use filestream object to read the image.

            //read to the full length of image to a byte array.

            //add this byte as an oracle parameter and insert it into database.
            //SqlDataAdapter empadap1;
            //DataSet dset;


                //proceed only when the image has a valid path

                string Image;
                if (Image != "")

                    FileStream fs;

                    fs = new FileStream(@Image, FileMode.Open, FileAccess.Read);

                    //a byte array to read the image

                    byte[] picbyte = new byte[fs.Length];

                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));


                    //open the database using and insert the data

                    //string connstr = @"Data Source=.;Initial Catalog=TestImage;
               // Persist Security Info=True;User ID=sa";

                    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Kiran\My Documents\image.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");


                    string query;

                    query = "insert into test_table(id_image,pic) values(" +
                    textBox1.Text + "," + " @pic)";

                    SqlParameter picparameter = new SqlParameter();

                    picparameter.SqlDbType = SqlDbType.Image;

                    picparameter.ParameterName = "pic";

                    picparameter.Value = picbyte;

                    SqlCommand cmd = new SqlCommand(query, conn);



                    MessageBox.Show("Image Added");







            catch (Exception ex)



this code shows the error of "unassigned local variable Image" pls provide me solution as early as possible and pls if possible then give me error free code of image storing and retriving from sql db 2005 for c# without stoared processure.pls.pls.
Posted 15-Jan-13 17:41pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

here you are not giving any values to image .
string Image;
before checking if (Image != "") this condition .try to give like
string Image=string.empty;
other wise give a value to Image.
Amol S.Gholap 16-Jan-13 0:16am
this shows the error of "String does not contain the value of empty"....
josh-jw 16-Jan-13 0:21am
string Image;
Image=//give any valid image path.then only if condition will work.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


As you are not assigning any value to image variable thats why it is showing you error.
can you tell me why you have added the following line here which is of no use.

string Image;
if (Image != "")

If you are not using the string variable in your code then whats the meaning of adding it to your code? remove that code if u really don't want to use that.


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
Web01 | 2.8.170628.1 | Last Updated 16 Jan 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