Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi newbie in coding. I created a from which contain around 12 dynamic picbox and labels. im able to read data from dataset and able to show images in the picbox & text in label. Also able to create individual click event to every picbox.
Now the thing i want to accomplished when i click on a picbox i want to search that binary_data of image of picbox in my table and want to know from which row that binary_data belong. My column contain a field "title". so when row is find out from where the data belong then a textbox1 show the "Title".

Also when i try to insert photo_aray value in oldebcommand its show me error "Error 2 Argument: cannot convert from byte[] to string"

can any one help me in this matter urgent. Thanku in advance


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace library
{
    public partial class showingmore : Form
    {
        private string passvalue;
        public string passval
        {
            get { return passvalue; }
            set { passvalue = value; }
        }
        public showingmore()
        {
            InitializeComponent();

        }

        //MemoryStream ms;
        byte[] photo_aray;



        private void showingmore_Load_1(object sender, EventArgs e)
        {

            OleDbConnection con = new OleDbConnection("Provider=SQLOLEDB;User ID=sa;Password =12345678; Initial Catalog=library; server=raj; TRUSTED_CONNECTION=true;");
            OleDbDataAdapter Adap = new OleDbDataAdapter("select * from movie ", con);
            //textBox1.Text = passvalue;
            DataSet ds = new DataSet();
            Adap.Fill(ds);

            //textBox1.Text = ds.Tables[0].Rows[15][0].ToString(); // the working way to use ds to fill data

            //textBox1.Text = ds.Tables[0].Rows[1];
            int icount = ds.Tables[0].Rows.Count;
            textBox1.Text = icount.ToString();


            /*******************************************************************************************************************/

            /**** Creating Label's and Picture Box ****/
            int n = 12; // total time for running loop.
            int j = 14; // y-axis co-ordinate label 
            int k = 479; // x-axis co-ordinate label
            int l = 18; // y-axis co-ordinate label

            // Creating label through loop's.
            for (int i = 0; i < n; i++)
            {
                //Create label
                Label labels = new Label();
                labels.Text = ds.Tables[0].Rows[i][0].ToString();
                labels.Tag = i;

                if (i <= 5)
                {
                    //Position label on screen
                    labels.Location = new Point(j, k);
                    j = j + 228;
                    // Label text color
                    labels.ForeColor = Color.Gainsboro;
                }
                else
                {
                    k = 782; // x-axis co-ordinate
                    labels.Location = new Point(l, k);
                    l = l + 228;
                    labels.ForeColor = Color.Gainsboro;
                }
                this.Controls.Add(labels);
            }

            n = 12; // total time for running loop.
            j = 18; // y-axis co-ordinate 
            k = 246; // x-axis co-ordinate
            l = 18; // y-axis co-ordinate

            // Creating PictureBox through loop's.
            for (int i = 0; i < n; i++)
            {
                //Create PictureBox
                PictureBox picbox = new PictureBox();
                picbox.Click += picbox_Click; //Here an event on picbox created which called the fuction created below.
                picbox.Tag = i;
                picbox.Image = null;
                if (ds.Tables[0].Rows[i][14] != System.DBNull.Value)
                {
                    photo_aray = (byte[])ds.Tables[0].Rows[i][14];
                    MemoryStream ms = new MemoryStream(photo_aray);
                    picbox.Image = Image.FromStream(ms);


                }

                if (i <= 5)
                {
                    //Position PictureBox on screen
                    picbox.Location = new Point(j, k);
                    picbox.Size = new Size(161, 220);
                    picbox.BackColor = Color.Gainsboro;
                    j = j + 228;
                }
                else
                {
                    k = 543; // y-axis co-ordinate
                    picbox.Location = new Point(l, k);
                    picbox.Size = new Size(161, 220);
                    picbox.BackColor = Color.Gainsboro;
                    l = l + 228;
                }

                this.Controls.Add(picbox);





            }


        }



        private void picbox_Click(object sender, EventArgs e)
        {
            //do your stuff here which handles generically all of your picture boxes clicks.
            byte[] photo_aray2;

            OleDbConnection con = new OleDbConnection("Provider=SQLOLEDB;User ID=sa;Password =12345678; Initial Catalog=library; server=raj; TRUSTED_CONNECTION=true;");
            OleDbDataAdapter Adap = new OleDbDataAdapter("select * from movie ", con);

            textBox1.Text = passvalue;
            DataSet ds = new DataSet();
            Adap.Fill(ds);

            PictureBox picbox = sender as PictureBox;
            if (picbox != null)
            {
                // now you know the button that was clicked
                switch ((int)picbox.Tag)
                {
                    case 0:
                        textBox1.Text = "1";
                        picbox.Tag = 0;
                        textBox3.Text = ds.Tables[0].Rows[0]["picturebox"].ToString();




                        if (picbox.Image != null)
                        {
                            MemoryStream mst = new MemoryStream();
                            picbox.Image.Save(mst, ImageFormat.Jpeg);
                            byte[] photo_aray = new byte[mst.Length];
                            mst.Position = 0;
                            mst.Read(photo_aray, 0, photo_aray.Length);
                            textBox4.Text = photo_aray.ToString();

                        }


                        break;
                    case 1:
                        textBox1.Text = "2";
                        picbox.Tag = 1;
                        textBox3.Text = ds.Tables[0].Rows[1]["size"].ToString();
                        //textBox4.Text = ds.Tables[0].Rows[1]["picturebox"].ToString();





                        if (picbox.Image != null)
                        {
                            MemoryStream mst = new MemoryStream();
                            picbox.Image.Save(mst, ImageFormat.Jpeg);
                            byte[] photo_aray = new byte[mst.Length];
                            mst.Position = 0;
                            mst.Read(photo_aray, 0, photo_aray.Length);


                            photo_aray2 = Encoding.ASCII.GetBytes("select * from movie where picturebox='" + photo_aray + "'");
                            

                            OleDbCommand cmd = new OleDbCommand(photo_aray2, con);

                            //textBox3.Text = photo_aray.ToString();
                            //textBox4.Text = photo_aray2.ToString();
















                        }


                        break;
                    case 2:
                        textBox1.Text = "3";
                        picbox.Tag = 2;
                        textBox3.Text = ds.Tables[0].Rows[2]["size"].ToString();
                        textBox4.Text = ds.Tables[0].Rows[2]["picturebox"].ToString();

                        break;
                }
            }





        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}
Posted
Comments
PIEBALDconsult 9-Apr-15 0:51am    
Better to create a hash of the data and then compare only the hash values.
metalmainiace 9-Apr-15 2:01am    
can u please describe newbie in coding.
Sergey Alexandrovich Kryukov 9-Apr-15 1:08am    
But why comparing this data? What are you hoping to achieve?
—SA
metalmainiace 9-Apr-15 2:00am    
i have a from which contain only pic_box which were created dynamically. Every picbox caontain a unique image. The logic im thinking when i click on a pic_box it open a new form that form have textbox's in which i want to show value like title, director, actor, imagebox etc. So how can i search by click on the pic_box and the data pic_box can hold is image only. So im thinking when i click on a pic_box in form 1 it store that image value in a photo_aray1 byte[] variable. then i run sql search query to find in table which column "picturebox" have the same binary data of the image. From where im able to get what is the table row no. is im follow the right logic or not. if not can u suggest anything.
Sergey Alexandrovich Kryukov 9-Apr-15 2:03am    
Let's try again. What are you hoping to achieve with all that?
—SA

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