private void showTeamsToolStripMenuItem_Click(object sender, EventArgs e) { TeamDashbord td = new TeamDashbord(); td.MdiParent = this; td.Show(); }
private void bt_DeleteTeam_Click(object sender, EventArgs e) { Boolean deleted = false; try { int index = dataGridView1.CurrentRow.Index; if (index == -1) { throw new Exception("You should select a line on the datagrid to remove it"); } ; int id = (int)dataGridView1[0, index].Value; DialogResult confirmation = MessageBox.Show("You want really delete the selected team ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmation == DialogResult.Yes) { SqlConnection con; using (con = new SqlConnection(@"Data Source=DESKTOP-7J9ODH9;Initial Catalog=SoccerRobotDB;Integrated Security=True;Pooling=False")) { //2-open the connection con.Open(); //3-create the command object SqlCommand cmd = new SqlCommand("delete from [Team] where id=" + id, con); //4-Execute command //three posibilities //cmd.executeReader()===>select //or cmd.ExecuteNonQuery() ===>insert/update/delete //or cmd.ExecuteScalar() ==>select Max, Min, AVG, count, SUM (aggregation) cmd.ExecuteNonQuery(); MessageBox.Show("The Team has been successfuly deleted"); deleted = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (deleted) { this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = TeamDashbord.getAllTeams(); } } }
private void bt_AddTeam_Click(object sender, EventArgs e) { //Connected Mode //use of classes : xyzConnction, xyzCommand, xyzDataReader //xyz may be sql -->sql server //xyz may be oracle -->Oracle //xyz may be Mysql -->Mysql //xyz may be oledb or odbc : generic driver if (Action == "Add") { Boolean added = false; //step 1 : create connection try { //con = new SqlConnection(@"Data Source=DESKTOP-7J9ODH9;Initial Catalog=SoccerRobotDB;Integrated Security=True;Pooling=False"); //xyz may be sql -->sql server //we could use the property ConnectionString to set the connection string // con.ConnectionString = @"Data Source=DESKTOP-7J9ODH9;Initial Catalog=SoccerRobotDB;Integrated Security=True;Pooling=False"; //step 2 : open the connection con.Open(); //step 3 : Create command //SqlCommand cmd = new SqlCommand("insert into values....",con); //or cmd.CommandText = "insert into Team(Name,Color) values(@Name,@Color)"; cmd.Connection = con; cmd.CommandType = CommandType.Text;//optional //cmd.Parameters.AddWithValue("@Id", int.Parse(txt_Id.Text)); //Id is identity cmd.Parameters.AddWithValue("@Name", txt_Name.Text); cmd.Parameters.AddWithValue("@Color", txt_Color.Text); //cmd.Parameters.Add("@Id", SqlDbType.Int).Direction = ParameterDirection.Output; //the @id will be an output //step 4 : Execute command cmd.ExecuteNonQuery(); //is used for insert, update and delete //cmd.ExecuteReader(); //used in select case MessageBox.Show("The Team has been successufly added"); added = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //last step close the connection if (con != null && con.State == ConnectionState.Open) { con.Close(); } if (added) { this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = TeamDashbord.getAllTeams(); Reset(); txt_Id.Text = (getLastId() + 10).ToString(); } } //or we could use "using", so we don't have to call open nother close //step 1 : create connection /* using (SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-7J9ODH9;Initial Catalog=SoccerRobotDB;Integrated Security=True;Pooling=False")) * { * //step 1 * con.Open() * * //step 2 * //..... * * }//end use==>con.Close() */ } else { Boolean updated = false; try { //step 2 : open the connection con.Open(); //step 3 : Create command //SqlCommand cmd = new SqlCommand("insert into values....",con); //or cmd.CommandText = "update Team set Name = @Name, Color = @Color where id=@Id;"; cmd.Connection = con; cmd.CommandType = CommandType.Text;//optional cmd.Parameters.AddWithValue("@Id", int.Parse(txt_Id.Text)); cmd.Parameters.AddWithValue("@Name", txt_Name.Text); cmd.Parameters.AddWithValue("@Color", txt_Color.Text); //step 4 : Execute command cmd.ExecuteNonQuery(); //is used for insert, update and delete //cmd.ExecuteReader(); //used in select case MessageBox.Show("The Team has been successufly updated"); updated = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { //last step close the connection if (con != null && con.State == ConnectionState.Open) { con.Close(); } if (updated) { this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = TeamDashbord.getAllTeams(); } } } }