1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HOW INSERT UPDATE RECORD IN C# WITH SQL DATABASE and COMBOBOX

Discussion in 'C#' started by necko16, Dec 11, 2017.

  1. #1
    I have 2 table in my sql database: 1. city - id - cityname

    1. 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.
    SEMrush
    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 insert new record and edit record frmAddEditStudent:
    public partial class frmAddEditStudent : Form 
    { 
        tblStudentEntities dbIme; 
        CityEntities Citydb; 
        public frmAddEditStudent(tblStudent obj) 
        { 
            InitializeComponent(); 
            dbIme = new tblStudentEntities(); 
            if (obj == null) 
            { 
     
                tblStudentBindingSource.DataSource = new tblStudent(); 
                //Add contact to model, allow insert 
                dbIme.tblStudents.Add(tblStudentBindingSource.Current as tblStudent); 
            } 
            else 
            { 
                tblStudentBindingSource.DataSource = obj; 
                //Attach contact to model, allow edit 
                dbIme.tblStudents.Attach(tblStudentBindingSource.Current as tblStudent); 
            } 
        } 
     
        private void frmAddEditStudent_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            if (DialogResult == DialogResult.OK) 
            { 
                if (string.IsNullOrEmpty(txtStudentName.Text)) 
                { 
                    MessageBox.Show("Please Insert data", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    txtStudentName.Focus(); 
                    e.Cancel = true; 
                    return; 
                } 
                dbIme.SaveChanges(); 
                e.Cancel = false; 
            } 
            e.Cancel = false; 
        } 
     
        private void frmAddEditStudent_Load(object sender, EventArgs e) 
        { 
            Citydb = new CityEntities(); 
            tblCityBindingSource.DataSource = Citydb.tblCitys.ToList(); 
     
     
        } 
     
     
    }  
    Code (markup):
     
    necko16, Dec 11, 2017 IP
    SEMrush
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,607
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You need to give yourself a hook - to the ID that you want to edit. Stuff an id as a hidden column in the gridview to reference which Id to use - for editing.
     
    ccoonen, Feb 13, 2018 IP