Click here to Skip to main content
15,917,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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.
 
Share this answer
 
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.
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.

C#
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
 
Share this answer
 

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