Esempio n. 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            btnImport.Enabled = false;
            SplashScreenManager.ShowDefaultWaitForm("Vui Lòng Chờ...", "Đang thực hiện thao tác...");
            string filename = txtLinkFile.Text;

            if (filename == "")
            {
                XtraMessageBox.Show("Vui Lòng Chọn File để IMPORT", "Thông Báo");
                return;
            }
            try
            {
                DataTable       dt = new DataTable();
                string          FRConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", filename);
                OleDbConnection connection         = new OleDbConnection();
                connection.ConnectionString = FRConnectionString;
                //OleDbCommand cmd = new OleDbCommand("Select '" + DateTime.Today.ToShortDateString() + "',* from [Sheet1$]", connection);
                OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", connection);
                connection.Open();
                // OleDbDataReader dr = cmd.ExecuteReader();
                OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                adapter.Fill(dt);

                if (dt != null && dt.Rows.Count > 0)
                {
                    System.Data.DataColumn newColumn = new System.Data.DataColumn("CreateBy", typeof(System.String));
                    newColumn.DefaultValue = Form_Main.user.Username;
                    dt.Columns.Add(newColumn);

                    string ERROR = "";
                    ERROR = SQLHelper.sp_extension_ImportProduct(dt, Form_Main.user.Username);
                    if (ERROR != "")
                    {
                        API.API_ERRORLOG(new ERRORLOG(Form_Main.IPAddress, "Form_Product", "Imort Dữ Liệu()", ERROR));
                    }
                    else
                    {
                        XtraMessageBox.Show("Dữ liệu đã được import!", "Thông Báo");
                        LoadInfoProduct();
                        btnImport.Enabled = false;
                    }
                    //dynamic jsondata = JObject.Parse(jsonReturn);
                    //var jsondataChild = jsondata.GetValue("Data");
                    //var Result = jsondataChild.First.GetValue("Result").Value;
                    //var Message = jsondataChild.First.GetValue("Message").Value;
                }

                //List<Product> lstnew = new List<Product>();

                //           for (int i = 0; i < dt.Rows.Count; i++)
                //           {
                //               Product ob = new Product();
                //ob.ProductCode = dt.Rows[i]["ProductCode"].ToString().Replace("\"", "''");
                //if (string.IsNullOrEmpty(ob.ProductCode))
                //{
                //	continue;
                //}
                //ob.ProductName = dt.Rows[i]["ProductName"].ToString().Replace("\"", "''");
                //ob.Description = dt.Rows[i]["Description"].ToString().Replace("\"", "''");
                //ob.CostPrice = decimal.Parse(dt.Rows[i]["CostPrice"].ToString());
                //               ob.Price = decimal.Parse(dt.Rows[i]["Price"].ToString());
                //               ob.UserID = Form_Main.user.Username;
                //               lstnew.Add(ob);
                //           }
                //           var json = JsonConvert.SerializeObject(lstnew);
            }
            catch (Exception ex)
            {
                API.API_ERRORLOG(new ERRORLOG(Form_Main.IPAddress, "Form_Product", "Imort Dữ Liệu()", ex.ToString()));

                btnImport.Enabled = true;
                if (XtraMessageBox.Show(
                        "Máy bạn chưa hỗ trợ xử lý excel, Bấm 'YES' để tải file setup hỗ trợ", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information
                        ) == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process.Start("chrome.exe", Form_Main.URL_API + "/Uploads/Tmp/template/AccessDatabaseEngine.exe");
                    }
                    catch
                    {
                        System.Diagnostics.Process.Start(Form_Main.URL_API + "/Uploads/Tmp/template/AccessDatabaseEngine.exe");
                        throw;
                    }
                }
            }
            SplashScreenManager.CloseDefaultWaitForm();
        }