Page 1 of 1

Can not found Inserted Record?

Posted: Wed 06 Jun 2012 10:11
by ying515_huang
We inserted record when it is not SubmitChanges().
user want modify inserted record. but it is not found.

Code: Select all

        public void InsertLines(SmpWipmmtLinesVMeta data)
        {
            try
            {
                DateTime? db_date = db.GetDbSysDate();
                data.WmLineId = Convert.ToDecimal(db.GetWipmmtLinesNextval());
                data.WmHeaderId = this.hd_id;
                SmpWipmmtLinesTp dls = new SmpWipmmtLinesTp();
                copy_data(data, dls);
                db.SmpWipmmtLinesTps.InsertOnSubmit(dls);
                decimal i = 0;
                SmpWipmmtLinesTp target = db.SmpWipmmtLinesTps.Where(p => p.WmLineId == data.WmLineId).FirstOrDefault();
                if (target != null)  <-- why it is always null
                {
                    i = target.WmLineId;
                }
            }
SmpWipmmtLinesTp is database view!

ASP.NET MVC 2.0 & Visual 2010 & framework 4.0
Oracle DB 11
dotConnect Ver 6.10.111.0

Re: Can not found Inserted Record?

Posted: Thu 07 Jun 2012 13:34
by StanislavK
When the InsertOnSubmit method is invoked, no actual interoperations with the database are performed. The new entity is saved to the database only when SubmitChanges is called. Thus, the new row is not available in the database yet when you execute the query.

On the other hand, the query is translated into SQL and executed at the server. As the server returns an empty result set, the query result is null.

Thus, the 'target' object should not be null only if you invoke SubmitChanges before querying.

Please tell us if anything is unclear.