protected void btnSaveThuoc_Click(object sender, EventArgs e)
        {
            int res;

            if (ddlThuoc.SelectedValue == "0")
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "chuacovattu", "alert('Chưa chọn thuốc');", true);
                return;
            }
            if (ddlLoaiCa.SelectedValue == "0")
            {
                string ress = csCont.CaSauAn_InsertUpdateThuoc_SS(int.Parse(hdCaSauAn.Value), int.Parse(ddlThuoc.SelectedValue), decimal.Parse(txtKhoiLuong.Text), UserId);
                if (ress == "")
                {
                    Page.ClientScript.RegisterStartupScript(typeof(string), "daluuxong", "alert('Đã thêm thành công');", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(typeof(string), "daluuxong", "alert('Một số loại cá không cho thuốc được:" + ress + "');", true);
                }
                LoadThuoc();
                return;
            }
            string  StrPhanCachKhuChuong = "";
            string  khuchuong            = "";
            string  currkhuchuong        = "";
            string  StrSoLuongChuong     = "";
            string  StrSoLuongChuongTT   = "";
            string  StrChuong            = "";
            string  StrKL = "";
            decimal testKL;
            int     SoLuongCa = 0;
            int     SoLuongTT = 0;
            int     index     = 0;

            foreach (GridViewRow r in grvDanhSach.Rows)
            {
                TextBox txtKL      = (TextBox)(r.FindControl("txtKhoiLuong"));
                Label   lblSoLuong = (Label)(r.FindControl("lblSoLuong"));
                Label   lblSLTT    = (Label)(r.FindControl("lblSLTT"));
                Label   lblChuong  = (Label)(r.FindControl("lblChuong"));
                testKL = -1;
                if (Decimal.TryParse(txtKL.Text, out testKL) && testKL > 0)
                {
                    StrSoLuongChuong   += "@" + lblSoLuong.Text + "@";
                    StrSoLuongChuongTT += "@" + lblSLTT.Text + "@";
                    StrChuong          += "@" + lblChuong.ToolTip + "@";
                    StrKL        += "@" + txtKL.Text.Replace(",", ".") + "@";
                    SoLuongCa    += int.Parse(lblSoLuong.Text);
                    SoLuongTT    += int.Parse(lblSLTT.Text);
                    currkhuchuong = lblChuong.Text.Substring(0, 2);
                    if (currkhuchuong != khuchuong)
                    {
                        StrPhanCachKhuChuong += "@" + index + "@";
                        khuchuong             = currkhuchuong;
                    }
                    index++;
                }
            }
            if (StrPhanCachKhuChuong != "")
            {
                StrPhanCachKhuChuong = StrPhanCachKhuChuong.Substring(3) + "@" + index.ToString() + "@";
            }
            res = csCont.CaSauAn_InsertUpdateThuoc(int.Parse(hdCaSauAn.Value), int.Parse(ddlThuoc.SelectedValue), decimal.Parse(txtKhoiLuong.Text), int.Parse(ddlLoaiCa.SelectedValue), SoLuongCa, SoLuongTT, StrSoLuongChuong, StrSoLuongChuongTT, StrChuong, StrKL, StrPhanCachKhuChuong, UserId, true);
            if (res == 0)
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "daluuxong", "alert('Lưu không thành công! Có thể thời điểm cho ăn hoặc khối lượng thuốc không hợp lệ');", true);
            }
            else
            {
                LoadThuoc();
            }
        }
        private void importExcelThuoc(string dir, string file)
        {
            DateTime NgayAn               = DateTime.MinValue;
            decimal  KhoiLuong            = 0;
            int      SoLuongCa            = 0;
            int      SoLuongTT            = 0;
            decimal  value                = 0;
            int      idVatTu              = 0;
            int      SoCaAn               = 0;
            int      SoCaTT               = 0;
            string   StrChuong            = "";
            string   StrSoLuongChuong     = "";
            string   StrSoLuongChuongTT   = "";
            string   StrPhanCachKhuChuong = "";
            string   khuchuong            = "";
            string   currkhuchuong        = "";
            int      index                = 0;
            string   StrKL                = "";
            decimal  kl     = 0;
            int      LoaiCa = 0;

            string[] aChuongOnly   = null;
            string[] aChuongExcept = null;
            decimal  TongDoiChieu  = 0;
            string   f             = "{0:0.";

            for (int i = 0; i < int.Parse(txtThapPhanT.Text); i++)
            {
                f += "#";
            }
            f += "}";

            IFormatProvider culture     = new System.Globalization.CultureInfo("vi-VN", true);
            string          Excelstrcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + file + @";Extended Properties=""Excel 8.0;HDR=YES;""";
            int             numSheet    = int.Parse(txtNumSheet.Text);

            for (int num = 1; num <= numSheet; num++)
            {
                string          SQLstr   = "SELECT * FROM [Sheet" + num.ToString() + "$]";
                OleDbConnection ExcelCon = new OleDbConnection(Excelstrcon);
                ExcelCon.Open();
                OleDbCommand     dbCommand   = new OleDbCommand(SQLstr, ExcelCon);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
                DataTable        dTable      = new DataTable();
                try
                {
                    dataAdapter.Fill(dTable);
                    int idx = 2;
                    foreach (DataRow r in dTable.Rows)
                    {
                        DateTime NgayChoAn       = DateTime.Parse(r["Ngay"].ToString() + "/" + r["Thang"].ToString() + "/" + r["Nam"].ToString() + " 23:00:00", culture);
                        DateTime NgayChoAnOutput = DateTime.MinValue;
                        int      IDCaSauAn       = csCont.CaSauAn_GetCaSauAnByNgay(NgayChoAn, out NgayChoAnOutput);
                        if (IDCaSauAn != 0 && NgayChoAn != NgayChoAnOutput)
                        {
                            csCont.CaSauAn_Update(IDCaSauAn, NgayChoAn, UserId);
                        }
                        if (idx == 2)
                        {
                            LoaiCa        = Convert.ToInt32(r["LoaiCa"]);
                            aChuongOnly   = null;
                            aChuongExcept = null;
                            string sChuongOnly = r["Chuong-Only"].ToString();
                            if (sChuongOnly != "")
                            {
                                sChuongOnly = sChuongOnly.Substring(1, sChuongOnly.Length - 2);
                                aChuongOnly = sChuongOnly.Split(new string[] { "!!" }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            string sChuongExcept = r["Chuong-Except"].ToString();
                            if (sChuongExcept != "")
                            {
                                sChuongExcept = sChuongExcept.Substring(1, sChuongExcept.Length - 2);
                                aChuongExcept = sChuongExcept.Split(new string[] { "!!" }, StringSplitOptions.RemoveEmptyEntries);
                            }
                        }
                        if (r["SL"] != DBNull.Value && r["SL"].ToString() != "" && Convert.ToDecimal(r["SL"]) != 0)
                        {
                            value = Convert.ToDecimal(r["SL"]);
                            string VT = r["VT"].ToString();
                            idVatTu = int.Parse(VT.Substring(VT.IndexOf('(') + 1, VT.IndexOf(')') - VT.IndexOf('(') - 1));
                            if (IDCaSauAn == 0)
                            {
                                IDCaSauAn = csCont.CaSauAn_ThemMoi(NgayChoAn, UserId, "");
                            }

                            DataTable tblChuong = csCont.CaSauAn_GetChuongByThuocByLoaiCa(IDCaSauAn, idVatTu, LoaiCa, out KhoiLuong, out SoLuongCa, out SoLuongTT, out NgayAn);
                            if (aChuongOnly != null)
                            {
                                for (int j = tblChuong.Rows.Count - 1; j > -1; j--)
                                {
                                    int k = 0;
                                    for (k = 0; k < aChuongOnly.Length; k++)
                                    {
                                        if (tblChuong.Rows[j]["Chuong"].ToString().Contains(aChuongOnly[k]))
                                        {
                                            break;
                                        }
                                    }
                                    if (k == aChuongOnly.Length)
                                    {
                                        tblChuong.Rows.RemoveAt(j);
                                    }
                                }
                            }
                            if (aChuongExcept != null)
                            {
                                for (int j = tblChuong.Rows.Count - 1; j > -1; j--)
                                {
                                    for (int k = 0; k < aChuongExcept.Length; k++)
                                    {
                                        if (tblChuong.Rows[j]["Chuong"].ToString().Contains(aChuongExcept[k]))
                                        {
                                            tblChuong.Rows.RemoveAt(j);
                                            break;
                                        }
                                    }
                                }
                            }
                            SoCaAn             = 0;
                            SoCaTT             = 0;
                            StrChuong          = "";
                            StrSoLuongChuong   = "";
                            StrSoLuongChuongTT = "";
                            StrKL = "";
                            foreach (DataRow rC in tblChuong.Rows)
                            {
                                SoCaAn += Convert.ToInt32(rC["SoLuong"]);
                                SoCaTT += Convert.ToInt32(rC["SoLuongTT"]);
                            }
                            TongDoiChieu = 0;
                            int h = 0;
                            foreach (DataRow rC in tblChuong.Rows)
                            {
                                StrChuong += "@" + rC["IDChuong"].ToString() + "@";
                                kl         = value * Convert.ToDecimal(rC["SoLuong"]) / Convert.ToDecimal(SoCaAn);
                                //StrKL += "@" + String.Format("{0:0.#####}", kl).Replace(',', '.') + "@";
                                TongDoiChieu += Convert.ToDecimal(String.Format(f, kl));
                                if (h == tblChuong.Rows.Count - 1)
                                {
                                    if (TongDoiChieu != value)
                                    {
                                        decimal temp = Convert.ToDecimal(String.Format(f, kl)) + value - TongDoiChieu;
                                        StrKL += "@" + String.Format(f, temp).Replace(',', '.') + "@";
                                    }
                                    else
                                    {
                                        StrKL += "@" + String.Format(f, kl).Replace(',', '.') + "@";
                                    }
                                }
                                else
                                {
                                    StrKL += "@" + String.Format(f, kl).Replace(',', '.') + "@";
                                }
                                h++;
                                StrSoLuongChuong   += "@" + rC["SoLuong"].ToString() + "@";
                                StrSoLuongChuongTT += "@" + rC["SoLuongTT"].ToString() + "@";
                                currkhuchuong       = rC["Chuong"].ToString().Substring(0, 2);
                                if (currkhuchuong != khuchuong)
                                {
                                    StrPhanCachKhuChuong += "@" + index + "@";
                                    khuchuong             = currkhuchuong;
                                }
                                index++;
                            }
                            StrPhanCachKhuChuong = StrPhanCachKhuChuong.Substring(3) + "@" + index.ToString() + "@";
                            int res = csCont.CaSauAn_InsertUpdateThuoc(IDCaSauAn, idVatTu, value, LoaiCa, SoCaAn, SoLuongTT, StrSoLuongChuong, StrSoLuongChuongTT, StrChuong, StrKL, StrPhanCachKhuChuong, UserId, (!chkValueAdd.Checked));
                            if (res != 1)
                            {
                                lblMessage.Text += "<br/>Sheet " + num.ToString() + ",dòng " + idx.ToString() + ", thuốc " + r["VT"].ToString() + " không import được do số lượng hoặc thời điểm biến động không hợp lệ.";
                            }
                        }
                        idx++;
                    }

                    // dispose used objects
                    dTable.Dispose();
                    dataAdapter.Dispose();
                    dbCommand.Dispose();
                    ExcelCon.Close();
                    ExcelCon.Dispose();

                    lblMessage.Text += "<br/>Đã import xong Sheet " + num.ToString() + "!";
                }
                catch (Exception ex)
                {
                    // dispose used objects
                    dTable.Dispose();
                    dataAdapter.Dispose();
                    dbCommand.Dispose();
                    ExcelCon.Close();
                    ExcelCon.Dispose();
                    Response.Write(ex.ToString());
                }
            }
        }