Modifying the application to properly handle unknown values

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi all

I have created new fields in the Db but I get hit with error when trying to display my data,

The 'PacksPerLayer' property on 'Product' could not be set to a 'null' value. You must set this property to a non-null value of type 'decimal'. When bringing across my data from my local db the field PacksPerLayer does allow the NULL property

I would like to change the application to allow nulls and modify the application to properly handle unknown values. Is this at all possible?

All help highly regarded

Richard
12 years ago
Try changing your domain class to use decimal? (nullable decimal) for the PackPerLayer field.


public virtual decimal? PackersPerLayer { get; set; }


Something you will notice later is that any other time you reference PackersPerLayer, you might need to check PackersPerLayer.HasValue  (bool) to see if it is null or not.  The actual value will be in PacksPerLayer.Value.
12 years ago
Hi

I have but I'm currently not able to insert my data across I dropped the data to make certain change's and added a default constraint '0'

Here's the code
USE [Nop]
GO

ALTER TABLE [dbo].[Product] ADD  CONSTRAINT [CK_Product_LayersPerPallet]  DEFAULT ('0') FOR [LayersPerPallet]
GO


This is the constraint I tried to Insert from my db to the Nop db but I'm getting hit with this error -

Error converting data type varchar to numeric. So my feilds aint currently populating any Idea. Also with this snippet of code where would it be placed.

PackersPerLayer.HasValue  (bool) 
12 years ago
You're trying to put a string into a number.  Please post the code where you are having problems.
12 years ago
I meant to type PacksPerLayer.  I would ignore that part for now until you get some errors from it.
12 years ago
This the following error I receive.

The 'WholesalePrice' property on 'Product' could not be set to a 'String' value. You must set this property to a non-null value of type 'Decimal'.

Line 144:                    var reader = cmd.ExecuteReader();
Line 145:                    //return reader.DataReaderToObjectList<TEntity>();
Line 146:                    var result = context.Translate<TEntity>(reader).ToList();
Line 147:                    for (int i = 0; i < result.Count; i++)
Line 148:                        result[i] = AttachEntityToContext(result[i]);

When accessing my products.
12 years ago
Where do you define WholesalePrice?  Is that declared as a string or a decimal?
12 years ago
NOP CORE DOMIAN Product.cs

/// <summary>
/// Gets or sets the WholesalePrice
/// </summary>
public virtual string WholesalePrice { get; set; }

Its displaying. Good
12 years ago
You might have gone the wrong direction with it, though.  I would guess that you would want a Price column to be decimal.  It sounds like it was a decimal in the code but a varchar in the database.  I think you only needed to change the database to decimal (18,2)
12 years ago
Yes you are right thanks and Job done.

On a note I have created a new table WholeCCC IN Core Domain I have written the following class.

using System;
using System.Collections.Generic;
using Nop.Core.Domain.Localization;

namespace Nop.Core.Domain.Catalog
{
    public class WholeCCC : BaseEntity, ILocalizedEntity
    {
        private ICollection<ProductCategory> _productCategories;
        private ICollection<ProductPicture> _productPictures;

        /// <summary>
        /// Gets or sets the StockCode
        /// </summary>
        public virtual string StockCode { get; set; }

        /// <summary>
        /// Gets or sets the Description
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// Gets or sets the Pack
        /// </summary>
        public virtual string Pack { get; set; }

        /// <summary>
        /// Gets or sets the ANALCODE
        /// </summary>
        public virtual string ANALCODE { get; set; }

        /// <summary>
        /// Gets or sets a WholesalePrice
        /// </summary>
        public virtual string WholesalePrice { get; set; }

        /// <summary>
        /// Gets or sets RetailPrice
        /// </summary>
        public virtual string RetailPrice { get; set; }

        /// <summary>
        /// Gets or sets the MRP
        /// </summary>
        public virtual string MRP { get; set; }

        /// <summary>
        /// Gets or sets the SourceBarcode
        /// </summary>
        public virtual string SourceBarcode { get; set; }

        /// <summary>
        /// Gets or sets the RetailBarcode
        /// </summary>
        public virtual string RetailBarcode { get; set; }

        /// <summary>
        /// Gets or sets the ShopPrice
        /// </summary>
        public virtual string ShopPrice { get; set; }

        /// <summary>
        /// Gets or sets QTYPACKSPERLAYER
        /// </summary>
        public virtual decimal QTYPACKSPERLAYER { get; set; }

        /// <summary>
        /// Gets or sets the QTYLAYERSPERPALLET
        /// </summary>
        public virtual decimal QTYLAYERSPERPALLET { get; set; }

        /// <summary>
        /// Gets or sets the PACKTOTALPERPALLET
        /// </summary>
        public virtual decimal PACKTOTALPERPALLET { get; set; }

        /// <summary>
        /// Gets or sets the collection of ProductCategory
        /// </summary>
        public virtual ICollection<ProductCategory> ProductCategories
        {
            get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); }
            protected set { _productCategories = value; }
        }

        /// <summary>
        /// Gets or sets the collection of ProductPicture
        /// </summary>
        public virtual ICollection<ProductPicture> ProductPictures
        {
            get { return _productPictures ?? (_productPictures = new List<ProductPicture>()); }
            protected set { _productPictures = value; }
        }

        
    }

}

Is there a possible way of me now mapping to the Category Table with the field I created [ANAL CODE] In the category table. I started generating the following code in the Data Core. But I'm hit with the error Method must have a return action that area I have made bold.

using System.Data.Entity.ModelConfiguration;
using Nop.Core.Domain.Catalog;

namespace Nop.Data.Mapping.Catalog
{
    public partial class WholeCCCMap : EntityTypeConfiguration<WholeCCC>
    {
       public WholeCCC()
        {
            this.ToTable("WholeCCC");
            this.Property(p => p.Description).IsRequired().HasMaxLength(400);
            this.Property(p => p.Pack).IsMaxLength();
            this.Property(p => p.ANALCODE).IsMaxLength();
            this.Property(p => p.WholesalePrice).IsMaxLength();
            this.Property(p => p.RetailPrice).HasMaxLength(400);
            this.Property(p => p.MRP);
            this.Property(p => p.SourceBarcode).HasMaxLength(400);
            this.Property(p => p.RetailBarcode).HasMaxLength(200);
            this.Property(p => p.ShopPrice);
            this.Property(p => p.QTYPACKSPERLAYER).HasPrecision(18, 1);
            this.Property(p => p.QTYLAYERSPERPALLET).HasPrecision(18, 1);
            this.Property(p => p.PACKTOTALPERPALLET).HasPrecision(18, 1);
        }
    }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.