//update station private void updateStationbutton_Click(object sender, EventArgs e) { if (updateStationTextBox.Text == "") { MessageBox.Show("Select a record"); } else { //try updating station with corresponding id and refresh data try { String station = updateStationTextBox.Text; DbHandler.UpdateStation(stationId, station); DisplayData(); ClearData(); MessageBox.Show("Successfully data updated!"); } catch (Exception exc) { Console.WriteLine(exc); } } }
private void removeButton_Click(object sender, EventArgs e) { string sRemoveTextbox = idRemoveTextbox.Text; if (sRemoveTextbox == "") { MessageBox.Show("Select a record!"); } else { DialogResult dialog = MessageBox.Show("Are you sure you want to delete this record?", "Confirmation", MessageBoxButtons.YesNoCancel); if (dialog == DialogResult.Yes) { DbHandler.DeleteDistance(id); RefreshDataGridView(); MessageBox.Show("Successfully data deleted"); } else if (dialog == DialogResult.No) { MessageBox.Show("Nothing's been changed"); } } }
private void addButton_Click(object sender, EventArgs e) { try { connString.Open(); } catch (Exception ex) { Console.WriteLine("Exception in DBHandler", ex); } selectedLine1 = line1Combobox.SelectedValue.ToString(); selectedLine2 = line2Combobox.SelectedValue.ToString(); selectedStation = station1Combobox.SelectedValue.ToString(); selectedStation2 = station2Combobox.SelectedValue.ToString(); string sCheckDistanceTextBox = addDistanceTextbox.Text.ToString(); int intDistanceTextBox; var intCheckDistanceTextBox = Int32.TryParse(addDistanceTextbox.Text, out intDistanceTextBox); // reading from distance table checking for already added distance between two stations with same train lines string myQuery = "Select Count(*) From Distance Where Station1 = @station1 " + "AND Station2 = @station2 AND Line1 = @line1 AND Line2 = @line2 "; SqlCommand myCommand = new SqlCommand(myQuery, connString); myCommand.Parameters.AddWithValue("@station1", selectedStation); myCommand.Parameters.AddWithValue("@station2", selectedStation2); myCommand.Parameters.AddWithValue("@line1", selectedLine1); myCommand.Parameters.AddWithValue("@line2", selectedLine2); //reading the data int rows = (int)myCommand.ExecuteScalar(); //if there is 1 matching then dont add if (rows > 0) { MessageBox.Show("Station 1: " + selectedStation + " for line: " + selectedLine1 + " and Station 2: " + selectedStation2 + " for line 2: " + selectedLine2 + " are already been added!"); errorOccured = 1; connString.Close(); } try { connString.Open(); } catch (Exception ex) { Console.WriteLine("Exception in DBHandler", ex); } // reading from distance table checking for already added distance between two stations but selected in opposite comboboxes //so there is no duplicates in either stations with same train lines string myQuery2 = "Select Count(*) From Distance Where Station1 = @sStation2 " + "AND Station2 = @sStation1 AND Line1 = @sLine1 AND Line2 = @sLine2 "; SqlCommand myCommand2 = new SqlCommand(myQuery2, connString); myCommand2.Parameters.AddWithValue("@sStation2", selectedStation2); myCommand2.Parameters.AddWithValue("@sStation1", selectedStation); myCommand2.Parameters.AddWithValue("@sLine1", selectedLine1); myCommand2.Parameters.AddWithValue("@sLine2", selectedLine2); //reading the data int rows2 = (int)myCommand2.ExecuteScalar(); //if there is 1 matching then dont add if (rows2 > 0) { MessageBox.Show("second error: Station 1: " + selectedStation + " for line: " + selectedLine1 + " and Station 2: " + selectedStation2 + " for line 2: " + selectedLine2 + " are already been added!"); connString.Close(); } //distance textbox cannot be empty else if (sCheckDistanceTextBox == "") { MessageBox.Show("You need to enter some value"); } //if the distance is lower than 0 then output error else if (intDistanceTextBox == 0 || intDistanceTextBox < 0) { MessageBox.Show("Distance should be more than '0'"); } //if the lines aren't the same else if (!selectedLine1.Equals(selectedLine2)) { MessageBox.Show("Not allowed to choose different lines!"); } //if the stations are the same else if (selectedStation.Equals(selectedStation2)) { MessageBox.Show("Not allowed to choose same stations!"); } else { if (errorOccured == 1) { errorOccured = 0; } else { DialogResult dialog = MessageBox.Show("Are you sure you want to continue?", "Confirmation", MessageBoxButtons.YesNoCancel); if (dialog == DialogResult.Yes) { //add the data to the database DbHandler.AddDistance(selectedStation, selectedStation2, selectedLine1, selectedLine2, sCheckDistanceTextBox); RefreshDataGridView(); MessageBox.Show("Data successfully added!"); } else if (dialog == DialogResult.No) { MessageBox.Show("Nothing's been changed"); } } //close connection connString.Close(); } }