protected override void OnInit(EventArgs e) { base.OnInit(e); var products = new SlmScr033Biz().GetProductList(); cmbProduct.DataSource = products; cmbProduct.DataValueField = "ValueField"; cmbProduct.DataTextField = "TextField"; cmbProduct.DataBind(); cmbProduct.Items.Insert(0, new ListItem("", "0")); }
protected void btnUpload_Click(object sender, EventArgs e) { try { if (cmbProduct.SelectedValue == "0") { lblPdError.Text = "กรุณาระบุผลิตภัณฑ์"; return; } if (fuData.HasFile) { var ext = Path.GetExtension(fuData.FileName).ToLower(); if (ext != ".xls") { lblUploadError.Text = "กรุณาระบุไฟล์ให้ถูกต้อง (.xls)"; return; } lblFilename.Text = fuData.FileName; using (OleDbConnection conn = new OleDbConnection()) { DataTable dt = new DataTable(); string filename = Path.GetTempFileName(); fuData.SaveAs(filename); fuData.ClearAllFilesFromPersistedStore(); conn.ConnectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No;IMEX=0;'", filename); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter adp = new OleDbDataAdapter(cmd); adp.Fill(dt); adp.Dispose(); cmd.Dispose(); List <ControlListData> errLst; tbResult.Visible = false; // save data SlmScr033Biz bz = new SlmScr033Biz(); int total, succ, fail; if (!bz.SaveData(cmbProduct.SelectedValue, dt, out total, out succ, out fail, out errLst, Page.User.Identity.Name)) { AppUtil.ClientAlert(this, bz.ErrorMessage); } else { if (errLst.Count == 0) { pnlError.Visible = false; AppUtil.ClientAlert(this, "นำเข้าข้อมูลเรียบร้อย"); } else { ErrorList = errLst; gvError.PageIndex = 0; BindError(); AppUtil.ClientAlert(this, "ข้อมูลไม่ถูกนำเข้า กรุณาตรวจสอบข้อผิดพลาด"); } tbResult.Visible = true; lblSucc.Text = succ.ToString("#,##0"); lblFail.Text = fail.ToString("#,##0"); lblTotal.Text = total.ToString("#,##0"); } } } else { lblUploadError.Text = "กรุณาระบุไฟล์ที่ต้องการนำเข้า"; } } catch (Exception ex) { var msg = ex.Message; if (msg.Contains("Sheet1") && msg.Contains("not a valid")) { msg = "ไม่พบข้อมูลในชีท 'Sheet1'"; } _log.Error(msg); AppUtil.ClientAlert(this, msg); } }