Nedzad Mesic

Nedzad Mesic

  • NA
  • 13
  • 560

How insert update record in c# with sql database and combobo

Dec 11 2017 8:50 AM
I have 2 table in my sql database:
 
1. city - id - cityname
2.Student -id -studentname -cityID
 
I successfully insert, edit and delete data in the database.
 
I have two c# windows form for City:
 
1.frmCity with dataGridView and all City data.... 2.frmAddEditCity with textbox for insert and edit....
 
I have two c# windows form for Student:
 
1.frmCity with dataGridView and all Studentdata.... 2.frmAddEditStudent with textbox for insert and edit...
 
BUT.....
 
In form frmAddEditStudent FOR CityID I have textbox and I use DataBindigs (BindigSource property) to get data... I can disply CITY id or City name...
 
My problem is when I want to insert new record or edit some record... and I don't know how to use combobox instead textbox "txtCityID" and save and edit record.
 
If I edit some record in combobox should be selected city name from that Student record(new york) with the possibility of changing through combox (and in combo are all citys from database)
 
is the same with insert - i need combobox with city names and value(id), and chois city from combo to save record...
 
This is my code for inser new record and edit record frmAddEditStudent:
  1. public partial class frmAddEditStudent : Form    
  2. {  
  3.     tblStudentEntities dbIme;  
  4.     CityEntities Citydb;    
  5.     public frmAddEditStudent(tblStudent obj)     
  6.     {   
  7.         InitializeComponent();  
  8.         dbIme = new tblStudentEntities();  
  9.         if (obj == null)  
  10.         {  
  11.             tblStudentBindingSource.DataSource = new tblStudent();  
  12.             //Add contact to model, allow insert  
  13.             dbIme.tblStudents.Add(tblStudentBindingSource.Current as tblStudent);  
  14.        
  15.         }  
  16.         else  
  17.         {  
  18.             tblStudentBindingSource.DataSource = obj;  
  19.             //Attach contact to model, allow edit  
  20.             dbIme.tblStudents.Attach(tblStudentBindingSource.Current as tblStudent);  
  21.         }  
  22.     }  
  23.     private void frmAddEditStudent_FormClosing(object sender, FormClosingEventArgs e)  
  24.     {  
  25.         if (DialogResult == DialogResult.OK)  
  26.         {  
  27.             if (string.IsNullOrEmpty(txtStudentName.Text))  
  28.             {  
  29.                 MessageBox.Show("Please Insert data""Message", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  30.                 txtStudentName.Focus();  
  31.                 e.Cancel = true;  
  32.                 return;  
  33.             }  
  34.             dbIme.SaveChanges();  
  35.             e.Cancel = false;  
  36.         }  
  37.         e.Cancel = false;  
  38.     }  
  39.     private void frmAddEditStudent_Load(object sender, EventArgs e)   
  40.     {  
  41.         Citydb = new CityEntities();  
  42.         tblCityBindingSource.DataSource = Citydb.tblCitys.ToList();  
  43.     }  
  44. }

Answers (2)