none
Updating database problem RRS feed

  • Question

  • I have now run out of ideas as to what I am doing wrong. I am filling a combobox with a list of categories from the database. I have a textbox that I use to show the value of the combobox to delete, modify or even create a new category. Here is the code for the modify button:
            private void btnModifyCategory_Click( object sender, EventArgs e )
            {
                using ( MaterialEntities me = new MaterialEntities() )
                {
                    string selectedCategory = cboAdminCategories.SelectedItem.ToString();
                    string modifiedSelectedCategory = txtCategoryToChange.Text;
    
                    var category = ( from c in me.Categories
                                     where c.categoryName == selectedCategory
                                     select c.categoryName ).FirstOrDefault();
    
                    if ( category != null )
                    {                    
                        DialogResult result = MessageBox.Show( "Are you sure you want to modify \"" + selectedCategory + "\" to \"" + modifiedSelectedCategory + "\"", "Modify a category",
                            MessageBoxButtons.OKCancel, MessageBoxIcon.Question );
                        if ( result == DialogResult.OK )
                        {
                            category = modifiedSelectedCategory;
                            me.SaveChanges();
                            cboAdminCategories.Refresh();
                            //cboAdminCategories.Items.Clear();
                            //FillComboWithCategories( cboAdminCategories );
                        }
                    }
                }
            }
    

    The database property is set to "Copy if newer" and I even use SSMS express to look at the database in the debug bin folder.

    In debug mode all seems fine but nothing seems to happen. I have tried delete and create, same problem. What am I doing wrong?

    Thanks for any guidance. 


    Still trying to program
    Monday, October 3, 2011 4:38 PM

Answers

  • Hi Jonsey;

    In order for Entity Framework to track entities in your application it needs an entity returned back from the database. When you return only one column from a table it does not return an entity of that table type but a anonymous type that is not tracked and is not updateable. in order for your code to work make the following changes:

    private void btnModifyCategory_Click( object sender, EventArgs e )
    {
        using ( MaterialEntities me = new MaterialEntities() )
        {
            string selectedCategory = cboAdminCategories.SelectedItem.ToString();
            string modifiedSelectedCategory = txtCategoryToChange.Text;
    
            var category = ( from c in me.Categories
                             where c.categoryName == selectedCategory
                             select c ).FirstOrDefault();
    
            if ( category != null )
            {                    
                DialogResult result = MessageBox.Show( "Are you sure you want to modify \"" + selectedCategory + "\" to \"" + modifiedSelectedCategory + "\"", "Modify a category", MessageBoxButtons.OKCancel, MessageBoxIcon.Question );
                
                if ( result == DialogResult.OK )
                {
                    category.categoryName = modifiedSelectedCategory;
                    me.SaveChanges();
                    cboAdminCategories.Refresh();
                    //cboAdminCategories.Items.Clear();
                    //FillComboWithCategories( cboAdminCategories );
                }
            }
        }
    }
    
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Kris444 Tuesday, October 4, 2011 3:00 AM
    • Marked as answer by Jonsey Tuesday, October 4, 2011 1:55 PM
    Monday, October 3, 2011 5:54 PM

All replies

  • Jonsey,

    Where are you actually modifying the category entity?  I don't see that happening within the scope of your MaterialEntities context (within the scope of your using statement in other words).  I think that's the problem, me.SaveChanges() doesn't have anything to save, because you haven't actually made a change to a category entity anywhere within your btnModifyCategory_Click event and therefore nothing gets perisisted back to the database.   "category = modifiedSelectedCategory" doesn't do anything, that's only setting a string equal to another string.  
     


    Tom Overton
    • Proposed as answer by Kris444 Tuesday, October 4, 2011 3:00 AM
    Monday, October 3, 2011 4:56 PM
  • Hi Jonsey;

    In order for Entity Framework to track entities in your application it needs an entity returned back from the database. When you return only one column from a table it does not return an entity of that table type but a anonymous type that is not tracked and is not updateable. in order for your code to work make the following changes:

    private void btnModifyCategory_Click( object sender, EventArgs e )
    {
        using ( MaterialEntities me = new MaterialEntities() )
        {
            string selectedCategory = cboAdminCategories.SelectedItem.ToString();
            string modifiedSelectedCategory = txtCategoryToChange.Text;
    
            var category = ( from c in me.Categories
                             where c.categoryName == selectedCategory
                             select c ).FirstOrDefault();
    
            if ( category != null )
            {                    
                DialogResult result = MessageBox.Show( "Are you sure you want to modify \"" + selectedCategory + "\" to \"" + modifiedSelectedCategory + "\"", "Modify a category", MessageBoxButtons.OKCancel, MessageBoxIcon.Question );
                
                if ( result == DialogResult.OK )
                {
                    category.categoryName = modifiedSelectedCategory;
                    me.SaveChanges();
                    cboAdminCategories.Refresh();
                    //cboAdminCategories.Items.Clear();
                    //FillComboWithCategories( cboAdminCategories );
                }
            }
        }
    }
    
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Kris444 Tuesday, October 4, 2011 3:00 AM
    • Marked as answer by Jonsey Tuesday, October 4, 2011 1:55 PM
    Monday, October 3, 2011 5:54 PM
  • Thanks Tom and Fernando,

    I understand now why it wasn't working. Returning only one column from the table rather than the table itself. I can now carry on :-)

    Many thanks again


    :-( Still trying to program
    Tuesday, October 4, 2011 1:59 PM
  •  

    Not a problem, glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, October 4, 2011 3:42 PM