private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { con.Open(); int URow = int.Parse(e.RowIndex.ToString()); int URowIndex = int.Parse(e.ColumnIndex.ToString()); ID = Convert.ToInt32(dataGridView1.Rows[URow].Cells[0].Value.ToString()); ID1 = Convert.ToInt32(dataGridView1.Rows[URow].Cells[1].Value.ToString()); int dd = (int)dataGridView1.CurrentRow.Cells[0].Value; if (URowIndex == 5) { int d1 = (int)dataGridView1.CurrentRow.Cells[0].Value; if (URowIndex == 0) { MessageBox.Show("Click again"); con.Close(); } if (URowIndex != 0) { var askfirst1 = MessageBox.Show("Are you sure you want to chnage project of this advisor?", "Update", MessageBoxButtons.YesNo); if (askfirst1 == DialogResult.Yes) { Flagg2 = ID; SqlDataAdapter sd = new SqlDataAdapter("Select * From project", con); DataTable ds = new DataTable(); sd.Fill(ds); foreach (DataRow row in ds.Rows) { if (row["Id"].ToString() == dataGridView1.Rows[e.RowIndex].Cells["ProjectId"].FormattedValue.ToString()) { comboBox1.Text = row["Title"].ToString(); } } textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value) == 11) { comboBox2.Text = "Main Advisor"; } if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value) == 12) { comboBox2.Text = "Co-Advisror"; } if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value) == 14) { comboBox2.Text = "Industry Advisor"; } con.Close(); } else { Flagg2 = 0; con.Close(); this.Hide(); ProjectAdvisor f2 = new ProjectAdvisor(); f2.ShowDialog(); } } } else if (URowIndex == 6) { ID = Convert.ToInt32(dataGridView1.Rows[URow].Cells[0].Value.ToString()); if (URowIndex == 0) { MessageBox.Show("Click on Delete Again"); } if (URowIndex != 0) { var askfirst = MessageBox.Show("Are you sure you want to delete this?", "Delete", MessageBoxButtons.YesNo); if (askfirst == DialogResult.Yes) { cmd1 = new SqlCommand("DELETE FROM ProjectAdvisor where AdvisorId = @Id AND ProjectId = @Id1", con); cmd1.Parameters.AddWithValue("@Id", ID); cmd1.Parameters.AddWithValue("@Id1", ID1); cmd1.ExecuteNonQuery(); con.Close(); MessageBox.Show("Deleted Succesfully"); this.Hide(); ProjectAdvisor f2 = new ProjectAdvisor(); f2.ShowDialog(); this.Close(); } else { con.Close(); this.Hide(); ProjectAdvisor f2 = new ProjectAdvisor(); f2.ShowDialog(); } } else { Flagg2 = 0; con.Close(); } } }
private void Insert_Click(object sender, EventArgs e) { bool x = adId(); if (x == false) { bool a = projectId(); if (a == false) { bool t = adRole(); if (t == false) { bool c = StId_Exists(Convert.ToInt32(textBox1.Text), comboBox1.Text); if (c == false) { if (Flagg2 == 0) { con.Open(); string ProjectId = string.Format("SELECT Id From Project WHERE Title = '{0}'", comboBox1.Text); SqlCommand cmd = new SqlCommand(ProjectId, con); int p = (Int32)cmd.ExecuteScalar(); string RoleId = string.Format("SELECT Id From Lookup WHERE Value = '{0}'", comboBox2.Text); SqlCommand cmd2 = new SqlCommand(RoleId, con); int s = (Int32)cmd2.ExecuteScalar(); DateTime time = DateTime.Now; cmd2 = new SqlCommand("insert into ProjectAdvisor(AdvisorId, ProjectId, AdvisorRole, AssignmentDate) values ('" + textBox1.Text + "','" + p + "','" + s + "' , @time)", con); cmd2.Parameters.AddWithValue("@time", time); //int gI = Convert.ToInt32(comboBox1.Text); cmd2.ExecuteNonQuery(); MessageBox.Show("Inserted Successfully"); con.Close(); this.Hide(); ProjectAdvisor f4 = new ProjectAdvisor(); f4.ShowDialog(); this.Close(); } if (Flagg2 > 0) { con.Open(); string ProjectId = string.Format("SELECT Id From Project WHERE Title = '{0}'", comboBox1.Text); SqlCommand cmd = new SqlCommand(ProjectId, con); int p = (Int32)cmd.ExecuteScalar(); string RoleId = string.Format("SELECT Id From Lookup WHERE Value = '{0}'", comboBox2.Text); SqlCommand cmd2 = new SqlCommand(RoleId, con); int s = (Int32)cmd2.ExecuteScalar(); DateTime time = DateTime.Now; cmd2 = new SqlCommand("UPDATE ProjectAdvisor set AdvisorId = @AdvisorId , ProjectId = @ProjectId, AdvisorRole = @AdvisorRole, AssignmentDate = @AssignmentDate WHERE AdvisorId = @d1 AND ProjectId = @d2", con); cmd2.Parameters.AddWithValue("@d1", Flagg2); cmd2.Parameters.AddWithValue("@d2", ID1); cmd2.Parameters.AddWithValue("@AdvisorId", Convert.ToInt32(textBox1.Text)); cmd2.Parameters.AddWithValue("@ProjectId", Convert.ToInt32(p)); cmd2.Parameters.AddWithValue("@AdvisorRole", s); cmd2.Parameters.AddWithValue("@AssignmentDate", time); cmd2.ExecuteNonQuery(); MessageBox.Show("Updated successfully Successfully"); con.Close(); this.Hide(); ProjectAdvisor f4 = new ProjectAdvisor(); f4.ShowDialog(); this.Close(); } } else { MessageBox.Show("This project is already assigned to this Advisor"); } } else { MessageBox.Show("Select Advisor role"); } } else { MessageBox.Show("First add projects"); } } else { MessageBox.Show("Select Addvisor"); } }