private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { SqlConnection con = new SqlConnection(conURL); con.Open(); int currentRow = int.Parse(e.RowIndex.ToString()); int currentColumnIndex = int.Parse(e.ColumnIndex.ToString()); int gid = Convert.ToInt32(dataGridView1.Rows[currentRow].Cells[2].Value.ToString()); if (currentColumnIndex == 0) { string count = "SELECT COUNT(*), GroupId, Status FROM GroupStudent Group By Status, GroupId Having COUNT(*) = 4 AND Status = @statval AND GroupId = @gidval"; SqlCommand cmd3 = new SqlCommand(count, con); string statusval = string.Format("SELECT Id FROM Lookup WHERE Value = '{0}'", "Active"); SqlCommand cmd4 = new SqlCommand(statusval, con); int statid1 = (Int32)cmd4.ExecuteScalar(); cmd3.Parameters.AddWithValue("@statval", statid1); cmd3.Parameters.AddWithValue("@gidval", gid); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd3; DataTable countdt = new DataTable(); sda.Fill(countdt); if (countdt.Rows.Count > 0) { MessageBox.Show("Group already contains 4 active memebers"); } else { ManageGroups_Students f3 = new ManageGroups_Students(gid); this.Close(); f3.Show(); } } if (currentColumnIndex == 1) { ManageStudentGroups1 f4 = new ManageStudentGroups1(gid); this.Close(); f4.Show(); } }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { SqlConnection con = new SqlConnection(conURL); con.Open(); int currentRow = int.Parse(e.RowIndex.ToString()); int currentColumnIndex = int.Parse(e.ColumnIndex.ToString()); string regno = dataGridView1.Rows[currentRow].Cells[3].Value.ToString(); if (currentColumnIndex == 0) { string count = "SELECT COUNT(*), GroupId, Status FROM GroupStudent Group By Status, GroupId Having COUNT(*) = 4 AND Status = @statval AND GroupId = @gidval"; SqlCommand cmd3 = new SqlCommand(count, con); string statusval = string.Format("SELECT Id FROM Lookup WHERE Value = '{0}'", "Active"); SqlCommand cmd4 = new SqlCommand(statusval, con); int statid1 = (Int32)cmd4.ExecuteScalar(); cmd3.Parameters.AddWithValue("@statval", statid1); cmd3.Parameters.AddWithValue("@gidval", flag); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd3; DataTable countdt = new DataTable(); sda.Fill(countdt); if (countdt.Rows.Count > 0) { MessageBox.Show("Cannot add more than 4 active memebers"); } else { string groupst = "INSERT INTO GroupStudent(GroupId, StudentId, Status, AssignmentDate) values(@GroupId,@StudentId, @Status, @AssignmentDate)"; SqlCommand cmd = new SqlCommand(groupst, con); cmd.Parameters.AddWithValue("@GroupId", flag); string reg = string.Format("SELECT Id FROM Student WHERE RegistrationNo = '{0}'", regno); SqlCommand cmd1 = new SqlCommand(reg, con); int stid = (Int32)cmd1.ExecuteScalar(); cmd.Parameters.AddWithValue("@StudentId", stid); string status = string.Format("SELECT Id FROM Lookup WHERE Value = '{0}'", "Active"); SqlCommand cmd2 = new SqlCommand(status, con); int statid = (Int32)cmd2.ExecuteScalar(); cmd.Parameters.AddWithValue("@Status", statid); string status1 = string.Format("SELECT Id FROM Lookup WHERE Value = '{0}'", "InActive"); SqlCommand cmd6 = new SqlCommand(status1, con); int statid6 = (Int32)cmd6.ExecuteScalar(); string update = "UPDATE GroupStudent set Status = @val5 WHERE StudentId = @val6"; SqlCommand cmd5 = new SqlCommand(update, con); cmd5.Parameters.AddWithValue("@val5", statid6); cmd5.Parameters.AddWithValue("@val6", stid); cmd5.ExecuteNonQuery(); cmd.Parameters.AddWithValue("@AssignmentDate", DateTime.Today); cmd.ExecuteNonQuery(); MessageBox.Show("Student Added to Group"); ManageGroups_Students form = new ManageGroups_Students(flag); this.Close(); form.Show(); } } }