Hello,

Please forgive me, I am a newbie for programming in C#. I have some experience in VB. I am trying to convert my picture box that I am able to load a picture in, but I would like to convert it to binary then save it to SQL. I am using LINQ language for my application here.

I been looking for examples on the web and haven't found ones that would help me out here.

In my SQL database I have the field for the EmployeePicture as a varbinary(MAX)

Thank you.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Linq.Mapping;
using System.Drawing.Imaging;
using System.Data.Linq;
using System.IO;



namespace VenHumanResourcesEmployee
{
    public partial class frmEmployeeInformation : Form
    {
        public frmEmployeeInformation()
        {
            InitializeComponent();
        }
        EmployeeDatabaseDataContext myDC;  
        //Intializate Database 


        private void frmEmployeeInformation_Load(object sender, EventArgs e)
        {
            myDC = new EmployeeDatabaseDataContext();
            cboSearchName.ValueMember = "EmpID";

           //load Full Names in combobox for Search Names
            var qaNames = (
                from a in myDC.tblEmployees
                select new { a.EmpID, Names = a.EmpLastName + ", " + a.EmpFirstName })
                .OrderBy(a => a.Names)  //Sorting Last Name and First Name at same time
                .ToList(); 

            cboSearchName.DisplayMember = "Names";
            cboSearchName.DataSource = qaNames; // myDC.tblEmployees.ToList<tblEmployee>();
            cboCity.ValueMember = "CityID";
            cboCity.DisplayMember = "CityName";
            cboCity.DataSource = myDC.tblCities.ToList<tblCity>();
            cboSuperName.ValueMember="EMPID";
            cboSuperName.DisplayMember = "FullName";
            cboSuperName.DataSource = myDC.View_Supervisors.ToList<View_Supervisor>();

            }

        private void cboSearchName_SelectedIndexChanged(object sender, EventArgs e)
        {

            var employee = (from i in myDC.tblEmployees
                            where i.EmpID == (int)cboSearchName.SelectedValue
                            select new
                            {
                                i.EmpFirstName, 
                                i.EmpLastName,
                                i.CityID,
                                i.EmpSuperName,
                                i.EmpID
                            }
                            ).FirstOrDefault();


            txtFirstName.Text = employee.EmpFirstName;
            txtLastName.Text = employee.EmpLastName;
            txtEmpID.Text = employee.EmpID.ToString();
            cboCity.SelectedValue = employee.CityID;
            cboSuperName.SelectedValue = employee.EmpSuperName;


        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
          //Closing the Application
          Application.Exit();
        }

       private void cmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                myDC = new EmployeeDatabaseDataContext();

                var customer = (from c in myDC.tblEmployees
                                where c.EmpID == int.Parse(txtEmpID.Text)
                                select c).First(); //Retrived object

            customer.EmpID =int.Parse(txtEmpID.Text);
            customer.EmpFirstName = txtFirstName.Text;
            customer.EmpLastName = txtLastName.Text;
            customer.CityID = (int) cboCity.SelectedValue;
            customer.EmpSuperName = (int)cboSuperName.SelectedValue;

            //Save Image File to Binary
           // customer.EmpImage = convertToByte(picEmployee.Image);


            myDC.SubmitChanges();
            MessageBox.Show("User Information has been updated", "Thank you", MessageBoxButtons.OK);

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

        private void cmdAdd_Click(object sender, EventArgs e)
        {
           //MessageBox.Show("Your value: " + cboSearchName.SelectedIndex);
          //  AddMe myDC = new AddMe(); //insert
            tblEmployee myResult = new tblEmployee();

            myResult.EmpFirstName = txtFirstName.Text;
            myResult.EmpLastName = txtLastName.Text;
            myResult.CityID = (int) cboCity.SelectedValue;
                //cboCity.SelectedValue;


            myDC.tblEmployees.InsertOnSubmit(myResult);

            myDC.SubmitChanges();

            MessageBox.Show("Employee Created");



        }

        private void cmdDelete_Click(object sender, EventArgs e)


        {
            var myResult = (from r in myDC.tblEmployees
                            where r.EmpID == int.Parse(txtEmpID.Text)
                            select r).First(); //Retrieve object

            myDC.tblEmployees.DeleteOnSubmit(myResult); //Delete Object
            myDC.SubmitChanges();
            MessageBox.Show("Employee Information","Are you sure",MessageBoxButtons.OKCancel);

        }

    private void cmdAddPicture_Click(object sender, EventArgs e)
        {
            try
                {

             OpenFileDialog newdialog = new OpenFileDialog();

                //Filter for file types that are allowed in picture box
                newdialog.Filter = "JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|BMP Files (*.bmp)|*.bmp";
                newdialog.Title = "Select Employee Picture";

                // File Location for the Picture files default to server
                newdialog.InitialDirectory = @"\\servername\HRD\Employee Pictures\";
                if (newdialog.ShowDialog() == DialogResult.OK)
                    //Displaying the Image to form
                {
                    //Display the picture in the picture box
                    string picLoc = newdialog.FileName.ToString();
                    picEmployee.ImageLocation = picLoc;
                    //picEmployee.Image = Image.FromFile(newdialog.FileName);

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

        }


    }

}

Hello Ddanbe,

I will try that one. I will keep you posted. thank you!

This article has been dead for over six months. Start a new discussion instead.