public List <Labourer> getAllLabourer(MySqlConnection conn) { List <Labourer> listLabr = new List <Labourer>(); string sql = "SELECT * FROM labourer WHERE 1 "; MySqlCommand sqlComm = new MySqlCommand(sql, conn); try { MySqlDataReader myReader; myReader = sqlComm.ExecuteReader(); while (myReader.Read()) { Labourer sLabr = new Labourer(); sLabr.Id = (int)myReader.GetValue(0); sLabr.Name = (string)myReader.GetValue(1); sLabr.Age = (int)myReader.GetValue(2); sLabr.Gender = (string)myReader.GetValue(3); sLabr.StartDate = (string)myReader.GetValue(4); sLabr.EndDate = (string)myReader.GetValue(5); listLabr.Add(sLabr); } } catch (Exception e) { Console.WriteLine(e.ToString()); } return(listLabr); }
public int addNewLabourer(MySqlConnection conn, Labourer labourer) { string sql = "INSERT INTO labourer (name, age,gender,startWork,endWork)" + "VALUES('" + labourer.Name + "', '" + labourer.Age + "','" + labourer.Gender + "','" + labourer.StartDate + "','" + labourer.EndDate + "')"; MySqlCommand sqlComm = new MySqlCommand(sql, conn); return(sqlComm.ExecuteNonQuery()); }
private void addLabourerBtn_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(labourerNameTextBox.Text)) { MessageBox.Show("Please enter the name of labourer"); return; } if (String.IsNullOrEmpty(ageTextBox.Text)) { MessageBox.Show("Please enter the age of labourer"); return; } if (genderComboBox.SelectedItem == null) { MessageBox.Show("Please select the gender"); return; } if (String.IsNullOrEmpty(startWorkDate.Text)) { MessageBox.Show("Please select Start Work Date"); return; } if (String.IsNullOrEmpty(endWorkDate.Text)) { MessageBox.Show("Please select End Work Date"); return; } DBConnector dBConn = new DBConnector(); dBConn.connect(); Labourer labr = new Labourer(); labr.Name = labourerNameTextBox.Text; labr.Age = int.Parse(ageTextBox.Text); labr.Gender = genderComboBox.SelectedItem.ToString(); labr.StartDate = startWorkDate.Text; labr.EndDate = endWorkDate.Text; LabourerHandler labHnd = new LabourerHandler(); int recordCnt = labHnd.addNewLabourer(dBConn.getConn(), labr); MessageBox.Show(recordCnt + "record has been inserted!!"); labourerGridView.DataSource = labHnd.getAllLabourer(dBConn.getConn()); }