public Dictionary <FacDep, List <FacDep> > GetFacultyAndDepartments() { DataTable table = new DataTable(); string query = "SELECT Faculty_id, Faculty_name, D.Id as Department_id, Department_name " + "FROM Faculty as F, Department as D " + "WHERE D.Faculty_id = F.Id"; SqlDataAdapter adp = new SqlDataAdapter(query, conn); conn.Open(); adp.Fill(table); conn.Close(); Dictionary <FacDep, List <FacDep> > list = new Dictionary <FacDep, List <FacDep> >(); foreach (DataRow row in table.Rows) { FacDep fac = new FacDep(); fac.id = int.Parse(row["Faculty_id"].ToString()); fac.name = row["Faculty_name"].ToString(); FacDep dep = new FacDep(); dep.id = int.Parse(row["Department_id"].ToString()); dep.name = row["Department_name"].ToString(); if (!list.ContainsKey(fac)) { list.Add(fac, new List <FacDep>()); } list[fac].Add(dep); } return(list); }
/// <summary> /// This constructor will populate form fields with given academician's /// data. Use empty constructor when adding new academician. /// </summary> /// <param name="id">Id of the academician to load</param> public ProfileEditForm(Academician _ac) { InitializeComponent(); rtfTools.Renderer = new CustomRenderer(); //for styling purposes alertBox.BringToFront(); db = new DbHelper(); list = db.GetFacultyAndDepartments(); foreach (FacDep fac in list.Keys) { facultyCB.Items.Add(fac); } this.ac = _ac; if (ac != null) { // we will populate form with given academician data deleteButton.Visible = true; nameTB.Text = ac.Name; mailTB.Text = ac.Mail; phoneTB.Text = ac.Phone; websiteTB.Text = ac.Website; //select faculty from faculty list with LINQ FacDep fac = list.Keys.Single(f => f.id == ac.Faculty_id); facultyCB.SelectedIndex = facultyCB.Items.IndexOf(fac); //select department from department list with LINQ FacDep dep = list[fac].Single(d => d.id == ac.Deparment_id); departmentCB.SelectedIndex = departmentCB.Items.IndexOf(dep); if (ac.Detail_RTF.StartsWith(@"{\rtf")) { detailRichTB.Rtf = ac.Detail_RTF; } } }
private void saveButton_Click(object sender, EventArgs e) { if (nameTB.Text.Length < 3) { ShowError("Ad soyad en az 3 karakter olmalı."); return; } if (!mailTB.Text.Contains("@") || !mailTB.Text.Contains(".") || mailTB.Text.Length < 5) { ShowError("Geçersiz eposta adresi"); return; } if (System.Text.RegularExpressions.Regex.IsMatch(phoneTB.Text, @"[a-zA-Z]") || System.Text.RegularExpressions.Regex.IsMatch(phoneTB.Text, @"[^\w+-]") || (phoneTB.Text.Length > 1 && phoneTB.Text.Length < 7)) { ShowError("Geçersiz telefon numarası"); return; } if (facultyCB.SelectedIndex == -1) { ShowError("Bir fakülte seçmediniz."); return; } if (departmentCB.SelectedIndex == -1) { ShowError("Bir bölüm seçmediniz."); return; } FacDep fac = (FacDep)facultyCB.SelectedItem; FacDep dep = (FacDep)departmentCB.SelectedItem; if (ac == null) { //Its new academician, lets create it and insert it to db ac = new Academician(-1, nameTB.Text, fac.id, fac.name, dep.id, dep.name, mailTB.Text, phoneTB.Text, websiteTB.Text, detailRichTB.Rtf); int insertedId = db.InsertAcademician(ac); if (insertedId != -1) { //inserted to db successfully, redirect to profile page ((MainForm)MdiParent).ShowProfileForm(insertedId); } else { ShowError("Yeni kayıt eklenirken bir hata oluştu."); } } else { //create new academician with updated fields Academician _ac = new Academician(ac.Id, nameTB.Text, fac.id, fac.name, dep.id, dep.name, mailTB.Text, phoneTB.Text, websiteTB.Text, detailRichTB.Rtf); bool successful = db.UpdateAcademician(_ac); if (successful) { ((MainForm)MdiParent).ShowProfileForm(_ac.Id); } else { ShowError("Kayıt güncellenirken bir hata oluştu."); } } }