locked
Issue in using objectcontext to savechanges in different methods RRS feed

  • Question

  • Hi I am new to EF. I developed an import export system using EF. I have problem to save the data using savechanges IF I used in the differnt method even i have define the objectcontext as private or public to the current class. below is what i am trying to do and what i have done so far. i feel that the objectcontext is cleared after loading the page.... any suggestion to fix please. really apprecicate your time and advices. Milan

    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Objects;
    using System.Globalization;
    using System.Data.EntityClient;
    using System.Data.SqlClient;
    using System.Data.Common;
    using SP.DAL;
    using System.Diagnostics;
    using System.Web.UI.WebControls;

    namespace SP
    {
        public partial class editItems : System.Web.UI.Page
        {
            //i did use private, it did not work so i tried public, it did not work either

            public SP.DAL.AdventureWorks2012Entities context = new SP.DAL.AdventureWorks2012Entities();
            public SP.DAL.aa_orderitems orderitemsds = new SP.DAL.aa_orderitems();
            public List<SP.DAL.aa_products> prodlist = new List<SP.DAL.aa_products>();
            public  SP.DAL.aa_orders custinfo = new SP.DAL.aa_orders();


            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack != true)
                {
                    getOrderItems();
                }
            }

            protected void getOrderItems()
            {

                // i thought using will clear the objectcontext so i commented out for testing purpose, but still did not save   //the data
         //using (context)
                //{
                //  
                    var itemkey = Convert.ToInt32(Request.QueryString["itemid"]);
                    var ordernum = Convert.ToInt32(Request.QueryString["orderid"]);


                    orderitemsds = (from i in context.aa_orderitems.Include("aa_products")
                                    where i.itemid == itemkey
                                    select i).FirstOrDefault();

                    custinfo = (from c in context.aa_orders.Include("aa_customers")
                                where c.orderid == ordernum
                                select c).FirstOrDefault();

               
                    prodlist = (from p in context.aa_products
                                orderby p.prodname
                                select p).ToList();
                   
      //load data to screen
                    customertxt.Text = custinfo.aa_customers.last + ", " + custinfo.aa_customers.first;
                    emailTxt.Text = custinfo.aa_customers.email;
                    emailTxt.Text = custinfo.aa_customers.phone;
                    this.orderIdTxt.Text = orderitemsds.orderid.ToString();
                    this.orderDateTxt.Text = orderitemsds.dateadded.ToString();
                    this.prodNameTxt.Text = orderitemsds.aa_products.prodname.ToString();
                    this.qtyTxt.Text = orderitemsds.qty.ToString();

                    this.ddlprodname.DataSource = prodlist;
                    this.ddlprodname.DataBind();
                    ddlprodname.SelectedValue = orderitemsds.aa_products.prodid.ToString();

    // this savechanges works. it is before the page finish loading. but it did not work in the btnSave_Click() below
      try
                    {
                        orderitemsds.qty = 9; //  Convert.ToInt32(this.qtyTxt.Text);
                        orderitemsds.prodid = Convert.ToInt32(this.ddlprodname.SelectedValue);

                        //var iRow = context.SaveChanges();
                        //Debug.WriteLine("saved " + iRow.ToString());
                    }
                    catch (UpdateException ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }

                
            }

    // this did not work, i tried many ways and could not find what is the issue? PLEASE Help if you see the issue. Thanks

            protected void btnSave_Click(object sender, EventArgs e)
            {
                //using (context)
                //{
                try
                    {
                        orderitemsds.qty = Convert.ToInt32(this.qtyTxt.Text);
                        orderitemsds.prodid = Convert.ToInt32(this.ddlprodname.SelectedValue);

                        var iRow = context.SaveChanges();
                        Debug.WriteLine("saved " + iRow.ToString());
                    }
                    catch (UpdateException ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                //}

            }

        }


    }

    Monday, March 18, 2013 5:57 AM

Answers

  • Hi Milan;

    Please see the code snippet below and post the values for orderitemsds at that point.

    Also because your code depends on the data context to be around in multiple places in the page do NOT use a using block to contain the query and results because at the end of the using block the data context will be disposed of and that means that it is not available to other parts of the page.

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            //  PLACE A BREAKPOINT ON THE NEXT LINE TELL ME WHAT DOES orderitemsds HAVE AS VALUES
            orderitemsds.qty = Convert.ToInt32(this.qtyTxt.Text);
            orderitemsds.prodid = Convert.ToInt32(this.ddlprodname.SelectedValue);
    
            var iRow = context.SaveChanges();
            Debug.WriteLine("saved " + iRow.ToString());
        }
        catch (UpdateException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 20, 2013 7:39 PM
  • Hi Milan;

    It is difficult to say what is going on with the information given. If you can zip the project up with a test database and post it to your SkyDrive I will have a look at it to see if I can figure out what is going on. If you can not do that is can put together a test project that exhibits the same behavior I will have a look at that.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, March 21, 2013 3:05 AM

All replies

  • Hi Milan;

    Please post the exception you are getting as well as the inner exception so we can tell what the system states that is wrong.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, March 18, 2013 1:22 PM
  • Ok i will make sure i include the exeption. in my case now, there is no exception. the saving is ok if do it before the page loading. after that the btnsave event did not save the data. I think it is state issue. i will have to research more or anyone know the answer pls let me know. thank you. M.

    Monday, March 18, 2013 4:59 PM
  • Hi Fernando I still look for solution for this posted issue. any advices or know a good place to post this question. please let me know if you know. thannks
    Wednesday, March 20, 2013 5:16 PM
  • Hi Milan;

    Please see the code snippet below and post the values for orderitemsds at that point.

    Also because your code depends on the data context to be around in multiple places in the page do NOT use a using block to contain the query and results because at the end of the using block the data context will be disposed of and that means that it is not available to other parts of the page.

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            //  PLACE A BREAKPOINT ON THE NEXT LINE TELL ME WHAT DOES orderitemsds HAVE AS VALUES
            orderitemsds.qty = Convert.ToInt32(this.qtyTxt.Text);
            orderitemsds.prodid = Convert.ToInt32(this.ddlprodname.SelectedValue);
    
            var iRow = context.SaveChanges();
            Debug.WriteLine("saved " + iRow.ToString());
        }
        catch (UpdateException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 20, 2013 7:39 PM
  • Hi Fernando, yes i realize that if i use USING block, it will disable the the data context. that is why commented out for now. the VALUES orderitemsds will be the value i entered from the item data entry screen, for example, if select prodid = 5 (cup), and qty = 10, then i break point after these two lines, i did see the values assinged to orderitemsds correctly. However, after i executed the savechanges, the return affected row is still 0 and nothing is saved

      orderitemsds.qty = Convert.ToInt32(this.qtyTxt.Text);se
      orderitemsds
    .prodid = Convert.ToInt32(this.ddlprodname.SelectedValue);

    i have done a lot research still have not find the problem and fix it. any ideas? thanks



    Wednesday, March 20, 2013 10:45 PM
  • Hi Milan;

    It is difficult to say what is going on with the information given. If you can zip the project up with a test database and post it to your SkyDrive I will have a look at it to see if I can figure out what is going on. If you can not do that is can put together a test project that exhibits the same behavior I will have a look at that.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, March 21, 2013 3:05 AM