public int CreateInjectionCabinetLabel(InjectionPanalLabels newInjectionCabinetLabel)
        {
            int result = -1;
            try
            {
                conn = db.openConn();
                tr = conn.BeginTransaction();
                sb = new StringBuilder();
                sb.Remove(0, sb.Length);
                sb.Append("INSERT INTO injection_panal_labels(barcode,part,date,serial,shift,rev,matterial_no,lot_no,color_no,machine_no,mold_no,user_create_name,create_date,status)");
                sb.Append(" VALUES (@barcode,@part,@date,@serial,@shift,@rev,@matterial_no,@lot_no,@color_no,@machine_no,@mold_no,@user_create_name,@create_date,@status)");

                string sqlsave;
                sqlsave = sb.ToString();

                comm = new SqlCommand();
                comm.Connection = conn;
                comm.Transaction = tr;
                comm.CommandText = sqlsave;
                comm.Parameters.Clear();
                comm.Parameters.Add("@barcode", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.BarCode;
                comm.Parameters.Add("@part", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.Part;
                comm.Parameters.Add("@date", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.date;
                comm.Parameters.Add("@serial", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.Serial;
                comm.Parameters.Add("@shift", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.Shift;
                comm.Parameters.Add("@rev", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.Rev;
                comm.Parameters.Add("@matterial_no", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.MatterialNo;
                comm.Parameters.Add("@lot_no", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.LotNo;
                comm.Parameters.Add("@color_no", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.ColorNo;
                comm.Parameters.Add("@machine_no", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.MachineNo;
                comm.Parameters.Add("@mold_no", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.MoldNo;
                comm.Parameters.Add("@user_create_name", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.UserCreateName;
                comm.Parameters.Add("@create_date", SqlDbType.DateTime).Value = newInjectionCabinetLabel.CreateDate;
                comm.Parameters.Add("@status", SqlDbType.NVarChar).Value = newInjectionCabinetLabel.Status;
                comm.ExecuteNonQuery();
                tr.Commit();

                result = 1;

            }
            catch (Exception ex)
            {
                tr.Rollback();
                conn.Close();
                return result;
                throw ex;

            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        private void txtSearch_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                List<InjectionPanalLabels> injectionPanalLabels = new List<InjectionPanalLabels>();
                injectionPanalLabels =  injectionPannelService.GetByCodes(txtSearch.Text.Trim());

                if (injectionPanalLabels != null && injectionPanalLabels.Count > 0)
                {
                    dgvList.DataSource = injectionPanalLabels;
                    dgvList.ReadOnly = true;
                    FormatdgvList();

                }
                else
                {

                    dgvList.DataSource = null;
                    lblresult.Visible = true;
                    lblresult.Text = " ไม่พบข้อมูล";

                }
            }
        }
        private void dgvList_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            string _barcode = "";
            _barcode = dgvList.Rows[e.RowIndex].Cells[1].Value.ToString();

            injectionPanalLabels = injectionPannelService.GetByCode(_barcode);
        }
        public List<InjectionPanalLabels> GetByCodes(string _part, string _date)
        {
            List<InjectionPanalLabels> injectionPanalLabels = new List<InjectionPanalLabels>();
            InjectionPanalLabels injb = null;
            try
            {
                conn = db.openConn();
                sb = new StringBuilder();
                sb.Remove(0, sb.Length);
                sb.Append(" select barcode,part,date,serial,shift,rev,matterial_no,lot_no,color_no,machine_no,mold_no,user_create_name,create_date,status from  dbo.injection_cabinet_labels");
                sb.Append(" where  part  ='" + _part + "'");
                sb.Append(" and    date  ='" + _date + "'");

                string sql;
                sql = sb.ToString();
                comm = new SqlCommand();
                comm.CommandText = sql;
                comm.CommandType = CommandType.Text;
                comm.Connection = conn;
                dr = comm.ExecuteReader();
                if (dr.HasRows)
                {

                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    foreach (DataRow drw in dt.Rows)
                    {
                        injb = new InjectionPanalLabels();
                        injb.BarCode = drw["barcode"].ToString();
                        injb.Part = drw["part"].ToString();
                        injb.date = drw["date"].ToString();
                        injb.Serial = drw["serial"].ToString();
                        injb.Shift = drw["shift"].ToString();
                        injb.Rev = drw["rev"].ToString();
                        injb.MatterialNo = drw["matterial_no"].ToString();
                        injb.LotNo = drw["lot_no"].ToString();
                        injb.ColorNo = drw["color_no"].ToString();
                        injb.MachineNo = drw["machine_no"].ToString();
                        injb.MoldNo = drw["mold_no"].ToString();
                        injb.UserCreateName = drw["user_create_name"].ToString();
                        injb.CreateDate = Convert.ToDateTime(drw["create_date"].ToString());
                        injb.Status = drw["status"].ToString();
                        injectionPanalLabels.Add(injb);

                    }
                }

            }
            catch (Exception ex)
            {
                dr.Close();
                conn.Close();
                return null;
                throw ex;

            }
            finally
            {
                conn.Close();
            }

            return injectionPanalLabels;
        }
        public List<InjectionPanalLabels> GenBarCode(InjectionPanalLabels injectionCabinetLabel, int genNumber)
        {
            List<InjectionPanalLabels> injectionCabinetLabels = new List<InjectionPanalLabels>();
            InjectionPanalLabels inl;

            string[] arr = Regex.Split(injectionCabinetLabel.date, "/");
            int _year = Convert.ToInt32(arr[2])/* - 543*/;
            injectionCabinetLabel.date = arr[0] + "/" + arr[1] + "/" + _year;

            injectionCabinetLabel.Rev = "00";
            injectionCabinetLabel.MatterialNo = "700-314";
            injectionCabinetLabel.ColorNo = "B1";
            injectionCabinetLabel.MachineNo = "MC13001";

            int _lastSerial = this.GetLastSerial(injectionCabinetLabel.Part, injectionCabinetLabel.date, injectionCabinetLabel.Shift, injectionCabinetLabel.LotNo, injectionCabinetLabel.MoldNo);
            if (_lastSerial == 0)
            {
                _lastSerial = 1;
            }
            else
            {
                _lastSerial++;
            }
             int j = 0;
            genNumber = genNumber + _lastSerial;
            for (j = _lastSerial; j < genNumber; j++)
            {
                inl = new InjectionPanalLabels();
                inl.BarCode = injectionCabinetLabel.Part + "!" + injectionCabinetLabel.date+"!";
                inl.BarCode += j.ToString("00000") + "!" + injectionCabinetLabel.Shift+"!";
                inl.BarCode += injectionCabinetLabel.Rev + "!" + injectionCabinetLabel.MatterialNo+"!";
                inl.BarCode += injectionCabinetLabel.LotNo + "!" + injectionCabinetLabel.ColorNo+"!";
                inl.BarCode += injectionCabinetLabel.MachineNo+"!"+injectionCabinetLabel.MoldNo;
                inl.Part = injectionCabinetLabel.Part;
                inl.date = injectionCabinetLabel.date;
                inl.Serial = Convert.ToString(j);
                inl.Shift = injectionCabinetLabel.Shift;
                inl.Rev = injectionCabinetLabel.Rev;
                inl.MatterialNo = injectionCabinetLabel.MatterialNo;
                inl.LotNo = injectionCabinetLabel.LotNo;
                inl.ColorNo = injectionCabinetLabel.ColorNo;
                inl.MachineNo = injectionCabinetLabel.MachineNo;
                inl.MoldNo = injectionCabinetLabel.MoldNo;
                inl.UserCreateName = "admin";
                inl.CreateDate = DateTime.Now;
                inl.Status = "1";

                injectionCabinetLabels.Add(inl);

            }

            return injectionCabinetLabels;
        }
        private void btnGen_Click(object sender, EventArgs e)
        {
            if (cbopart.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาเลือก Part  ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cbopart.Focus();
                return;
            }

            if (txtLotNo.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อน Lot No  ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtLotNo.Focus();
                return;
            }

            //if (cbomold.Text.Trim() == "")
            //{
            //    MessageBox.Show("กรุณาเลือก Mold No   ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //    cbomold.Focus();
            //    return;
            //}
            if (txtNumPrint.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อน จำนวน  ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtNumPrint.Focus();
                return;
            }

            InjectionPanalLabels _il = new InjectionPanalLabels();
            _il.Part = cbopart.SelectedValue.ToString();
            string _date = String.Format("{0:dd/MM/yyyy}", dtbDate.Value);
            _il.date = _date;
            //_il.Serial = "";
            _il.Shift = cboShift.Text.Trim() ;
            //_il.Rev ="" ;
            //_il.MatterialNo = "";
            _il.LotNo = txtLotNo.Text.Trim();
            //_il.ColorNo ="" ;
            _il.MachineNo = "";
            //_il.MoldNo = cbomold.Text.Trim();

            _il.Rev = lblrevision.Text.Trim();
            _il.MatterialNo = lblmeterial.Text.Trim();
            _il.ColorNo = lbocolor.Text.Trim();
            _il.MachineNo = injectionMachine.Name;
            _il.MoldNo = lblmachine.Text.Trim();

            int _numPrint =Convert.ToInt32(txtNumPrint.Text.Trim());
            injectionPanalLabels = injectionService.GenBarCode(_il, _numPrint);
            dgvList.DataSource = injectionPanalLabels;
            FormatdgvList();
        }