0

I am having trouble in storing fpt templates in SQL Server.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

//imports
using DHELTASSys.AuditTrail;
using DHELTASSys.Modules;

namespace Enrollment
{
    delegate void Function();   // a simple delegate for marshalling calls from event handlers to the GUI thread

    public partial class CreateAccount : Form
    {
        HRModuleBL obj = new HRModuleBL();
        DHELTASSysAuditTrail audit = new DHELTASSysAuditTrail();

        public CreateAccount(int emp_id)
        {
            InitializeComponent();
            audit.Emp_id = emp_id;
        }

        protected void EmptyFields()
        {

        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }


        private void OnTemplate(DPFP.Template template)
        {
            this.Invoke(new Function(delegate()
            {
                Template = template;
                VerifyButton.Enabled = SaveButton.Enabled = (Template != null);
                if (Template != null)
                    MessageBox.Show("The fingerprint template is ready for verification and saving", "Fingerprint Enrollment");
                else
                    MessageBox.Show("The fingerprint template is not valid. Repeat fingerprint enrollment.", "Fingerprint Enrollment");
            }));
        }

        private DPFP.Template Template;








        private void EnrollButton_Click(object sender, EventArgs e)
        {
            EnrollmentForm Enroller = new EnrollmentForm();
            Enroller.OnTemplate += this.OnTemplate;
            Enroller.ShowDialog();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            obj.Last_name = txtLastname.Text;
            obj.First_name = txtFirstName.Text;
            obj.Middle_name = txtMiddleName.Text;
            obj.Position_name = cmbPosition.Text;
            obj.Company_name = cmbCompany.Text;
            obj.Password = txtTempPassword.Text;
            obj.Department_name = cmbDepartment.Text;

            if (obj.Last_name == string.Empty) //Validation for empty texts
            {
                MessageBox.Show("Last name can't be empty!");
            } else if (obj.First_name == string.Empty) 
            {
                MessageBox.Show("First name can't be empty!");
            }
            else if (obj.Middle_name == string.Empty)
            {
                MessageBox.Show("Middle name can't be empty!");
            }
            else if (obj.Position_name == string.Empty)
            {
                MessageBox.Show("Position name can't be empty!");
            }
            else if (obj.Department_name == string.Empty)
            {
                MessageBox.Show("Deparment can't be empty!");
            }
            else if (obj.Company_name == string.Empty)
            {
                MessageBox.Show("Company name can't be empty!");
            }
            else if (obj.Password == string.Empty)
            {
                MessageBox.Show("Password can't be empty!");
            }
            else if (txtConfirmTempPassword.Text == string.Empty)
            {
                MessageBox.Show("Please verify your input password!");
            }
            else
            {

                if (txtTempPassword.Text != txtConfirmTempPassword.Text)
                {

                    MessageBox.Show("Password does not match", "Password Mismatch",
                        MessageBoxButtons.OK);
                }
                else
                {
                    MemoryStream fingerprintData = new MemoryStream();
                    Template.Serialize(fingerprintData);
                    fingerprintData.Position = 0;
                    BinaryReader br = new BinaryReader(fingerprintData);
                    byte[] bytes = br.ReadBytes((Int32)fingerprintData.Length);

                    obj.Biometric_code = bytes;
                    obj.AddAccountSetTempPassword();
                    audit.AddAuditTrail("Created account for " + obj.First_name + " " + obj.Last_name + ".");

                    MessageBox.Show("Account Created for " + txtLastname.Text + "," + txtFirstName.Text);

                    txtConfirmTempPassword.Text = "";
                    txtFirstName.Text = "";
                    txtLastname.Text = "";
                    txtMiddleName.Text = "";
                    txtTempPassword.Text = "";
                    cmbCompany.Text = "";
                    cmbDepartment.Text = "";
                    cmbPosition.Text = "";

                }
            }
        }

        private void VerifyButton_Click(object sender, EventArgs e)
        {
            VerificationForm Verifier = new VerificationForm();
            Verifier.Verify(Template);
        }
    }
}

Wherein I have initiated the object obj for the class used to insert data to SQL Server. Using the AddAccountSetTempPassword() method, I store the data into the database.

Problem is, when I look into the database, the image field for all fingerprint data is written as this 0x53797374656D2E427974655B5D.

UPDATE #1 Here is the class that I used to insert data into the database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Imports
using System.Data;
using DHELTASSys.DataAccess;
using DHELTASSys.AuditTrail;

namespace DHELTASSys.Modules
{
    class HRModuleBL
    {
        DHELTASSysAuditTrail audit = new DHELTASSysAuditTrail();

        private int emp_id;
        public int Emp_id
        {
            get { return emp_id; }
            set { emp_id = value; }
        }

        private string password;
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string last_name;
        public string Last_name
        {
            get { return last_name; }
            set { last_name = value; }
        }

        private string first_name;
        public string First_name
        {
            get { return first_name; }
            set { first_name = value; }
        }

        private string middle_name;
        public string Middle_name
        {
            get { return middle_name; }
            set { middle_name = value; }
        }

        private string position_name;
        public string Position_name
        {
            get { return position_name; }
            set { position_name = value; }
        }

        private string company_name;
        public string Company_name
        {
            get { return company_name; }
            set { company_name = value; }
        }

        private string email;
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        private string gender;
        public string Gender
        {
            get { return gender; }
            set { gender = value; }
        }

        private string address;
        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        private string primary_Number;
        public string Primary_Number
        {
            get { return primary_Number; }
            set { primary_Number = value; }
        }

        private string alternative_Number;
        public string Alternative_Number
        {
            get { return alternative_Number; }
            set { alternative_Number = value; }
        }

        private string city;
        public string City
        {
            get { return city; }
            set { city = value; }
        }

        private DateTime birthdate;
        public DateTime Birthdate
        {
            get { return birthdate; }
            set { birthdate = value; }
        }

        private int sss_Number;
        public int Sss_Number
        {
            get { return sss_Number; }
            set { sss_Number = value; }
        }

        private int philHealth_number;
        public int Philhealth_number
        {
            get { return philHealth_number; }
            set { philHealth_number = value; }
        }

        private byte[] biometric_code;
        public byte[] Biometric_code
        {
            get { return biometric_code; }
            set { biometric_code = value; }
        }

        private string employee_status;
        public string Employee_status
        {
            get { return employee_status; }
            set { employee_status = value; }
        }

        private int company_id;
        public int Company_id
        {
            get { return company_id; }
            set { company_id = value; }
        }

        private string department_name;
        public string Department_name
        {
            get { return department_name; }
            set { department_name = value; }
        }

        //Methods

        //Create account and set temporary password for employee

        public void AddAccountSetTempPassword()
        {
            if (Position_name == "Supervisor") //If employee is supervisor
            {
                string cmd = "EXECUTE AddAccountSetTempPassword '" + Password + "',"
                + "'" + Last_name + "',"
                + "'" + First_name + "',"
                + "'" + Middle_name + "',"
                + "'" + Position_name + "',"
                + "'" + Company_name + "',"
                + "'" + Department_name + "',"
                + "'" + Biometric_code + "'";
                DHELTASSysDataAccess.Modify(cmd);

                string cmdTwo = "Execute AddSupervisor";
                DHELTASSysDataAccess.Modify(cmdTwo);
            }
            else //If employee isn't a supervisor
            {
                string cmd = "EXECUTE AddAccountSetTempPassword '" + Password + "',"
                + "'" + Last_name + "',"
                + "'" + First_name + "',"
                + "'" + Middle_name + "',"
                + "'" + Position_name + "',"
                + "'" + Company_name + "',"
                + "'" + Department_name + "',"
                + "'" + Biometric_code + "'";
                DHELTASSysDataAccess.Modify(cmd);

            }



        }


        //Employee will enter permanent password for account
        public void AddPermanentPasswordForAccount()
        {
            string cmd = "EXECUTE AddPermanentPasswordForAccount"
                + "'" + Password + "',"
                + "'" + Emp_id + "',";
            DHELTASSysDataAccess.Modify(cmd);
            //DHELTASSysAuditTrail.AddAuditTrail( "Changed password.");
        }

        //Employee adds his/her account details
        public void AddAccountDetails()
        {
            string cmd = "EXECUTE AddAccountDetails"
                + "'" + Email + "',"
                + "'" + Gender + "',"
                + "'" + Address + "',"
                + "'" + Primary_Number + "',"
                + "'" + Alternative_Number + "',"
                + "'" + City + "',"
                + "'" + Birthdate + "',"
                + "'" + Sss_Number + "',"
                + "'" + Philhealth_number + "',"
                + "'" + Emp_id + "'";
            DHELTASSysDataAccess.Modify(cmd);
            //DHELTASSysAuditTrail.AddAuditTrail("Account details updated.");
        }

        //Displays all employees' information
        public DataTable ViewEmployeeInformation()
        {
            string cmd = "EXECUTE ViewEmployeeInformation"
                + "'" + Company_id + "'";
            DataTable dtEmployees = DHELTASSysDataAccess.Select(cmd);
            return dtEmployees;
        }

        //Verifies account login in the forms application
        public DataTable AccountEnrollmentLogin()
        {
            string cmd = "EXECUTE AccountEnrollmentLogin"
                + "'" + Emp_id + "',"
                + "'" + Password + "'";
            DataTable dt = DHELTASSysDataAccess.Select(cmd);
            return dt;
        }

        //Check if HR Manager
        public DataTable CheckIfHRManager()
        {
            string cmd = "EXECUTE CheckIfHRManager"
                + "'" + Emp_id + "'";
            DataTable dt = DHELTASSysDataAccess.Select(cmd);
            return dt;
        }
    }
}

And the table looks like this:

table : employee
emp_id->int->PK
password->nvarchar(MAX)
last_name->varchar(50)
first_name->varchar(50)
middle_name->varchar(50)
position_id->int->FK
department_id->int->FK
company_id->int->FK
email_address->varchar(50)
gender->varchar(10)
address->varchar(100)
primary_contact_number->varchar(20)
alternative_contact_number->varchar(20)
city->varchar(50)
birthdate->date
sssNumber->int
philhealth_number->int
employee_status->bit
biometrics_image->image

I wish to convert the template into bytes to save it into the database. Thanks in advance.

Marcus Ang
  • 99
  • 1
  • 3
  • 10
  • `0x53797374656D2E427974655B5D` - it's how byte data looks in DB – MikkaRin Mar 12 '14 at 11:39
  • So i'm doing it right? I thought different rows have different bits showing @MikkaRin – Marcus Ang Mar 12 '14 at 11:43
  • Looks like. But better show us a AddAccountSetTempPassword() method and structure of table, where you store fingerprints – MikkaRin Mar 12 '14 at 11:44
  • Will do when I got to my laptop, just posted this awhile ago when I was packing up. – Marcus Ang Mar 12 '14 at 11:48
  • Added the class and table @MikkaRin – Marcus Ang Mar 12 '14 at 12:09
  • for me it looks absolutely normal. To really check is it works or not - write a method to return image from DB and compare pictures. This article will help you http://www.daniweb.com/software-development/csharp/code/217419/how-to-insertretrieve-image-in-sql-server-database – MikkaRin Mar 12 '14 at 12:13
  • You should really change your database access to use parameterized queries instead of concatenating strings together. Your current code is wide open to SQL injection attacks. If someone enters dept name of ', null; drop table employee; - - then you could be in trouble – BateTech Mar 13 '14 at 01:10

0 Answers0