none
show picture in picturebox with the help of nTier application with c# RRS feed

  • Question

  • Hi,this is my firt time here to put my question. Actually, I m work on N_tier application in c# . here is my code

     private void button4_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 is a string varible
                        imgloc = dlg.FileName.ToString();
                        pictureBox1.ImageLocation = imgloc;
                    }


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

                }
       
            }
    i wrote this code to browse this picture

    private void btnsave_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);
     //person is my Business Object class object which is taking the UI textboxes and datetime picker values and Setting in (BO) Business Object Variables

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

                person.UserDate = dateTimePicker1.Value.Date;
                //person.UserDate = Convert.ToDateTime(dateTimePicker1).ToString("MM/dd/yyyy")  ;
                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);
    //This is my business Access layer and i pass to this layer (person)object of (Bo)Business Object layer

                BAL.BALPerson pBAL = new BAL.BALPerson();
                pBAL.insertdata(person);//object of buniness object layer
    ==============================
    THIS IS MY (Bo) Business Object Layer Class where i declared private variables and properties

    =======================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;

    namespace WindowsFormstemp.BO
    {
        class Person
        {
            #region variables
          private   int m_ID = 0;
          private   string m_Name = string.Empty;
          private  DateTime m_UserDate;
          private  string m_Password = string.Empty;
          private  string m_Address = string.Empty;
          private  int m_Age = 0;
          private  DateTime m_Dateofbirth;
          private  string m_Gender = string.Empty;
          private   byte[] m_Image = null;
           
            #endregion
            public int ID
            {
                get
                { return m_ID; }
                set
                { m_ID = value; }
           
            }

            public string Name
            {
                get
                {return m_Name; }
                set
                {m_Name=value;}
           
           
            }
            public DateTime UserDate
            {
                get
                { return m_UserDate; }
                set
                { m_UserDate=value; }
           
            }

            public string Password
            {
                get
                { return m_Password; }
                set
                { m_Password = value; }
            }
            public string Address
            {
                get
                { return m_Address; }
                set
                { m_Address = value; }
           
            }
            public int Age
            {
                get
                { return m_Age; }
                set
                { m_Age = value; }
           
            }
            public DateTime Dateofbirth
            {
                get
                { return m_Dateofbirth; }
                set
                {  m_Dateofbirth=value;}  
             
            }
            public string Gender
            {
                get
                { return m_Gender; }
                set
                { m_Gender = value; }
           
            }
            public byte [] Image
            {
                get
                { return m_Image; }
                set
                { m_Image = value; }
           
           
            }
    }
    }


            }
    //============================
    // this is my (BAL)Business Access Layer Class and its Code
    //=============================

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data;


    namespace WindowsFormstemp.BAL
    {
        class BALPerson
        {
            public int checkmanepassword(BO.Person person)
            {

                DAL.DALPerson pDAL = new DAL.DALPerson();
                try
                {
                    return pDAL.checkusernamepassword(person);

                }
                catch
                {
                    throw;
                }
                finally
                {
                pDAL=null;

                }
            }


            public int insertdata(BO.Person person)
            {

                DAL.DALPerson pDAL = new DAL.DALPerson();
                try
                {

                    return pDAL.insertdata(person);


                }
                catch
                {
                    throw;
                }
                finally
                {
                    pDAL = null;

                }
         
           
            }

        }
    }

    //==================================
    Now this is my last and class it is called (DAL)Data Access Layer where is wrote Function to store data into Database
    =========================================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    using System.IO;
    using System.Windows.Forms;

     

    namespace WindowsFormstemp.DAL
    {
        class DALPerson
        {
            SqlDataReader objreader;
            SqlCommand command;
            string imgloc;
               
            int count = 0;
            string connectionstring = ConfigurationManager.ConnectionStrings["Connectionbd"].ConnectionString;
            public int checkusernamepassword(BO.Person person )
            {
                SqlConnection con = new SqlConnection(connectionstring);
                con.Open();
                SqlCommand command = new SqlCommand("sp_SELECTPASSWORD", con);
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    command.Parameters.AddWithValue("@eName", person.Name);
                    command.Parameters.AddWithValue("@ePassword", person.Password);

                    objreader = command.ExecuteReader();
                    //return objreader;
                    while (objreader.Read())
                    {
                        count = 0 + 1;
                    }
                    if (count == 1)
                    {
                        MessageBox.Show(" your Username and password is correct !!!!!..... ");
                    }
                    else
                    {
                        MessageBox.Show("Please checked Your userName and Password ");
                    }

                    return count;

                   

                }

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


                public int insertdata(BO.Person person)

     

                {
                    //byte[] imagebt = null;
                    //FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
                    //BinaryReader br = new BinaryReader(fs);
                    //imagebt = br.ReadBytes((int)fs.Length);

     


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

                }

               

           
           
            }

     

     

     

        }


    //============
    I stored picture in database in binary data type but i now i wanna show this picture in picturebox  here is code  what is wrote to FillCombo
    //============================
    private void filcombo()
            {
                try
                {
                    string sqlquery = "SELECT * FROM tbltemp";
                    con.Open();
                    cmd = new SqlCommand(sqlquery, con);

                    dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            //string sid = Convert.ToInt32(dr["ID"]).ToString();
                            string sName = Convert.ToString(dr["Name"]);
                            //string sDate = Convert.ToDateTime(dr["Dates"]).ToString();
                            //string sAddress = Convert.ToString(dr["Address"]);

                            comboBox1.Items.Add(sName);
                        }
                    }
                    con.Close();
                    dr.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    //==========================
    now here is code what i wrote to show data from Database on from every thing is ok but when i wanna try to  but i have problem to show picture in picturebox any one can help me and solve my this problem plz i m stuck here from oneday and other i wanna when i lick on my datagridview my all data come on form including picture in picturebox


    with regards

    i m waiting any ans which will solve my this problem

    Wednesday, September 11, 2013 5:52 PM

Answers