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;
        }

Update the Name of a Country:

We create a clich handler for button Update:

private void UpdateBut_Click(object sender, EventArgs e)
{
   paisAtual.nome = paisTextBox.Text;
   int rowsAffected = dbContext.SaveChanges();
   MessageBox.Show("Updated " + rowsAffected.ToString() + " table records.");
}

Create a New Country:

We create a click handler for button Create:

private void CreateBut_Click(object sender, EventArgs e)
{
    Pais novoPais = new Pais();
    novoPais.nome = paisTextBox.Text;

    dbContext.AddToPaises(novoPais);
    int rowsAffected = dbContext.SaveChanges();
    paisAtual = novoPais;

    MessageBox.Show("Created " + rowsAffected.ToString() + " new record(s) in the table.");

    dbContext = new simpleEntityFrameworkApp.bdExemploEntities();

    paisesBox1.DataSource = dbContext.Paises;
    paisesBox1.DisplayMember = "nome";

    paisesBox1.Invalidate();
}

Delete a Country:

We create a click handler for button Delete:

private void delButton_Click(object sender, EventArgs e)
{
    dbContext.DeleteObject(paisAtual);
    int rowsAffected = dbContext.SaveChanges();
    MessageBox.Show("Deleted " + rowsAffected.ToString() + " table records.");

    label1.Text = paisesBox1.SelectedValue.ToString();
    paisAtual = (Pais)paisesBox1.SelectedItem;
    paisTextBox.Text = paisAtual.nome;
}

Finally, the click handler of the Cancel button only disposes of the database mapping context and closes the form.
        private void Cancelbotao_Click(object sender, EventArgs e)
        {
            dbContext.Dispose();
            this.Close();
        }


Once again, whoever is interested in obtaining the code with the example, please ask for it in a comment to this post.

No comments:

Post a Comment