public IList<RentDetail> getRentOrderDetailByRentCode(string _code) { IList<RentDetail> rentDetails = new List<RentDetail>(); RentDetail rentDetail = null; try { Conn = db.openConnAccess(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append(" SELECT ID,RentCode,PCode,PName,UCode,UName,NumberRent,NumberReturn,Physical,Penalty FROM tbRentDetail "); sb.Append(" WHERE (RentCode ='" + _code + "')"); string sql; sql = sb.ToString(); com = new OleDbCommand(); com.CommandText = sql; com.CommandType = CommandType.Text; com.Connection = Conn; dr = com.ExecuteReader(); if (dr.HasRows) { DataTable dt = new DataTable(); dt.Load(dr); int index = 1; foreach (DataRow drw in dt.Rows) { rentDetail = new RentDetail(); rentDetail.ID = Convert.ToInt32(drw["ID"].ToString()); //0 rentDetail.Index = Convert.ToString(index);//1 rentDetail.RentCode = drw["RentCode"].ToString();//2 rentDetail.PCode = drw["PCode"].ToString();//3 Product product = serviceProduct.getByCode(drw["PCode"].ToString()); rentDetail.PName = product.PName;//4 rentDetail.UCode = drw["UCode"].ToString();//5 Unit u = serviceUnit.getByCode(drw["UCode"].ToString()); rentDetail.UName =u.UName;//6 rentDetail.NumberRent =Convert.ToInt32(drw["NumberRent"].ToString());//7 rentDetail.NumberReturn = Convert.ToInt32(drw["NumberReturn"].ToString());//8 rentDetail.Physical = drw["Physical"].ToString();//9 rentDetail.Penalty= Convert.ToInt32(drw["Penalty"].ToString());//10 rentDetails.Add(rentDetail); index++; } } dr.Close(); } catch (Exception ex) { dr.Close(); Conn.Close(); return null; throw ex; } finally { Conn.Close(); } return rentDetails; }
private void cmdSave_Click(object sender, EventArgs e) { if (txtPRCode.Text.Trim() == "") { MessageBox.Show("กรุณากรอกชื่อผู้ยืมด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information); txtPRCode.Focus(); return; } int count = lsvProductList.Items.Count; if (count == 0) { MessageBox.Show("กรุณาเลือกสินค้าด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information); txtSearchProduct.Focus(); return; } DateTime start = dtpRent.Value; DateTime end = dtpReturn.Value; if (end < start) { MessageBox.Show("วันคืนสินค้าต้องมากกว่าหรือเท่ากับวันยืมสินค้าด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information); dtpReturn.Select(); return; } if (MessageBox.Show("คุณต้องการเพิ่มการยืมใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { try { Rent rent = serviceRent.getByRentCode(txtRN.Text.Trim()); if (rent == null) { Rent newRent = new Rent(); newRent.RentCode = txtRN.Text.Trim(); newRent.PRCode = txtPRCode.Text.Trim(); newRent.PCompany = txtPCompany.Text.Trim(); newRent.PAddress = txtPAddress.Text.Trim(); newRent.PPhone = txtPPhone.Text.Trim(); newRent.PObjective = txtPObjective.Text.Trim(); newRent.DateRent = dtpRent.Value; newRent.DateReturn = dtpReturn.Value; IList<RentDetail> rentDetails = new List<RentDetail>(); RentDetail rentDetail; int j = 1; for (int i = 0; i <= lsvProductList.Items.Count - 1; i++) { rentDetail = new RentDetail(); rentDetail.Index = Convert.ToString(j); rentDetail.RentCode = txtRN.Text.Trim(); rentDetail.PCode = lsvProductList.Items[i].SubItems[0].Text.Replace(",", ""); rentDetail.PName = lsvProductList.Items[i].SubItems[1].Text.Replace(",", ""); Product product = serviceProduct.getByCode(lsvProductList.Items[i].SubItems[0].Text.Replace(",", "")); rentDetail.UCode = product.UCode; rentDetail.UName = lsvProductList.Items[i].SubItems[2].Text.Replace(",", ""); rentDetail.NumberRent = Convert.ToInt32(lsvProductList.Items[i].SubItems[3].Text.Replace(",", "")); rentDetails.Add(rentDetail); j++; } newRent.rentDetails = rentDetails; bool save = serviceRent.Save(newRent, cboWarehouse.SelectedValue.ToString()); if (save) { MessageBox.Show("เพิ่มการยืมสินค้า เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); ClearProduct(); txtRN.Text = ""; lsvProductList.Items.Clear(); txtSearchProduct.Focus(); ClearPersonRent(); txtRN.Text = serviceRent.getRentCode(); } else { MessageBox.Show("ไม่สามารถ เพิ่มการยืมสินค้าใหม่ได้!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("มีเลขที่เอกสารการยืมนี้อยู่แล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("ไม่สามารถ เพิ่มการยืมสินค้าได้ เนื่องจาก !!! : " + ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }