private void LoadGrid() { try { String path = @"Data.csv"; using (StreamReader reader = new StreamReader(path)) { String line = ""; if (File.Exists(path)) { dataGridTable.Rows.Clear(); while (!reader.EndOfStream) { line = reader.ReadLine(); String[] rowData = line.Split(','); int rowNum = dataGridTable.Rows.Add(); DataGridViewRow row = dataGridTable.Rows[rowNum]; String inn = rowData[6]; DateTime t = DateTime.Parse(rowData[6]); row.Cells["ColnCardNum"].Value = rowData[0]; row.Cells["ColnFullName"].Value = rowData[1]; row.Cells["ColnPhNum"].Value = rowData[2]; row.Cells["ColnEmail"].Value = rowData[3]; row.Cells["ColnOccupation"].Value = rowData[4]; row.Cells["ColnGender"].Value = rowData[5]; row.Cells["ColnInTime"].Value = DateTime.Parse(rowData[6]).ToString("hh:mm tt"); DateTime outTime = DateTime.Parse(rowData[7]); if (!outTime.Equals(default(DateTime))) { row.Cells["ColnOutTime"].Value = outTime.ToString("hh:mm tt"); } row.Cells["ColnDay"].Value = rowData[8]; Visitors visitors = new Visitors(int.Parse(rowData[0]), rowData[1], rowData[2], rowData[3], rowData[4], rowData[5], DateTime.Parse(rowData[6]), DateTime.Parse(rowData[7]), DateTime.Parse(rowData[6]).DayOfWeek); LsVisitors.Add(visitors); } } } } catch (Exception err) { MessageBox.Show("Error while loading data from the csv file.", "Error!"); } }
private void BtnCheckIn_Click(object sender, EventArgs e) { try { int cardNo = int.Parse(txtCardNo.Text); int cNo = 0; String name = ""; String phNo = ""; String email = ""; String occupation = ""; String gender = ""; DateTime inTime = DateTime.Now; DateTime outTime = default(DateTime); DayOfWeek day = inTime.DayOfWeek; foreach (Visitors v in LsVisitors) { if (v.CardNo == cardNo && !v.OutTime.Equals(default(DateTime))) { cNo = v.CardNo; name = v.Name; phNo = v.PhNo; email = v.Email; occupation = v.Occupation; gender = v.Gender; txtCardNo.Text = ""; } else if (v.CardNo == cardNo && v.OutTime.Equals(default(DateTime))) { MessageBox.Show("This visitor has not exited previously.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); txtCardNo.Text = ""; return; } } Visitors visit = new Visitors(cNo, name, phNo, email, occupation, gender, inTime, outTime, day); LsVisitors.Add(visit); String data = cNo + "," + name + "," + phNo + "," + email + "," + occupation + "," + gender + "," + inTime + "," + outTime + "," + day; ToCSV(data); MessageBox.Show("The visitor has checked in.", "Info!", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadGrid(); } catch (Exception a) { MessageBox.Show("Enter correct value!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); txtCardNo.Text = ""; } }
private void BtnSave_Click(object sender, EventArgs e) { try { Regex rx = new Regex(@"^98*([0-9]{8})$"); Regex rgx = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); Regex name = new Regex(@"^\w+\s\w+\s?\w+$"); int cardNo = 0; String path = @"Data.csv"; if (!File.Exists(path)) { File.Create(path); } using (StreamReader reader = new StreamReader(path)) { String line = ""; if (File.Exists(@"Data.csv")) { int[] cN = new int[dataGridTable.RowCount]; int counter = 0; while (!reader.EndOfStream) { line = reader.ReadLine(); String[] rowData = line.Split(','); cardNo = int.Parse(rowData[0]); cN[counter] = int.Parse(rowData[0]); counter++; } int greatest = 0; for (int i = 0; i < cN.Length; i++) { if (cN[i] > greatest) { greatest = cN[i]; } } if (cardNo >= greatest) { cardNo = ++cardNo; } else { cardNo = greatest + 1; } } } String visitorName; if (String.IsNullOrEmpty(txtName.Text.Trim()) || !name.IsMatch(txtName.Text.Trim())) { MessageBox.Show("The name field is empty or incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { visitorName = txtName.Text.Trim(); } String email; if (String.IsNullOrEmpty(txtEmail.Text.Trim()) || !rgx.IsMatch(txtEmail.Text.Trim())) { MessageBox.Show("The email field is empty or incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { email = txtEmail.Text.Trim(); } String occupation; if (cmbOccupation.SelectedItem == null) { MessageBox.Show("Please select an occupation", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); btnClearAll.PerformClick(); return; } else { occupation = cmbOccupation.Text; } String gender; if (radMale.Checked) { gender = radMale.Text; } else { gender = radFemale.Text; } DateTime inTime = DateTime.Now; DateTime outTime = default(DateTime); TimeSpan opens = new TimeSpan(10, 0, 0); TimeSpan closes = new TimeSpan(17, 0, 0); DayOfWeek day = inTime.DayOfWeek; //if (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday) //{ // MessageBox.Show("The museum is closed."); // btnClearAll.PerformClick(); // return; //} //else if (inTime.TimeOfDay > opens && inTime.TimeOfDay < closes) //{ //} //else //{ // MessageBox.Show("The musuem is now close, please visit between 10 AM and 5 PM"); // btnClearAll.PerformClick(); // return; //} String phNo; if (String.IsNullOrEmpty(txtPhNo.Text.Trim()) || !rx.IsMatch(txtPhNo.Text.Trim())) { MessageBox.Show("The phone field is empty or incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { phNo = txtPhNo.Text.Trim(); } int check = ValidateRedundancy(visitorName, phNo, occupation, gender, email); if (check == 0) { MessageBox.Show("This is an old visitor.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); btnClearAll.PerformClick(); return; } Visitors visitors = new Visitors(cardNo, visitorName, phNo, email, occupation, gender, inTime, outTime, day); LsVisitors.Add(visitors); String data = cardNo + "," + visitorName + "," + phNo + "," + email + "," + occupation + "," + gender + "," + inTime + "," + outTime + "," + day; ToCSV(data); LoadGrid(); MessageBox.Show("The visitor has been registered and checked in with card no." + cardNo + ".", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception f) { MessageBox.Show("The values entered are either missing or incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }