Problems reading output parameters

Topics: Data Access Application Block
Mar 4, 2010 at 8:11 PM

I am struggling to get this to work.  I am saving a parent record with 2 child records within a transaction.   When I go to read the output parameters, it fails and I'm not sure why.

 

Here is a sample of my code:

 



        public void Save(ServiceTemplate serviceTemplate)
        {
            ServiceTemplateDurationDO detailDO = new ServiceTemplateDurationDO();

            Database db = getDatabase;
            
            using (DbConnection conn = db.CreateConnection())
            {
                conn.Open();
                DbTransaction transaction = conn.BeginTransaction();

                try
                {
                    Save(serviceTemplate, transaction);

                    foreach (ServiceTemplateDuration detail in serviceTemplate.ServiceTemplateDurations)
                    {
                        detail.ServiceTemplateId = serviceTemplate.Id;

                        if (!detail.IsDeleted)
                        {
                            detailDO.Save(detail, transaction);
                        }
                        else
                        {
                            detailDO.Delete(detail.Id, transaction);
                        }
                    }


                    // Commit the transaction.
                    transaction.Commit();
                }
                catch
                {
                    // Roll back the transaction.
                    transaction.Rollback();
                    throw;
                }
                conn.Close();
            }
        }

        private void Save(ServiceTemplate serviceTemplate, DbTransaction transaction)
        {
            string sql = "ServiceTemplateSave";

            Database db = getDatabase;
            DbCommand cmd = db.GetStoredProcCommand(sql);

            db.AddOutParameter(cmd, "Id", DbType.Int32, serviceTemplate.Id);
            db.AddInParameter(cmd, "SalonId", DbType.Int32, serviceTemplate.SalonId);
            db.AddInParameter(cmd, "StylistId", DbType.Int32, serviceTemplate.SalonId);
            db.AddInParameter(cmd, "ClientId", DbType.Int32, serviceTemplate.ClientId);
            db.AddInParameter(cmd, "ServiceId", DbType.Int32, serviceTemplate.ServiceId);
            db.AddInParameter(cmd, "Cost", DbType.Currency, serviceTemplate.Cost);
            db.AddInParameter(cmd, "Notes", DbType.String, serviceTemplate.Notes);
            db.AddInParameter(cmd, "TimeStamp", DbType.Binary, serviceTemplate.TimeStamp);

            int results = 0;

            if (transaction != null)
            {
                results = db.ExecuteNonQuery(cmd, transaction);
            }
            else
            {
                results = db.ExecuteNonQuery(cmd);
            }


            object _id = db.GetParameterValue(cmd, "Id");
            serviceTemplate.Id = (System.Int32)_id;
            object _timeStamp = db.GetParameterValue(cmd, "TimeStamp");
            serviceTemplate.TimeStamp = (System.Byte[])_timeStamp;
        }

Mar 5, 2010 at 6:07 AM

Hi,

Does it throw an exception? Please see this FAQ item if it helps http://entlib.codeplex.com/wikipage?title=How%20can%20I%20get%20the%20out%20parameter%20value%20using%20the%20Data%20Access%20Application%20Block%3f&referringTitle=EntLib%20FAQ

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com