protected void btnInsert_Click(object sender, EventArgs e) { bool isValidOperation = true; string insertQuery = ""; SqlDataReader listDaySession; if (txtComCode.Text != "" && ddlTutor.SelectedValue != "") { string DaySession; DBCon myCon2 = new DBCon(); string query = "select DaySession from tblCourseTeacherMap where ComCod = '" + ddlCourseTitle.SelectedValue + "' and TeacherID = '" + ddlTutor.SelectedValue + "'"; if (ddlOperation.SelectedValue == "Insert") { myCon.ConOpen(); listDaySession = myCon.ExecuteReader(query); while (listDaySession.Read() && isValidOperation) { DaySession = listDaySession.GetString(0); string checkQuery = "select count(*) from tblStudentCourseMap where DaySession = '" + DaySession + "'"; int Occurence = myCon2.ExecuteScalarInt(checkQuery); if (Occurence > 0) { Response.Write("<script LANGUAGE='JavaScript' >alert('" + DaySession + " has another entry, aborting operation. Please remove entry and try again.');</script>"); isValidOperation = false; break; } } myCon.ConClose(); } myCon.ConOpen(); query = "select DaySession from tblCourseTeacherMap where ComCod = '" + ddlCourseTitle.SelectedValue + "' and TeacherID = '" + ddlTutor.SelectedValue + "'"; listDaySession = myCon.ExecuteReader(query); while (listDaySession.Read() && isValidOperation) { DaySession = listDaySession.GetString(0); if (ddlOperation.SelectedValue == "Insert") { insertQuery = "insert into tblStudentCourseMap(StudentID,ComCod,DaySession,TeacherID) values ('" + Current_User_ID + "','" + txtComCode.Text + "','" + DaySession + "','" + ddlTutor.SelectedValue + "')"; } else { if (ddlOperation.SelectedValue == "Remove") { insertQuery = "Delete from tblStudentCourseMap where StudentID = '" + Current_User_ID + "' and ComCod = '" + txtComCode.Text + "' and DaySession = '" + DaySession + "' and TeacherID = '" + ddlTutor.SelectedValue + "'"; } } myCon2.ExecuteNonQuery(insertQuery); } myCon.ConClose(); if (isValidOperation) { Make_Dirty_Approval_Status(); Response.Redirect(Request.RawUrl); } } }