Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I M WORKING ON Tiers application i saved picture may be its right way or wrong way but after saving image in Sql server Database workthroug n_tiers c# windows application i can't get back Image plz some one help me here is my some code for saving but i don't have retriving code check my code where i m wrong and plz send me some one code for retrieving code too.
this code of function i wrote on my DataAccessLayer.


C#
public int insertdata(BO.Person person)

           {

               SqlConnection con=new SqlConnection(connectionstring);
               try
               {
               con.Open();
                command = new SqlCommand("sp_Insertdatavalues", con);
               command.CommandType = CommandType.StoredProcedure;

               command.Parameters.AddWithValue("@ID", person.ID);
               command.Parameters.AddWithValue("@name",person.Name);
               command.Parameters.AddWithValue("@dates", person.UserDate);
               command.Parameters.AddWithValue("@address", person.Address);
               command.Parameters.AddWithValue("@age", person.Age);
               command.Parameters.AddWithValue("@gender", person.Gender);
               command.Parameters.AddWithValue("@dateofbirth", person.Dateofbirth);
               command.Parameters.AddWithValue("@img", person.Image);

               return command.ExecuteNonQuery();

               }
           catch
               {

                   throw;
               }

               finally
               {
                   command.Dispose();
                   con.Close();
                   con.Dispose();
               }

           }

=====================================================================================
this code i wrote on btnbrowpic to take picture

C#
private void btnbrowspic_Click(object sender, EventArgs e)
       {

           try
           {
               OpenFileDialog dlg = new OpenFileDialog();
               dlg.Filter = "JPG Files(*.jpg)|*.jpg|GIF Files(*gif)|*gif|ALL Files(*.*)|*.*";
               dlg.Title = "SELECT PICTURE";
               if (dlg.ShowDialog() == DialogResult.OK)
               {
                   imgloc = dlg.FileName.ToString();
                   pictureBox1.ImageLocation = imgloc;
               }


           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);

           }

       }

=============================================================================================
and this code i wrote on btnsave_click event

private void button1_Click(object sender, EventArgs e)
{
byte[] imagebt = null;
FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs);
imagebt = br.ReadBytes((int)fs.Length);

BO.Person person = new BO.Person();
person.ID = Convert.ToInt32(textBox1.Text);
person.Name = textBox2.Text;

person.UserDate = dateTimePicker1.Value.Date;

person.Address = textBox3.Text;
person.Age = Convert.ToInt32(textBox4.Text);
person.Dateofbirth = dateTimePicker2.Value.Date;
person.Gender = Gender;
int i = BitConverter.ToInt32(imagebt, 0);


person.Image = BitConverter.GetBytes(i);

BAL.BALPerson pBAL = new BAL.BALPerson();
pBAL.insertdata(person);


}
plz some one check and give me image retriving code too i will be Gratfull
Posted

1 solution

Hi,

Try Saving image in this way, this is simpler,
do not need to use bit converter.

C#
MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, ImageFormat.Jpeg);
Byte[] imageArray = stream.ToArray();


BO.Person person = new BO.Person();
person.ID = Convert.ToInt32(textBox1.Text);
person.Name = textBox2.Text;

person.UserDate = dateTimePicker1.Value.Date;

person.Address = textBox3.Text;
person.Age = Convert.ToInt32(textBox4.Text);
person.Dateofbirth = dateTimePicker2.Value.Date;
person.Gender = Gender;
person.Image = imageArray; //Image Array.

BAL.BALPerson pBAL = new BAL.BALPerson();
pBAL.insertdata(person);



And when you get data(Image) from database
C#
Byte[] imageArray  = person.Image; // If your image is not in byte array format try converting it to Byte[]
MemoryStream stream = new MemoryStream(imageArray);

pictureBox1.Image  = Image.FromStream(Stream);


Hope this helps!
 
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