Exemple #1
0
        private void btnSaveXml_Click(object sender, EventArgs e)
        {
            if (txtRecId.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาใส่หมายเลขใบสั่งซื้อด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtRecId.Focus();
                return;
            }
            if (MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {

                //if (ConnectionAvailable("http://7sea.homeip.net") == true)
                //{

                    //tr = Conn.BeginTransaction();

                    //try
                    //{
                    string sqlSave;
                    sb.Remove(0, sb.Length);
                    sb.Append("INSERT INTO RECEIVE_HEAD (ReceiveHeadId,ShipperId,ReceiveHeadDate,ReceiveHeadDateTime)");
                    sb.Append(" VALUES (@ReceiveHeadId,@ShipperId,@ReceiveHeadDate,@ReceiveHeadDateTime)");

                    sqlSave = sb.ToString();

                    com.Parameters.Clear();
                    com.Transaction = tr;
                    com.CommandText = sqlSave;
                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(txtRecId.Text.Trim());
                    com.Parameters.Add("@ShipperId", SqlDbType.Int).Value = 1;
                    if (dptRecDate.Value.ToString("dd/MM/yyyy") == DateTime.Now.ToString("dd/MM/yyyy"))
                    {
                        //MessageBox.Show("datnow" + DateTime.Now);
                        com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                        com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");

                    }
                    else
                    {
                        com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                        com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                    }

                    com.ExecuteNonQuery();

                    int Numrow = dgvReciveXml.Rows.Count;
                    for (int i = 0; i <= Numrow - 1; i++)
                    {

                        sb.Remove(0, sb.Length);
                        sb.Append("INSERT INTO RECEIVE_DETAIL (ReceiveHeadId,SupplierId,ProductId,ReceiveDetailQuantity,ReceiveDetailPriceUnit)");
                        sb.Append(" VALUES( @ReceiveHeadId,@SupplierId,@ProductId,@ReceiveDetailQuantity,@ReceiveDetailPriceUnit)");

                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.CommandText = sqlSave;
                        com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(txtRecId.Text.ToString());
                        com.Parameters.Add("@SupplierId", SqlDbType.Int).Value = 79;
                        com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = dgvReciveXml.Rows[i].Cells[2].Value.ToString();

                        com.Parameters.Add("@ReceiveDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(dgvReciveXml.Rows[i].Cells[3].Value.ToString()).ToString("#,##0.00");
                        com.Parameters.Add("@ReceiveDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(dgvReciveXml.Rows[i].Cells[4].Value.ToString()).ToString("#,##0.0000");

                        com.ExecuteNonQuery();

                    }

                    MessageBox.Show("บันทึกการรับเรียบร้อยแล้ว  ");
                    //   ต้องการ print หรือไม่

                    DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dlg == DialogResult.OK)
                    {
                        receive.rptReceiveSelect rptReceive = new receive.rptReceiveSelect();
                        rptReceive.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                        rptReceive.setReceiveId = intRecId;
                        rptReceive.ShowDialog();

                    }

                    RecId();
                    txtRecId.Focus();

                //}
                //else
                //{
                //    MessageBox.Show("สัญญา Internet ไม่อำนวยในการเพิ่มสินค้า");

                //}

                //}
                //catch (Exception ex)
                //{

                //    MessageBox.Show("ไม่สามารถบันทึกรายการสั่งซื้อได้ เนื่องจาก " + ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                //    tr.Rollback();

                //}
            }
        }
        private void InsertToDestinationFromHuasai()
        {
            if (MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                String sqlSave = "";
                sqlSave += "INSERT INTO RECEIVE_HEAD (ReceiveHeadId,ShipperId,ReceiveHeadDate,ReceiveHeadDateTime)" +
                " VALUES (@ReceiveHeadId,@ShipperId,@ReceiveHeadDate,@ReceiveHeadDateTime)";
                String strConn = "";

                strConn = "Data Source=192.168.2.3;Initial Catalog=7SEA_HUASAI2; Persist Security Info=True;User ID=KTCNET;Password=7chuasai";

                SqlConnection Con = new SqlConnection();
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                }
                Con.ConnectionString = strConn;
                Con.Open();
                SqlCommand com = new SqlCommand();
                com.Parameters.Clear();

                String sqlDelete = "";
                sqlDelete = "select ReceiveHeadId from Receive_Head where ReceiveHeadId = " + textBox3.Text;

                com.CommandText = sqlDelete;
                Object obj = com.ExecuteScalar();

                if (obj.ToString() != null)
                {
                    sqlDelete = "DELETE FROM Receive_weight WHERE ReceiveHeadDate = '" + Class.Function.GetDate(dateTimePicker2.Value) + "' ";
                    com.CommandText = sqlDelete;
                    com.ExecuteNonQuery();
                    sqlDelete = "DELETE FROM Receive_detail WHERE ReceiveHeadDate = '" + Class.Function.GetDate(dateTimePicker2.Value) + "' ";
                    com.CommandText = sqlDelete;
                    com.ExecuteNonQuery();
                    sqlDelete = "DELETE FROM Receive_Head WHERE ReceiveHeadDate = '" + Class.Function.GetDate(dateTimePicker2.Value) + "' ";
                    com.CommandText = sqlDelete;
                    com.ExecuteNonQuery();
                }

                com.CommandText = sqlSave;
                com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(textBox3.Text.Trim());
                com.Parameters.Add("@ShipperId", SqlDbType.Int).Value = 1;
                if (dateTimePicker2.Value.ToString("dd/MM/yyyy") == DateTime.Now.ToString("dd/MM/yyyy"))
                {
                    //MessageBox.Show("datnow" + DateTime.Now);
                    com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dateTimePicker2.Value.ToString("dd/MM/yyyy");
                    com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");

                }
                else
                {
                    com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("dd/MM/yyyy HH:mm:ss");
                    com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("dd/MM/yyyy");
                }

                com.ExecuteNonQuery();

                int Numrow = dataGridView3.Rows.Count;
                for (int i = 0; i <= Numrow - 1; i++)
                {

                    sqlSave = "INSERT INTO RECEIVE_DETAIL (ReceiveHeadId,SupplierId,ProductId,ReceiveDetailQuantity,ReceiveDetailPriceUnit)" +
                    " VALUES( @ReceiveHeadId,@SupplierId,@ProductId,@ReceiveDetailQuantity,@ReceiveDetailPriceUnit)";

                    com.Parameters.Clear();
                    com.CommandText = sqlSave;
                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(textBox3.Text.ToString());
                    com.Parameters.Add("@SupplierId", SqlDbType.Int).Value = 1;
                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = dataGridView3.Rows[i].Cells[2].Value.ToString();

                    com.Parameters.Add("@ReceiveDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(dataGridView3.Rows[i].Cells[3].Value.ToString()).ToString("#,##0.00");
                    com.Parameters.Add("@ReceiveDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(dataGridView3.Rows[i].Cells[4].Value.ToString()).ToString("#,##0.0000");

                    com.ExecuteNonQuery();

                }

                MessageBox.Show("บันทึกการรับเรียบร้อยแล้ว  ");
                //   ต้องการ print หรือไม่

                DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dlg == DialogResult.OK)
                {
                    receive.rptReceiveSelect rptReceive = new receive.rptReceiveSelect();
                    rptReceive.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                    rptReceive.setReceiveId = Convert.ToInt16(textBox2.Text);
                    rptReceive.ShowDialog();

                }

                RecId(Con);
                textBox3.Focus();

            }
        }
        // save รายการเบิกลง database
        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (status != 1)
            {
                RecId();//MaxId

                if (cboDrawId.Text == "")
                {
                    cboDrawId.Focus();
                    return;
                }
                if (MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    tr = Conn.BeginTransaction();

                    try
                    {
                        string sqlSave;
                        sb = new StringBuilder();
                        sb.Remove(0, sb.Length);
                        sb.Append("INSERT INTO RECEIVE_HEAD (ReceiveHeadId,ReceiveHeadDate,ShipperId,ReceiveHeadDateTime,ReceiveTypeId,DrawId,LocationProductId,ReceiveDiscount,EmployerId,UserName,ConfirmId )");
                        sb.Append(" VALUES (@ReceiveHeadId,@ReceiveHeadDate,@ShipperId,@ReceiveHeadDateTime,@ReceiveTypeId,@DrawId,@LocationProductId,@ReceiveDiscount,@EmployerId,@UserName,@ConfirmId)");

                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.Transaction = tr;
                        com.CommandText = sqlSave;
                        com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                        com.Parameters.Add("@ShipperId", SqlDbType.Int).Value = intShipperId;
                        if (dptRecDate.Value.ToString("dd/MM/yyyy") == DateTime.Now.ToString("dd/MM/yyyy"))
                        {
                            com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                            com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                        }
                        else
                        {
                            com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                            com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                        }
                        com.Parameters.Add("@ReceiveTypeId", SqlDbType.NVarChar).Value = cboReceiveType.SelectedValue.ToString();
                        com.Parameters.Add("@DrawId", SqlDbType.NVarChar).Value = txtDrawId.Text.Trim();
                        com.Parameters.Add("@LocationProductId", SqlDbType.NVarChar).Value = cboDrawId.SelectedValue.ToString();
                        com.Parameters.Add("@ReceiveDiscount", SqlDbType.Float).Value = 0;
                        com.Parameters.Add("@EmployerId", SqlDbType.NVarChar).Value = intEmployerId;
                        com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                        com.Parameters.Add("@ConfirmId", SqlDbType.NVarChar).Value = "1";
                        com.ExecuteNonQuery();

                        string ProductId;

                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            ProductId = lsvProductList.Items[i].SubItems[3].Text;

                            sb.Remove(0, sb.Length);
                            sb.Append("INSERT INTO RECEIVE_DETAIL (ReceiveHeadId, ");
                            sb.Append("SupplierId, ");
                            sb.Append("ProductId,");
                            sb.Append("ReceiveDetailQuantityPack, ");
                            sb.Append("ReceiveDetailQuantity, ");
                            sb.Append("ReceiveDetailPriceUnit, ");
                            sb.Append("ReceiveDetailTotal, ");
                            sb.Append("AmountProduct, ");
                            sb.Append("ShippingId, ");
                            sb.Append("Invoice, ");
                            sb.Append("DateBillShipping, ");
                            sb.Append("DateBillSupplier, ");
                            sb.Append("Packing, ");
                            sb.Append("PoNumber, ");
                            sb.Append("DiscountBth, ");
                            sb.Append("DiscountPercent) ");
                            sb.Append(" VALUES( @ReceiveHeadId, ");
                            sb.Append("@SupplierId, ");
                            sb.Append("@ProductId, ");
                            sb.Append("@ReceiveDetailQuantityPack, ");
                            sb.Append("@ReceiveDetailQuantity, ");
                            sb.Append("@ReceiveDetailPriceUnit, ");
                            sb.Append("@ReceiveDetailTotal, ");
                            sb.Append("@AmountProduct, ");
                            sb.Append("@ShippingId, ");
                            sb.Append("@Invoice, ");
                            sb.Append("@DateBillShipping, ");
                            sb.Append("@DateBillSupplier, ");
                            sb.Append("@Packing, ");
                            sb.Append("@PoNumber, ");
                            sb.Append("@DiscountBth, ");
                            sb.Append("@DiscountPercent)");

                            sqlSave = sb.ToString();

                            com.Parameters.Clear();
                            com.CommandText = sqlSave;
                            com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                            com.Parameters.Add("@SupplierId", SqlDbType.Int).Value = Convert.ToInt32(lsvProductList.Items[i].SubItems[18].Text.Replace(",", ""));
                            com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[7].Text.Replace(",", "");
                            com.Parameters.Add("@ShippingId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[1].Text.Replace(",", "");
                            com.Parameters.Add("@Invoice", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[2].Text.Replace(",", "");

                            if (lsvProductList.Items[i].SubItems[15].Text.Replace(",", "") != "")
                            {
                                com.Parameters.Add("@ReceiveDetailQuantityPack", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[15].Text.Replace(",", "")); //นำหนักรวม
                            }
                            else
                            {
                                com.Parameters.Add("@ReceiveDetailQuantityPack", SqlDbType.Int).Value = 0;
                            }
                            if (lsvProductList.Items[i].SubItems[9].Text.Replace(",", "") != "")
                            {
                                com.Parameters.Add("@AmountProduct", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[9].Text.Replace(",", ""));

                            }
                            else
                            {
                                com.Parameters.Add("@AmountProduct", SqlDbType.Int).Value = 0;
                            }
                            com.Parameters.Add("@ReceiveDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[11].Text.Replace(",", "")).ToString("#,##0.00"); //จำนวนชิ้น
                            com.Parameters.Add("@ReceiveDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[16].Text.Replace(",", "")).ToString("#,##0.0000");
                            com.Parameters.Add("@ReceiveDetailTotal", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[17].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@DateBillShipping", SqlDbType.DateTime).Value = Convert.ToDateTime(lsvProductList.Items[i].SubItems[5].Text.Replace(",", "")); //("dd/MM/yyyy");
                            com.Parameters.Add("@DateBillSupplier", SqlDbType.DateTime).Value = Convert.ToDateTime(lsvProductList.Items[i].SubItems[6].Text.Replace(",", "")); //("dd/MM/yyyy");
                            com.Parameters.Add("@DiscountBth", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[19].Text.Replace(",", "");
                            com.Parameters.Add("@DiscountPercent", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[20].Text.Replace(",", "");
                            com.Parameters.Add("@Packing", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[3].Text.Replace(",", "");
                            com.Parameters.Add("@PoNumber", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[4].Text.Replace(",", "");

                            MessageBox.Show(lsvProductList.Items[i].SubItems[5].Text.Replace(",", ""));

                            com.ExecuteNonQuery();

                            string weightext = lsvProductList.Items[i].SubItems[13].Text.ToString();
                            string Packtext = lsvProductList.Items[i].SubItems[14].Text.ToString();

                            string[] weighttextarr = weightext.Split('#');
                            int len = weighttextarr.Length;
                            string[] Packtextarr = Packtext.Split('#');
                            int lenPack = Packtextarr.Length;

                            if ((weightext != "") && (Packtext != ""))
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);
                                    sb.Append("INSERT INTO RECEIVE_WEIGHT (ReceiveHeadId,ProductId,ReceiveWeight,ReceivePack)");
                                    sb.Append(" VALUES( @ReceiveHeadId,@ProductId,@ReceiveWeight,@ReceivePack)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[7].Text.Replace(",", "");
                                    com.Parameters.Add("@ReceiveWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString());
                                    com.Parameters.Add("@ReceivePack", SqlDbType.Float).Value = Convert.ToSingle(Packtextarr[j].ToString());
                                    com.ExecuteNonQuery();

                                }

                            }
                            else
                            {
                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);
                                    sb.Append("INSERT INTO RECEIVE_WEIGHT (ReceiveHeadId,ProductId,ReceiveWeight )");
                                    sb.Append(" VALUES( @ReceiveHeadId,@ProductId,@ReceiveWeight)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[7].Text.Replace(",", "");
                                    com.Parameters.Add("@ReceiveWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString());
                                    com.ExecuteNonQuery();

                                }
                            }

                        }

                        tr.Commit();

                        //   ต้องการ print หรือไม่

                        DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (dlg == DialogResult.OK)
                        {
                            receive.rptReceiveSelect rptReceive = new receive.rptReceiveSelect();
                            rptReceive.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                            rptReceive.setReceiveId = intRecId;
                            rptReceive.ShowDialog();

                        }

                        ClearProductList();
                        ClearProductDetails();

                        RecId();

                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("ไม่สามารถบันทึกรายการสั่งซื้อได้ เนื่องจาก " + ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        cmdSave.Focus();
                        tr.Rollback();

                    }
                }
            }
            else if (status == 1)
            {

                if (MessageBox.Show("คุณต้องการบันทึกการแก้ไขรายการ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {

                    string sqlSave;
                    sb = new StringBuilder();
                    sb.Remove(0, sb.Length);
                    sb.Append(" UPDATE RECEIVE_HEAD ");
                    sb.Append("SET ReceiveHeadDate=@ReceiveHeadDate,ShipperId=@ShipperId,ReceiveHeadDateTime=@ReceiveHeadDateTime,ReceiveTypeId=@ReceiveTypeId,DrawId=@DrawId,LocationProductId=@LocationProductId,ReceiveDiscount=@ReceiveDiscount,EmployerId=@EmployerId,UserName=@UserName,ConfirmId=@ConfirmId ");
                    sb.Append("WHERE ReceiveHeadId=@ReceiveHeadId");

                    sb.Append(" DELETE FROM  RECEIVE_DETAIL  ");
                    sb.Append("WHERE ReceiveHeadId=@ReceiveHeadId");

                    sb.Append(" DELETE FROM  RECEIVE_WEIGHT  ");
                    sb.Append("WHERE ReceiveHeadId = @ReceiveHeadId");

                    sqlSave = sb.ToString();

                    com.Parameters.Clear();
                    com.Transaction = tr;
                    com.CommandText = sqlSave;
                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                    com.Parameters.Add("@ShipperId", SqlDbType.Int).Value = intShipperId;
                    //MessageBox.Show(intShipperId);
                    if (dptRecDate.Value.ToString("dd/MM/yyyy") == DateTime.Now.ToString("dd/MM/yyyy"))
                    {
                        com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                        com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                    }
                    else
                    {
                        com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                        com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                    }
                    com.Parameters.Add("@ReceiveTypeId", SqlDbType.NVarChar).Value = cboReceiveType.SelectedValue.ToString();
                    com.Parameters.Add("@DrawId", SqlDbType.NVarChar).Value = txtDrawId.Text.Trim();
                    com.Parameters.Add("@LocationProductId", SqlDbType.NVarChar).Value = cboDrawId.SelectedValue.ToString();
                    com.Parameters.Add("@ReceiveDiscount", SqlDbType.Float).Value = 0; //มันเป็นค่าอะไร? ทำไมเป็น 0 ตลอดเลย? Iyayben
                    com.Parameters.Add("@EmployerId", SqlDbType.NVarChar).Value = intEmployerId;
                    com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                    com.Parameters.Add("@ConfirmId", SqlDbType.NVarChar).Value = "1";
                    com.ExecuteNonQuery();

                    for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                    {
                        sb.Remove(0, sb.Length);
                        sb.Append("INSERT INTO RECEIVE_DETAIL (ReceiveHeadId,SupplierId,ProductId,ReceiveDetailQuantityPack,ReceiveDetailQuantity,ReceiveDetailPriceUnit,ReceiveDetailTotal,AmountProduct,ShippingId,Invoice,DateBillShipping,DateBillSupplier,DiscountBth,DiscountPercent,Packing,PoNumber)");//Iyayben เพิ่ม DiscountBth,DiscountPercent
                        sb.Append(" VALUES( @ReceiveHeadId,@SupplierId,@ProductId,@ReceiveDetailQuantityPack,@ReceiveDetailQuantity,@ReceiveDetailPriceUnit,@ReceiveDetailTotal,@AmountProduct,@ShippingId,@Invoice,@DateBillShipping,@DateBillSupplier,@DiscountBth,@DiscountPercent,@Packing,@PoNumber)");//Iyayben เพิ่ม @DiscountBth,@DiscountPercent

                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.CommandText = sqlSave;
                        com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                        com.Parameters.Add("@SupplierId", SqlDbType.Int).Value = Convert.ToInt32(lsvProductList.Items[i].SubItems[18].Text.Replace(",", ""));
                        com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[7].Text.Replace(",", "");
                        com.Parameters.Add("@ShippingId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[1].Text.Replace(",", "");
                        com.Parameters.Add("@Invoice", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[2].Text.Replace(",", "");

                        if (lsvProductList.Items[i].SubItems[15].Text.Replace(",", "") != "")
                        {
                            com.Parameters.Add("@ReceiveDetailQuantityPack", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[15].Text.Replace(",", ""));
                        }
                        else
                        {
                            com.Parameters.Add("@ReceiveDetailQuantityPack", SqlDbType.Int).Value = 0;
                        }
                        if (lsvProductList.Items[i].SubItems[9].Text.Replace(",", "") != "")
                        {
                            com.Parameters.Add("@AmountProduct", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[9].Text.Replace(",", ""));
                        }
                        else
                        {
                            com.Parameters.Add("@AmountProduct", SqlDbType.Int).Value = 0;
                        }
                        com.Parameters.Add("@ReceiveDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[11].Text.Replace(",", "")).ToString("#,##0.00");
                        com.Parameters.Add("@ReceiveDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[16].Text.Replace(",", "")).ToString("#,##0.0000");
                        com.Parameters.Add("@ReceiveDetailTotal", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[17].Text.Replace(",", "")).ToString("#,##0.00");
                        com.Parameters.Add("@DateBillShipping", SqlDbType.DateTime).Value = Convert.ToDateTime(lsvProductList.Items[i].SubItems[5].Text.Replace(",", "")); //("dd/MM/yyyy");
                        com.Parameters.Add("@DateBillSupplier", SqlDbType.DateTime).Value = Convert.ToDateTime(lsvProductList.Items[i].SubItems[6].Text.Replace(",", "")); //("dd/MM/yyyy");
                        //com.Parameters.Add("@DiscountBth", SqlDbType.NVarChar).Value = "0";
                        //com.Parameters.Add("@DiscountPercent", SqlDbType.NVarChar).Value = "0";

                        //Iyayben
                        com.Parameters.Add("@DiscountBth", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[19].Text.Replace(",", ""));
                        com.Parameters.Add("@DiscountPercent", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[20].Text.Replace(",", ""));

                        com.Parameters.Add("@Packing", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[3].Text.Replace(",", "");
                        com.Parameters.Add("@PoNumber", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[4].Text.Replace(",", "");

                        com.ExecuteNonQuery();

                        string weightext = lsvProductList.Items[i].SubItems[13].Text.ToString();
                        string Packtext = lsvProductList.Items[i].SubItems[14].Text.ToString();

                        string[] weighttextarr = weightext.Split('#');
                        int len = weighttextarr.Length;
                        string[] Packtextarr = Packtext.Split('#');
                        int lenPack = Packtextarr.Length;

                        if ((weightext != "") && (Packtext != ""))
                        {

                            for (int j = 0; j < weighttextarr.Length - 1; j++)
                            {
                                sb.Remove(0, sb.Length);
                                sb.Append("INSERT INTO RECEIVE_WEIGHT (ReceiveHeadId,ProductId,ReceiveWeight,ReceivePack)");
                                sb.Append(" VALUES( @ReceiveHeadId,@ProductId,@ReceiveWeight,@ReceivePack)");
                                sqlSave = sb.ToString();
                                com.Parameters.Clear();
                                com.CommandText = sqlSave;
                                com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                                com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[7].Text.Replace(",", "");
                                com.Parameters.Add("@ReceiveWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString());
                                com.Parameters.Add("@ReceivePack", SqlDbType.Float).Value = Convert.ToSingle(Packtextarr[j].ToString());
                                com.ExecuteNonQuery();

                            }

                        }
                        else
                        {
                            for (int j = 0; j < weighttextarr.Length - 1; j++)
                            {
                                sb.Remove(0, sb.Length);
                                sb.Append("INSERT INTO RECEIVE_WEIGHT (ReceiveHeadId,ProductId,ReceiveWeight )");
                                sb.Append(" VALUES( @ReceiveHeadId,@ProductId,@ReceiveWeight)");
                                sqlSave = sb.ToString();
                                com.Parameters.Clear();
                                com.CommandText = sqlSave;
                                com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt32(strRecId);
                                com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[7].Text.Replace(",", "");
                                com.Parameters.Add("@ReceiveWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString());
                                com.ExecuteNonQuery();

                            }
                        }

                    }

                    Close();

                }
            }
        }
        private void InsertToDestinationFrom7CSK()
        {
            if (textBox2.Text == "") {
                return;
            }
            String cg = "";
            if (MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                String sqlSave = "";
                sqlSave += "INSERT INTO RECEIVE_HEAD (ReceiveHeadId,ShipperId,ReceiveHeadDate,ReceiveHeadDateTime,AbountId)"+
                " VALUES (@ReceiveHeadId,@ShipperId,@ReceiveHeadDate,@ReceiveHeadDateTime,@AbountId)";
                String strConn = "";

                if (comboBox2.SelectedIndex == 5)
                {
                    strConn = "Data Source=192.168.0.3;Initial Catalog=7CSK; Persist Security Info=True;User ID=KTCNET;Password=khu@9wbe";
                    cg = "7CSK";
                }
                //เคทีซี
                if (comboBox2.SelectedIndex == 6)
                {
                    strConn = "Data Source=192.168.0.3;Initial Catalog=KTC; Persist Security Info=True;User ID=KTCNET;Password=khu@9wbe";
                    cg = "KTC";
                }
                //สิเกา-สงขลา
                if (comboBox2.SelectedIndex == 0)
                {
                    strConn = "Data Source=192.168.1.2;Initial Catalog=7seaTrang; Persist Security Info=True;User ID=KTCNET;Password=khu@9wbe";
                    cg = "7seaTrang";
                }
                // ตะกั่วป่า
                if (comboBox2.SelectedIndex == 1)
                {
                    strConn = "Data Source=192.168.3.3;Initial Catalog=7CTP; Persist Security Info=True;User ID=KTCNET;Password=khu@9wbe";
                    cg = "7CTP";
                }
                // ท่าศาลา
                if (comboBox2.SelectedIndex == 2)
                {
                    strConn = "Data Source=192.168.4.3; Initial Catalog=7SEA_TASARA; Persist Security Info=True;User ID=KTCNET;Password=7csara";
                    cg = "7SEA_TASARA";
                }
                //หัวไทร
                if (comboBox2.SelectedIndex == 3)
                {
                    strConn = "Data Source=192.168.2.3;Initial Catalog=7SEA_HUASAI; Persist Security Info=True;User ID=KTCNET;Password=7chuasai";//ต้องเปลี่ยน data source เป็น DNS ของ หัวไทร
                    cg = "7SEA_HUASAI";
                }
                //หัวไทร
                if (comboBox2.SelectedIndex == 4)
                {
                    strConn = "Data Source=192.168.2.3;Initial Catalog=7SEA_HUASAI2; Persist Security Info=True;User ID=KTCNET;Password=7chuasai";//ต้องเปลี่ยน data source เป็น DNS ของ หัวไทร
                    cg = "7SEA_HUASAI2";
                }

                SqlConnection Con = new SqlConnection();
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                }
                Con.ConnectionString = strConn;
                if (Con.State == ConnectionState.Open) Con.Close();
                Con.Open();

                String sqlDelete = "";
                sqlDelete = "select ReceiveHeadId from Receive_Head where ReceiveHeadId = " + textBox2.Text;
                SqlCommand com = new SqlCommand();
                com.CommandText = sqlDelete;
                com.CommandType = CommandType.Text;
                com.Connection = Con;
                object Obj = com.ExecuteScalar();

                if (Obj != null)
                {
                    sqlDelete = "DELETE FROM Receive_weight WHERE ReceiveHeadId = " + textBox2.Text + " ";
                    com.CommandText = sqlDelete;
                    com.ExecuteNonQuery();
                    sqlDelete = "DELETE FROM Receive_detail WHERE ReceiveHeadId = " + textBox2.Text + " ";
                    com.CommandText = sqlDelete;
                    com.ExecuteNonQuery();
                    sqlDelete = "DELETE FROM Receive_Head WHERE ReceiveHeadId = " + textBox2.Text + " ";
                    com.CommandText = sqlDelete;
                    com.ExecuteNonQuery();
                }
                else {
                    MessageBox.Show("null");
                }
                com.Parameters.Clear();
                com.CommandText = sqlSave;
                com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(textBox2.Text.Trim());
                com.Parameters.Add("@ShipperId", SqlDbType.Int).Value = 1;
                com.Parameters.Add("@AbountId", SqlDbType.NVarChar).Value = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                if (dateTimePicker1.Value.ToString("dd/MM/yyyy") == DateTime.Now.ToString("dd/MM/yyyy"))
                {
                    //MessageBox.Show("datnow" + DateTime.Now);
                    com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("dd/MM/yyyy");
                    com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");

                }
                else
                {
                    com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("dd/MM/yyyy HH:mm:ss");
                    com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dateTimePicker1.Value.ToString("dd/MM/yyyy");
                }

                com.ExecuteNonQuery();

                int Numrow = dataGridView3.Rows.Count;
                for (int i = 0; i <= Numrow - 1; i++)
                {

                    sqlSave = "INSERT INTO RECEIVE_DETAIL (ReceiveHeadId,SupplierId,ProductId,ReceiveDetailQuantity,ReceiveDetailPriceUnit)" +
                    " VALUES( @ReceiveHeadId,@SupplierId,@ProductId,@ReceiveDetailQuantity,@ReceiveDetailPriceUnit)";

                    com.Parameters.Clear();
                    com.CommandText = sqlSave;
                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(textBox2.Text.ToString());
                    com.Parameters.Add("@SupplierId", SqlDbType.Int).Value = 79;
                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = dataGridView3.Rows[i].Cells[2].Value.ToString();

                    com.Parameters.Add("@ReceiveDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(dataGridView3.Rows[i].Cells[4].Value.ToString()).ToString("#,##0.00");
                    com.Parameters.Add("@ReceiveDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(dataGridView3.Rows[i].Cells[6].Value.ToString()).ToString("#,##0.00");

                    com.ExecuteNonQuery();

                }

                MessageBox.Show("บันทึกการรับเรียบร้อยแล้ว  ");
                //   ต้องการ print หรือไม่

                DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dlg == DialogResult.OK)
                {
                    receive.rptReceiveSelect rptReceive = new receive.rptReceiveSelect();
                    rptReceive.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                    rptReceive.setReceiveId = Convert.ToInt16(textBox2.Text);
                    rptReceive.setCat = cg;
                    rptReceive.setConn = strConn;

                    rptReceive.ShowDialog();
                }
                RecId(Con);
                textBox2.Focus();
            }
        }
        private void cmdSave_Click(object sender, EventArgs e)
        {
            receive.frmCalculate frmCal = new KTCERP.receive.frmCalculate(lblNet.Text);
            frmCal.StartPosition = FormStartPosition.CenterParent;
            frmCal.ShowDialog();
            double CashTotal = clsCash.sCashMoney;
            double Discount = clsCash.sDiscount;

            if (cboDrawId.Text == "")
            {
                cboDrawId.Focus();
                return;
            }
            if (MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                    tr = Conn.BeginTransaction();

                    try
                    {

                        string sqlSave2;
                        sb.Remove(0, sb.Length);
                        sb.Append(" UPDATE RECEIVE_HEAD ");
                        sb.Append("SET ReceiveHeadDate=@ReceiveHeadDate,ShipperId=@ShipperId,ReceiveHeadDateTime=@ReceiveHeadDateTime,ReceiveTypeId=@ReceiveTypeId,DrawId=@DrawId,LocationProductId=@LocationProductId,ReceiveDiscount=@ReceiveDiscount,EmployerId=@EmployerId,UserName=@UserName ");
                        sb.Append("WHERE ReceiveHeadId=@ReceiveHeadId");

                        sb.Append(" DELETE FROM  RECEIVE_DETAIL  ");
                        sb.Append("WHERE ReceiveHeadId=@ReceiveHeadId");

                        sb.Append(" DELETE FROM  RECEIVE_WEIGHT  ");
                        sb.Append("WHERE ReceiveHeadId = @ReceiveHeadId");

                        sqlSave2 = sb.ToString();

                        com.Parameters.Clear();
                        com.Transaction = tr;
                        com.CommandText = sqlSave2;
                        com.Parameters.Add("@EmployerId", SqlDbType.NVarChar).Value = intEmployerId;
                        com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                        com.Parameters.Add("@ReceiveHeadId", SqlDbType.NVarChar).Value = lblRecId.Text.Trim();
                        com.Parameters.Add("@ReceiveHeadDate", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy");
                        com.Parameters.Add("@ReceiveHeadDateTime", SqlDbType.DateTime).Value = dptRecDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                        com.Parameters.Add("@ShipperId", SqlDbType.Int).Value = intShipperId;
                        com.Parameters.Add("@ReceiveTypeId", SqlDbType.NVarChar).Value = cboReceiveType.SelectedValue.ToString();
                        com.Parameters.Add("@DrawId", SqlDbType.NVarChar).Value = txtDrawId.Text.Trim();
                        com.Parameters.Add("@LocationProductId", SqlDbType.NVarChar).Value = cboDrawId.SelectedValue.ToString();
                        com.Parameters.Add("@ReceiveDiscount", SqlDbType.Float).Value = Discount;

                        com.ExecuteNonQuery();

                        //**/

                        string ProductId;

                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            ProductId = lsvProductList.Items[i].SubItems[3].Text;

                            sb.Remove(0, sb.Length);
                            sb.Append("INSERT INTO RECEIVE_DETAIL (ReceiveHeadId,SupplierId,ProductId,ReceiveDetailQuantityPack,ReceiveDetailQuantity,ReceiveDetailPriceUnit,ReceiveDetailTotal,AmountProduct,ShippingId,Invoice,DateBillShipping,DateBillSupplier)");
                            sb.Append(" VALUES( @ReceiveHeadId,@SupplierId,@ProductId,@ReceiveDetailQuantityPack,@ReceiveDetailQuantity,@ReceiveDetailPriceUnit,@ReceiveDetailTotal,@AmountProduct,@ShippingId,@Invoice,@DateBillShipping,@DateBillSupplier)");

                            sqlSave2 = sb.ToString();

                            com.Parameters.Clear();
                            com.CommandText = sqlSave2;
                            com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(lblRecId.Text.Trim());
                            com.Parameters.Add("@SupplierId", SqlDbType.Int).Value = Convert.ToInt16(lsvProductList.Items[i].SubItems[14].Text.Replace(",", ""));
                            com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[3].Text.Replace(",", "");
                            com.Parameters.Add("@ShippingId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[1].Text.Replace(",", "");
                            com.Parameters.Add("@Invoice", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[2].Text.Replace(",", "");
                            if (lsvProductList.Items[i].SubItems[9].Text.Replace(",", "") != "")
                            {
                                com.Parameters.Add("@ReceiveDetailQuantityPack", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[9].Text.Replace(",", ""));
                            }
                            else
                            {
                                com.Parameters.Add("@ReceiveDetailQuantityPack", SqlDbType.NVarChar).Value = "";
                            }
                            if (lsvProductList.Items[i].SubItems[5].Text.Replace(",", "") != "")
                            {
                                com.Parameters.Add("@AmountProduct", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[5].Text.Replace(",", ""));
                            }
                            else
                            {
                                com.Parameters.Add("@AmountProduct", SqlDbType.NVarChar).Value = "0"; ;
                            }
                            com.Parameters.Add("@ReceiveDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[7].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@ReceiveDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[12].Text.Replace(",", "")).ToString("#,##0.0000");
                            com.Parameters.Add("@ReceiveDetailTotal", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[13].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@DateBillShipping", SqlDbType.DateTime).Value = Convert.ToDateTime(lsvProductList.Items[i].SubItems[15].Text.Replace(",", "")); //("dd/MM/yyyy");
                            com.Parameters.Add("@DateBillSupplier", SqlDbType.DateTime).Value = Convert.ToDateTime(lsvProductList.Items[i].SubItems[16].Text.Replace(",", "")); //("dd/MM/yyyy");
                            com.ExecuteNonQuery();

                            string weightext = lsvProductList.Items[i].SubItems[10].Text.ToString();
                            string Packtext = lsvProductList.Items[i].SubItems[11].Text.ToString();

                            string[] weighttextarr = weightext.Split('#');
                            int len = weighttextarr.Length;
                            string[] Packtextarr = Packtext.Split('#');
                            int lenPack = Packtextarr.Length;

                            if ((weightext != "") && (Packtext != ""))
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);
                                    sb.Append("INSERT INTO RECEIVE_WEIGHT (ReceiveHeadId,ProductId,ReceiveWeight,ReceivePack)");
                                    sb.Append(" VALUES( @ReceiveHeadId,@ProductId,@ReceiveWeight,@ReceivePack)");
                                    sqlSave2 = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave2;
                                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(lblRecId.Text.Trim());
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[3].Text.Replace(",", "");
                                    com.Parameters.Add("@ReceiveWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString());
                                    com.Parameters.Add("@ReceivePack", SqlDbType.Float).Value = Convert.ToSingle(Packtextarr[j].ToString());
                                    com.ExecuteNonQuery();

                                }

                            }
                            else
                            {
                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);
                                    sb.Append("INSERT INTO RECEIVE_WEIGHT (ReceiveHeadId,ProductId,ReceiveWeight )");
                                    sb.Append(" VALUES( @ReceiveHeadId,@ProductId,@ReceiveWeight)");
                                    sqlSave2 = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave2;
                                    com.Parameters.Add("@ReceiveHeadId", SqlDbType.Int).Value = Convert.ToInt16(lblRecId.Text.Trim());
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[3].Text.Replace(",", "");
                                    com.Parameters.Add("@ReceiveWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString());
                                    com.ExecuteNonQuery();

                                }
                            }

                        }

                        tr.Commit();

                        //                      ต้องการ print หรือไม่

                        ///*

                        DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (dlg == DialogResult.OK)
                        {
                            receive.rptReceiveSelect rptReceive = new receive.rptReceiveSelect();
                            rptReceive.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                            rptReceive.setReceiveId = Convert.ToInt16(lblRecId.Text.Trim());
                            rptReceive.ShowDialog();

                        }
                        else
                        {

                            MessageBox.Show("ปรับปรุงสำเร็จ");

                        }
                        Close();

                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("ไม่สามารถแกไขรายการรับได้ เนื่องจาก " + ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //cmdSave.Focus();
                        tr.Rollback();

                    }
                }
        }