private void btnSave_Click(object sender, EventArgs e) { try { if (txtBuildingName.Text == "" || txtRoomName.Text == "" || txtCapacity.Text == "") { MessageBox.Show("Please insert all fields!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { string roomType; if (rbtLectureHall.Checked) { roomType = "Lecture Hall"; } else { roomType = "Laboratory"; } SQLiteConnection connection = new Classes.SqliteHelper().GetSQLiteConnection(); connection.Open(); SQLiteCommand command = new SQLiteCommand("INSERT INTO [Location](Building, Room, Room_Type, Capacity) " + " VALUES ('" + txtBuildingName.Text + "','" + txtRoomName.Text + "','" + roomType + "','" + txtCapacity.Text + "')", connection); int i = command.ExecuteNonQuery(); if (i != 0) { clear(); connection.Close(); MessageBox.Show("Data Saved"); ManageLocations frm2 = new ManageLocations(); frm2.Show(); this.Hide(); } else { connection.Close(); MessageBox.Show("Data not Saved", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public DataTable SelectSessions(SessionType session) { SelectedSessionType = session; //DB connection SQLiteConnection conn = new Classes.SqliteHelper().GetSQLiteConnection(); DataTable dt = new DataTable(); try { //sql query string sql = "Select * from sessionCat where Type = @session"; //creating cmd using sql and conn SQLiteCommand cmd = new SQLiteCommand(sql, conn); cmd.Parameters.AddWithValue("@session", session.ToString()); //creating sql data adapted using cmd SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd); conn.Open(); adapter.Fill(dt); } catch (Exception ex) { } finally { conn.Close(); } return(dt); }
private void loadData() { try { SQLiteConnection con = new Classes.SqliteHelper().GetSQLiteConnection(); //SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C: \Users\User\Desktop\Time Table MS\Time Table MS\Database1.mdf; Integrated Security = True"); con.Open(); String cmd = "SELECT * FROM WorkingDaysHours"; SQLiteCommand command = new SQLiteCommand(cmd, con); dataGridView1.DataSource = null; dataGridView1.Rows.Clear(); SQLiteDataAdapter dAdapter = new SQLiteDataAdapter(command); DataSet ds = new DataSet(); dAdapter.Fill(ds); dataGridView1.ReadOnly = true; dataGridView1.DataSource = ds.Tables[0]; con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btn_Delete_Click(object sender, EventArgs e) { var confirmResult = MessageBox.Show("Are you sure to delete this student Group ??", "Confirm Delete!!", MessageBoxButtons.YesNo); if (confirmResult == DialogResult.Yes) { SQLiteConnection con = new Classes.SqliteHelper().GetSQLiteConnection(); //SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C: \Users\User\Desktop\Time Table MS\Time Table MS\Database1.mdf; Integrated Security = True"); con.Open(); String cmd = "DELETE FROM WorkingDaysHours WHERE Id='" + ID + "';"; SQLiteCommand command = new SQLiteCommand(cmd, con); int i = command.ExecuteNonQuery(); if (i != 0) { loadData(); numericUpDown1.ResetText(); mon_check.Checked = false; tue_check.Checked = false; wed_check.Checked = false; thu_check.Checked = false; fri_check.Checked = false; sun_check.Checked = false; sat_check.Checked = false; comboBox1.ResetText(); comboBox2.ResetText(); con.Close(); MessageBox.Show("Data Deleted"); } else { MessageBox.Show("Data not Deleted"); con.Close(); } } }
private void btnRemove_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { DataGridViewRow selected = dataGridView1.SelectedRows[0]; if (selected != null) { //delete SQLiteConnection conn = new Classes.SqliteHelper().GetSQLiteConnection(); DataTable dt = new DataTable(); try { String sessionID = selected.Cells[11].Value.ToString(); //sql query string sql = "Delete from sessionCat where sessionID = @sessionID"; //creating cmd using sql and conn SQLiteCommand cmd = new SQLiteCommand(sql, conn); cmd.Parameters.AddWithValue("@sessionID", sessionID); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); dataGridView1.DataSource = SelectSessions(SelectedSessionType); MessageBox.Show("Deleted From Database !"); } catch (Exception ex) { } finally { conn.Close(); } } } }
private void btn_Update_Click(object sender, EventArgs e) { int monday, tuesday, wednessday, thursday, friday, saturday, sunday; monday = tuesday = wednessday = thursday = friday = saturday = sunday = 0; if (mon_check.Checked) { monday = 1; } if (tue_check.Checked) { tuesday = 1; } if (wed_check.Checked) { wednessday = 1; } if (thu_check.Checked) { thursday = 1; } if (fri_check.Checked) { friday = 1; } if (sat_check.Checked) { saturday = 1; } if (sun_check.Checked) { sunday = 1; } SQLiteConnection con = new Classes.SqliteHelper().GetSQLiteConnection(); // SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C: \Users\User\Desktop\Time Table MS\Time Table MS\Database1.mdf; Integrated Security = True"); con.Open(); String cmd = "UPDATE WorkingDaysHours SET NoOfWorkingDays='" + numericUpDown1.Text + "', Monday='" + monday + "', Tuesday='" + tuesday + "', Wednesday='" + wednessday + "',Thursday='" + thursday + "',Friday='" + friday + "' ,Saturday='" + saturday + "',Sunday='" + sunday + "' ,StartTime='" + comboBox1.Text + "' ,StopTime='" + comboBox2.Text + "' WHERE Id='" + ID + "';"; SQLiteCommand command = new SQLiteCommand(cmd, con); int i = command.ExecuteNonQuery(); if (i != 0) { loadData(); numericUpDown1.ResetText(); mon_check.Checked = false; tue_check.Checked = false; wed_check.Checked = false; thu_check.Checked = false; fri_check.Checked = false; sun_check.Checked = false; sat_check.Checked = false; comboBox1.ResetText(); comboBox2.ResetText(); con.Close(); MessageBox.Show("Data Updated"); } else { MessageBox.Show("Data not Updated"); con.Close(); } }