예제 #1
0
        private void BtnEdit_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Vui lòng xác nhận thay đổi!!", "CẢNH BÁO!!!", MessageBoxButtons.YesNo);

            if (dialog == DialogResult.Yes)
            {
                try
                {
                    MySqlConnection connection = DBUtils.GetDBConnection();
                    connection.Open();
                    int      CurrentIndex  = dataList.CurrentCell.RowIndex;
                    string   ID            = Convert.ToString(dataList.Rows[CurrentIndex].Cells[0].Value.ToString());
                    string   name          = Convert.ToString(dataList.Rows[CurrentIndex].Cells[1].Value.ToString());
                    string   phone         = Convert.ToString(dataList.Rows[CurrentIndex].Cells[2].Value.ToString());
                    string   email         = Convert.ToString(dataList.Rows[CurrentIndex].Cells[3].Value.ToString());
                    string   ngaynop       = (Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString())).ToString("yyyy-MM-dd H:mm:ss");
                    string   ngaytra       = dataList.Rows[CurrentIndex].Cells[5].Value != null && dataList.Rows[CurrentIndex].Cells[5].Value.ToString() != "" ? "'" + (Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString())).ToString("yyyy-MM-dd H:mm:ss") + "'" : "NULL";
                    DateTime dateNow       = DateTime.Now;
                    string   hoanthanh     = dataList.Rows[CurrentIndex].Cells[5].Value == null || dataList.Rows[CurrentIndex].Cells[5].Value.ToString().Equals("") || DateTime.Compare(Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString()), dateNow) > 0 ? "0" : "1";
                    string   thoigiankhung = Convert.ToString(dataList.Rows[CurrentIndex].Cells[7].Value.ToString());
                    int      timeValue     = CheckLV(thoigiankhung);
                    string   thoigianthuc  = "NULL";
                    if (dataList.Rows[CurrentIndex].Cells[5].Value != null && dataList.Rows[CurrentIndex].Cells[5].Value.ToString() != "")
                    {
                        TimeSpan time = Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString()) - Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString());
                        thoigianthuc = time.Days.ToString();
                    }

                    string delay = "NULL";
                    if (dataList.Rows[CurrentIndex].Cells[5].Value == null || dataList.Rows[CurrentIndex].Cells[5].Value.ToString().Equals(""))
                    {
                        TimeSpan distantTimeToNow = dateNow - Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString());
                        if ((int)distantTimeToNow.Days >= timeValue)
                        {
                            delay = "'" + ((int)distantTimeToNow.Days - timeValue) + "'";
                        }
                    }
                    else
                    {
                        TimeSpan distantTimeToRep = Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString()) - Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString());
                        if ((int)distantTimeToRep.Days >= timeValue)
                        {
                            delay = "'" + ((int)distantTimeToRep.Days - timeValue) + "'";
                        }
                    }
                    string       updateStr = "Update ds2018 set `Tên người nộp`='" + name + "',`Số điện thoại`='" + phone + "',`Email`='" + email + "',`Ngày nộp`='" + ngaynop + "',`Ngày trả`=" + ngaytra + ",`Hoàn thành`='" + hoanthanh + "',`Thời gian Khung/lĩnh vực`='" + thoigiankhung + "',`Thời gian thực`=" + thoigianthuc + ",`Đúng/trễ`=" + delay + " where `ID`='" + ID + "'";
                    MySqlCommand updateCmd = new MySqlCommand(updateStr, connection)
                    {
                        CommandType = CommandType.Text
                    };
                    updateCmd.ExecuteNonQuery();
                    LoadData();
                    MessageBox.Show("Bạn đã lưu thành công!", "THÔNG BÁO", MessageBoxButtons.OK);
                    connection.Close();
                    CheckData();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Có lỗi khi sửa: " + ex.Message + " Sự cố này khiến tác vụ không thể hoàn thành, vui lòng khởi động lại ứng dụng hoặc liên hệ nhà cung cấp!");
                }
            }
        }
예제 #2
0
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection connection = DBUtils.GetDBConnection();
                connection.Open();
                int CurrentIndex = dataList.CurrentCell.RowIndex;
                int IDValue      = 0;
                for (int i = 0; i < dataList.Rows.Count - 2; i++)
                {
                    IDValue = Int32.Parse(dataList.Rows[i].Cells[0].Value.ToString()) > IDValue?Int32.Parse(dataList.Rows[i].Cells[0].Value.ToString()) : IDValue;
                }

                /*foreach (DataGridViewRow row in dataList.Rows)
                 * {
                 *  IDValue = Int32.Parse(dataList.Rows[row.Index].Cells[0].Value.ToString()) > IDValue && dataList.Rows[row.Index].Cells[0].Value !=null && !dataList.Rows[row.Index].Cells[0].Value.ToString().Equals("") ? Int32.Parse(dataList.Rows[row.Index].Cells[0].Value.ToString()) : IDValue;
                 * } */
                string   ID            = Convert.ToString(IDValue + 1);
                string   name          = Convert.ToString(dataList.Rows[CurrentIndex].Cells[1].Value.ToString());
                string   phone         = Convert.ToString(dataList.Rows[CurrentIndex].Cells[2].Value.ToString());
                string   email         = Convert.ToString(dataList.Rows[CurrentIndex].Cells[3].Value.ToString());
                string   ngaynop       = (Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString())).ToString("yyyy-MM-dd H:mm:ss");
                string   ngaytra       = dataList.Rows[CurrentIndex].Cells[5].Value != null && dataList.Rows[CurrentIndex].Cells[5].Value.ToString() != "" ? "'" + (Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString())).ToString("yyyy-MM-dd H:mm:ss") + "'" : "NULL";
                DateTime dateNow       = DateTime.Now;
                string   hoanthanh     = dataList.Rows[CurrentIndex].Cells[5].Value == null || dataList.Rows[CurrentIndex].Cells[5].Value.ToString().Equals("") || DateTime.Compare(Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString()), dateNow) > 0 ? "0" : "1";
                string   thoigiankhung = Convert.ToString(dataList.Rows[CurrentIndex].Cells[7].Value.ToString());
                int      timeValue     = CheckLV(thoigiankhung);
                string   thoigianthuc  = "NULL";
                if (dataList.Rows[CurrentIndex].Cells[5].Value != null && dataList.Rows[CurrentIndex].Cells[5].Value.ToString() != "")
                {
                    TimeSpan time = Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString()) - Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString());
                    thoigianthuc = time.Days.ToString();
                }

                string delay = "NULL";
                if (dataList.Rows[CurrentIndex].Cells[5].Value == null || dataList.Rows[CurrentIndex].Cells[5].Value.ToString().Equals(""))
                {
                    TimeSpan distantTimeToNow = dateNow - Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString());
                    if ((int)distantTimeToNow.Days >= timeValue)
                    {
                        delay = "'" + ((int)distantTimeToNow.Days - timeValue) + "'";
                    }
                }
                else
                {
                    TimeSpan distantTimeToRep = Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[5].Value.ToString()) - Convert.ToDateTime(dataList.Rows[CurrentIndex].Cells[4].Value.ToString());
                    if ((int)distantTimeToRep.Days >= timeValue)
                    {
                        delay = "'" + ((int)distantTimeToRep.Days - timeValue) + "'";
                    }
                }
                string       state     = "1";
                string       insertStr = "Insert into ds2018 Values('" + ID + "','" + name + "','" + phone + "','" + email + "','" + ngaynop + "'," + ngaytra + ",'" + hoanthanh + "','" + thoigiankhung + "'," + thoigianthuc + "," + delay + ",'" + state + "')";
                MySqlCommand insertCmd = new MySqlCommand(insertStr, connection)
                {
                    CommandType = CommandType.Text
                };
                insertCmd.ExecuteNonQuery();
                LoadData();
                MessageBox.Show("Bạn đã lưu thành công!", "THÔNG BÁO", MessageBoxButtons.OK);
                connection.Close();
                string mess = tbxNoResult.Text;
                SendSMSAsync(phone, mess);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Có sự cố xảy ra khi thêm mới: " + ex.Message + " Sự cố này khiến tác vụ không thể hoàn thành, vui lòng khởi động lại ứng dụng hoặc liên hệ nhà cung cấp!");
            }
        }