Showing posts with label Windows Forms. Show all posts
Showing posts with label Windows Forms. Show all posts

Tuesday, January 3, 2012

Start using .Net Entity Framework - 3


This article illustrates the usage of ADO.Net Entity Framework in creating, updating and deleting data records.

This article comes after:
    - Start using .Net Entity Framework - 1 , and
    - Start using .Net Entity Framework - 2

where we saw how to create a data model (ADO.Net Entity Data Model) from a pre-existing database, and how to use that model to query the database and fill a comboBox, by using a small example. The example database has only one table (Pais) with countries.

Taking the same example, we added a label to Form1, which will enable the visualization of each record's ID, and a text box that enables the user to modify the name of a country or create a new country in the database.



Buttons have also been added to Create New, Update and Delete a country in the table, and a Cancel button that closes the application.

To start, we add, in the method that handles the selection of a new line in the comboBox, the code to  fill the label (label1) with the country ID, and the text box (paisTextBox) with the name (nome) of the country. This allows the user to edit the current name of a country, in order to modify it, or to insert a new country's name, in order to create it in the database.

        private void paisesBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = paisesBox1.SelectedValue.ToString();
           
            paisAtual = (Pais)paisesBox1.SelectedItem;
            paisTextBox.Text = paisAtual.nome;
        }

Thursday, December 29, 2011

Start using .Net Entity Framework - 2

After having created the Windows Forms Application project and generated the data model from the database let's see how we can access the database through the model, using a small illustrative example.

In the Form1 form, created automatically when creating the Windows Forms Application project, we will create a comboBox, dragging it from the toolbox.


In the Properties tab we change its name to paisesBox1.