using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Windows.Forms;
using OgrenciKayit.eokulformudsTableAdapters;
using System.Globalization;
using static OgrenciKayit.Form1;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices.ComTypes;
using System.Configuration;
using System.IdentityModel.Protocols.WSTrust;
public partial class Form1 : Form
private int extraTeacherIndex = 0;
private void Form1_Load(object sender, EventArgs e)
this.ogretmenBilgileriTableAdapter.FillOgretmenBilgileri(this.eokulformuds1.ogretmenBilgileri);
string connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "select id, adi, soyadi from ogretmenBilgileri where nobet = 'Evet'";
dr = cmd.ExecuteReader();
ListViewItem oge = new ListViewItem(dr["adi"].ToString() + " " + dr["soyadi"].ToString().ToUpper());
listView1.Items.Add(oge);
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "select id, nobet_yeri from nobetYerleri";
dr = cmd.ExecuteReader();
ListViewItem oge = new ListViewItem(dr["nobet_yeri"].ToString());
listView2.Items.Add(oge);
extraDutyPlaceComboBox.Items.Add(oge.Text);
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "SELECT ConfigValue FROM AppConfig WHERE ConfigKey = 'periodLength'";
dr = cmd.ExecuteReader();
periodLength = dr.GetInt32(0);
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "SELECT ConfigValue FROM AppConfig WHERE ConfigKey = 'weekCount'";
dr = cmd.ExecuteReader();
weekCount = dr.GetInt32(0);
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "SELECT ConfigValue FROM AppConfig WHERE ConfigKey = 'extraTeacherIndex'";
dr = cmd.ExecuteReader();
extraTeacherIndex = dr.GetInt32(0);
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "SELECT ConfigValue FROM AppConfig WHERE ConfigKey = 'rotationCount'";
using (SqlDataReader dr = cmd.ExecuteReader())
rotationCount = dr.GetInt32(0);
public void button2_Click(object sender, EventArgs e)
string connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
DateTime startDate = monthCalendar1.SelectionStart;
DateTime endDate = monthCalendar1.SelectionEnd;
List<DateTime> excludedDates = new List<DateTime>();
foreach (var item in excludedDatesListBox.Items)
excludedDates.Add(Convert.ToDateTime(item));
List<DateTime> extraDutyDates = new List<DateTime>();
if (extraDutyDatesListBox.Items.Count > 0)
foreach (var item in extraDutyDatesListBox.Items)
extraDutyDates.Add(Convert.ToDateTime(item));
string extraDutyPlace = "";
if (extraDutyPlaceComboBox.SelectedItem != null)
extraDutyPlace = extraDutyPlaceComboBox.SelectedItem.ToString();
List<string> teachers = new List<string>();
foreach (ListViewItem item in listView1.Items)
List<string> dutyPlaces = new List<string>();
foreach (ListViewItem item in listView2.Items)
dutyPlaces.Add(item.Text);
Dictionary<string, DateTime> lastDutyDates = new Dictionary<string, DateTime>();
Dictionary<string, int> dutyCounts = new Dictionary<string, int>();
Dictionary<string, List<string>> teacherDutyPlaces = new Dictionary<string, List<string>>();
for (int i = 0; i < rotationCount; i++)
string firstPlace = dutyPlaces[0];
dutyPlaces.Add(firstPlace);
if (teachers.Count > dutyPlaces.Count * 5 && extraDutyPlaceComboBox.SelectedItem == null)
MessageBox.Show("Öğretmen sayısı haftalık nöbet yeri sayısından fazla ve nöbet yeri seçimi yapılmamış. Lütfen bir nöbet yeri seçin.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
int excessTeachers = teachers.Count - (dutyPlaces.Count * 5);
List<string> assignedTeachers = new List<string>();
List<string> holidayTeachers = new List<string>();
ReportForm reportForm = new ReportForm(dutyCounts, dutyPlaces,teachers);
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(reportForm.reportDataGridView);
row.Cells[0].Value = date.ToShortDateString();
row.Cells[1].Value = CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek);
if (weekCount >= periodLength)
string firstPlace = dutyPlaces[0];
dutyPlaces.Add(firstPlace);
if (date.DayOfWeek == DayOfWeek.Friday)
assignedTeachers.Clear();
if (date.DayOfWeek == DayOfWeek.Monday)
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
foreach (string place in dutyPlaces)
if (excludedDates.Contains(date))
if (teacherIndex < teachers.Count)
teacher = teachers[teacherIndex];
holidayTeachers.Add(teacher);
if (holidayTeachers.Count > 0 && teacherIndex >= teachers.Count)
teacher = holidayTeachers[0];
holidayTeachers.RemoveAt(0);
else if (teacherIndex < teachers.Count)
teacher = teachers[teacherIndex];
if (extraTeacherIndex >= teachers.Count)
teacher = teachers[extraTeacherIndex];
lastDutyDates[teacher] = date;
if (dutyCounts.ContainsKey(teacher))
assignedTeachers.Add(teacher);
row.Cells[cellIndex].Value = teacher;
reportForm.reportDataGridView.Rows.Add(row);
if (date.DayOfWeek == DayOfWeek.Monday && teachers.Count == dutyPlaces.Count * 5 + 1)
string teacher = teachers[teacherIndex];
if (excludedDates.Contains(date))
holidayTeachers.Add(teachers[teacherIndex]);
string columnName = "2. " + extraDutyPlaceComboBox.SelectedItem.ToString() + " Öğretmeni ";
if (!reportForm.reportDataGridView.Columns.Contains(columnName))
reportForm.reportDataGridView.Columns.Add(columnName, columnName);
foreach (DataGridViewRow rowk in reportForm.reportDataGridView.Rows)
if (row.Cells[0].Value != null && DateTime.TryParse(row.Cells[0].Value.ToString(), out DateTime rowDate))
if (rowDate.Date == date.Date)
row.Cells[reportForm.reportDataGridView.Columns[columnName].Index].Value = teacher;
if ((date.DayOfWeek == DayOfWeek.Monday || date.DayOfWeek == DayOfWeek.Friday) && teachers.Count == dutyPlaces.Count * 5 + 2)
string teacher = teachers[teacherIndex];
if (excludedDates.Contains(date))
holidayTeachers.Add(teachers[teacherIndex]);
string columnName = "2. " + extraDutyPlaceComboBox.SelectedItem.ToString() + " Öğretmeni ";
if (!reportForm.reportDataGridView.Columns.Contains(columnName))
reportForm.reportDataGridView.Columns.Add(columnName, columnName);
foreach (DataGridViewRow rowk in reportForm.reportDataGridView.Rows)
if (row.Cells[0].Value != null && DateTime.TryParse(row.Cells[0].Value.ToString(), out DateTime rowDate))
if (rowDate.Date == date.Date)
row.Cells[reportForm.reportDataGridView.Columns[columnName].Index].Value = teacher;
if ((date.DayOfWeek == DayOfWeek.Monday || date.DayOfWeek == DayOfWeek.Wednesday || date.DayOfWeek == DayOfWeek.Friday) && teachers.Count == dutyPlaces.Count * 5 + 3)
string teacher = teachers[teacherIndex];
if (excludedDates.Contains(date))
holidayTeachers.Add(teachers[teacherIndex]);
string columnName = "2. " + extraDutyPlaceComboBox.SelectedItem.ToString() + " Öğretmeni ";
if (!reportForm.reportDataGridView.Columns.Contains(columnName))
reportForm.reportDataGridView.Columns.Add(columnName, columnName);
foreach (DataGridViewRow rowk in reportForm.reportDataGridView.Rows)
if (row.Cells[0].Value != null && DateTime.TryParse(row.Cells[0].Value.ToString(), out DateTime rowDate))
if (rowDate.Date == date.Date)
row.Cells[reportForm.reportDataGridView.Columns[columnName].Index].Value = teacher;
if (extraDutyDates.Contains(date) && holidayTeachers.Count > 0)
string teacher = holidayTeachers[0];
holidayTeachers.RemoveAt(0);
string columnName = "2. " + extraDutyPlaceComboBox.SelectedItem.ToString() + " Öğretmeni ";
if (reportForm.reportDataGridView.Columns.Contains(columnName))
foreach (DataGridViewRow rowt in reportForm.reportDataGridView.Rows)
if (row.Cells[0].Value != null && DateTime.TryParse(row.Cells[0].Value.ToString(), out DateTime rowDate))
if (rowDate.Date == date.Date)
row.Cells[reportForm.reportDataGridView.Columns[columnName].Index].Value = teacher;
DataGridViewTextBoxColumn textBoxColumn = new DataGridViewTextBoxColumn();
textBoxColumn.Name = columnName;
textBoxColumn.HeaderText = columnName;
int columnIndex = reportForm.reportDataGridView.Columns.Add(textBoxColumn);
foreach (DataGridViewRow rowt in reportForm.reportDataGridView.Rows)
if (row.Cells[0].Value != null && DateTime.TryParse(row.Cells[0].Value.ToString(), out DateTime rowDate))
if (rowDate.Date == date.Date)
row.Cells[columnIndex].Value = teacher;
foreach (KeyValuePair<string, int> entry in dutyCounts)
reportForm.dutyCountDataGridView.Rows.Add(entry.Key, entry.Value.ToString());
reportForm.saveButton.Click += (s, args) =>
DialogResult secenek = MessageBox.Show("Nöbet Listesini kaydetmek istiyor musunuz?", "Bilgilendirme Penceresi", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (secenek == DialogResult.Yes)
foreach (DataGridViewRow row in reportForm.reportDataGridView.Rows)
string date = row.Cells[0].Value.ToString();
string place = row.Cells[1].Value.ToString();
string teacher = row.Cells[2].Value.ToString();
string query = "INSERT INTO DutyReport (Date, Place, Teacher) VALUES (@Date, @Place, @Teacher)";
using (SqlCommand command = new SqlCommand(query, con))
command.Parameters.AddWithValue("@Date", Convert.ToDateTime(date));
command.Parameters.AddWithValue("@Place", place);
command.Parameters.AddWithValue("@Teacher", teacher);
command.ExecuteNonQuery();
foreach (DataGridViewRow row in reportForm.dutyCountDataGridView.Rows)
string teacher = row.Cells[0].Value.ToString();
int count = int.Parse(row.Cells[1].Value.ToString());
string query = "MERGE DutyCount AS target USING (SELECT @Teacher, @Count) AS source (Teacher, Count) ON (target.Teacher = source.Teacher) " +
"WHEN MATCHED THEN UPDATE SET Count = target.Count + source.Count WHEN NOT MATCHED BY TARGET THEN INSERT (Teacher, Count) VALUES (source.Teacher, source.Count);";
using (SqlCommand command = new SqlCommand(query, con))
command.Parameters.AddWithValue("@Teacher", teacher);
command.Parameters.AddWithValue("@Count", count);
command.ExecuteNonQuery();
foreach (DataGridViewRow row in reportForm.reportDataGridView.Rows)
string date = row.Cells[0].Value.ToString();
string place = row.Cells[1].Value.ToString();
string teacher = row.Cells[2].Value.ToString();
string query = "MERGE TeacherDutyCounts AS target USING (SELECT @Teacher, @DutyPlace, 1 AS DutyCount) AS source (Teacher, DutyPlace, DutyCount)" +
" ON (target.Teacher = source.Teacher AND target.DutyPlace = source.DutyPlace) " +
"WHEN MATCHED THEN UPDATE SET DutyCount = target.DutyCount + source.DutyCount WHEN NOT MATCHED BY TARGET THEN INSERT (Teacher, DutyPlace, DutyCount)";
using (SqlCommand command = new SqlCommand(query, con))
command.Parameters.AddWithValue("@Teacher", teacher);
command.Parameters.AddWithValue("@DutyPlace", place);
command.Parameters.AddWithValue("@DutyCount", 1);
command.ExecuteNonQuery();
string updateQuery = "UPDATE AppConfig SET ConfigValue = @NewValue WHERE ConfigKey = 'extraTeacherIndex'";
using (SqlCommand command = new SqlCommand(updateQuery, con))
command.Parameters.AddWithValue("@NewValue", extraTeacherIndex);
command.ExecuteNonQuery();
using (SqlCommand cmdd = new SqlCommand())
cmdd.CommandText = "UPDATE AppConfig SET ConfigValue = @weekCount WHERE ConfigKey = 'weekCount'";
cmdd.Parameters.AddWithValue("@weekCount", weekCount);
using (SqlCommand cmd = new SqlCommand())
cmd.CommandText = "UPDATE AppConfig SET ConfigValue = @Count WHERE ConfigKey = 'rotationCount'";
cmd.Parameters.AddWithValue("@Count", rotationCount);
public class ReportForm : Form
public DataGridView reportDataGridView;
public DataGridView dutyCountDataGridView;
public Button saveButton;
public Dictionary<string, int> dutyCounts;
public List<string> dutyPlaces;
public List<string> teachers;
public ReportForm(Dictionary<string, int> dutyCountsFromForm1, List<string> dutyPlacesFromForm1, List<string> teachersFromForm1)
this.dutyCounts = dutyCountsFromForm1;
this.dutyPlaces = dutyPlacesFromForm1;
this.teachers = teachersFromForm1;
Size = new Size(800, 600);
reportDataGridView = new DataGridView
reportDataGridView.Columns.Add("Date", "Date");
reportDataGridView.Columns.Add("Day", "Day");
foreach (string place in dutyPlaces)
DataGridViewTextBoxColumn textBoxColumn = new DataGridViewTextBoxColumn();
textBoxColumn.Name = place;
textBoxColumn.HeaderText = place;
reportDataGridView.Columns.Add(textBoxColumn);
reportDataGridView.CellClick += reportDataGridView_CellClick;
dutyCountDataGridView = new DataGridView
dutyCountDataGridView.Columns.Add("Öğretmen", "Öğretmen");
dutyCountDataGridView.Columns.Add("Nöbet Sayısı", "Nöbet Sayısı");
TabControl tabControl = new TabControl
tabControl.TabPages.Add("Nöbet Raporu");
tabControl.TabPages.Add("Nöbet Sayıları");
tabControl.TabPages[0].Controls.Add(reportDataGridView);
tabControl.TabPages[1].Controls.Add(dutyCountDataGridView);
Controls.Add(tabControl);
saveButton.Click += (sender, e) =>
Controls.Add(saveButton);
private void reportDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
if (reportDataGridView.CurrentCell.Value != null && teachers.Contains(reportDataGridView.CurrentCell.Value.ToString()))
ComboBox comboBox = new ComboBox();
comboBox.Items.AddRange(teachers.ToArray());
comboBox.Text = reportDataGridView.CurrentCell.Value.ToString();
comboBox.SelectedValueChanged += (s, ev) =>
reportDataGridView.CurrentCell.Value = comboBox.SelectedValue;
Rectangle rectangle = reportDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
comboBox.Size = rectangle.Size;
comboBox.Location = rectangle.Location;
reportDataGridView.Controls.Add(comboBox);
private void addExcludedDateButton_Click(object sender, EventArgs e)
excludedDatesListBox.Items.Add(excludedDatePicker.Value.ToShortDateString());
private void removeExcludedDateButton_Click(object sender, EventArgs e)
if (excludedDatesListBox.SelectedIndex != -1)
excludedDatesListBox.Items.RemoveAt(excludedDatesListBox.SelectedIndex);
private void removeExraDutyButton_Click(object sender, EventArgs e)
if (extraDutyDatesListBox.SelectedIndex != -1)
extraDutyDatesListBox.Items.RemoveAt(extraDutyDatesListBox.SelectedIndex);
private void addExtraDutyDateButton_Click(object sender, EventArgs e)
extraDutyDatesListBox.Items.Add(extraDutyDatePicker.Value.ToShortDateString());
private void button1_Click(object sender, EventArgs e)
FormMain form2sec = new FormMain();