Click here to Skip to main content
12,404,478 members (68,939 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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()
        {
           InitializeComponent();
         }
 
        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;

 
            try
            {
 

 
                //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));
 

 
                    fs.Close();
 

 
                    //open the database using odp.net 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");
 

 
                    conn.Open();
 

 
                    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);
 

 
                    cmd.Parameters.Add(picparameter);
 

 
                    cmd.ExecuteNonQuery();
 

 
                    MessageBox.Show("Image Added");
 

 
                    cmd.Dispose();
 

 
                    conn.Close();
 

 
                    conn.Dispose();
 

 
                    //Connection();

 

                }
 

 
            }
 

 
            catch (Exception ex)
            {
 

 
                MessageBox.Show(ex.Message);
 

 
            }
 
 
        }
    }
}

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
 
good
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.
  Permalink  
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 2

HI,

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.

Thanks
  Permalink  

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.160721.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2016
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