Problem while adding an entity in nopCommerce 1.9

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
I am trying to play around with new release of nopCommerce. So first of all I try to add an entity which I called 'Album'. But when I try to visit the page from where I can add info to that table, I got the following error:

The current type, NopSolutions.NopCommerce.BusinessLogic.Albums.IAlbumService, is an interface and cannot be constructed. Are you missing a type mapping?

Can anyone please tell me what did I miss?
13 years ago
I am very new in Entity Relationship Framework. So who visit this topic, please help me to figure out what is my problem ...
13 years ago
Does anyone know how to solve this? please help me or give me a link where I find the solution?
13 years ago
I think you need the explain your steps exactly. This way someone can repeat your steps and see what's wrong.

Best is to add screenshots.
13 years ago
Thanx for your suggestion. Ok, I'll give total code and screenshot of my error.
13 years ago
Steps I have taken:
1. Table creation in database:

CREATE TABLE [dbo].[Nop_Album](
  [AlbumID] [int] IDENTITY(1,1) NOT NULL,
  [AlbumName] [nvarchar](100) NOT NULL,
  [AlbumDesc] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_Nop_Album] PRIMARY KEY CLUSTERED
(
  [AlbumID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

2. Add entity to NopModel.edmx and rename Nop_Album to Album. It automatically invoke a security warning ‘Running this text template can potentially harm your computer ’ and bla bla bla for two times. I click ok to run the template.  I also add using NopSolutions.NopCommerce.BusinessLogic.Albums; in NopObjectContext.ObjectSets.cs file.

3. After that I created the following classes under Nop.BusinessLogic in the folder named Albums.

Album.cs
using System.Collections.Generic;

namespace NopSolutions.NopCommerce.BusinessLogic.Albums
{
    public partial class Album : BaseEntity
    {
        #region Properties

        public int AlbumId { get; set; }

        public string AlbumName { get; set; }

        public string AlbumDesc { get; set; }

        #endregion

    }
}

IAlbumService.cs

using System.Collections.Generic;

namespace NopSolutions.NopCommerce.BusinessLogic.Albums
{
    public partial interface IAlbumService
    {
        void DeleteAlbum(int albumId);

        void InsertAlbum(Album album);

        void UpdateAlbum(Album album);

        Album GetAlbumById(int albumId);

        List<Album> GetAllAlbums();


    }
}

AlbumService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Caching;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.BusinessLogic.Albums
{
    public partial class AlbumService : IAlbumService
    {
        #region Fields

        private readonly NopObjectContext _context;

        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        public AlbumService(NopObjectContext context)
        {
            this._context = context;
            this._cacheManager = new NopRequestCache();
        }
        #endregion

        #region Methods

        public void DeleteAlbum(int albumId)
        {
            var album = GetAlbumById(albumId);
            if (albumId == null)
                return;

            if (!_context.IsAttached(album))
                _context.Albums.Attach(album);
            _context.DeleteObject(album);
            _context.SaveChanges();
        }


        public void InsertAlbum(Album album)
        {
            if (album == null)
                throw new ArgumentNullException("album");

            album.AlbumName = CommonHelper.EnsureNotNull(album.AlbumName);
            album.AlbumName = CommonHelper.EnsureMaximumLength(album.AlbumName, 200);
            album.AlbumDesc = CommonHelper.EnsureNotNull(album.AlbumDesc);

            _context.Albums.AddObject(album);
            _context.SaveChanges();
        }

        public void UpdateAlbum(Album album)
        {
            if (album == null)
                throw new ArgumentNullException("album");

            album.AlbumName = CommonHelper.EnsureNotNull(album.AlbumName);
            album.AlbumName = CommonHelper.EnsureMaximumLength(album.AlbumName, 200);
            album.AlbumDesc = CommonHelper.EnsureNotNull(album.AlbumDesc);

            if (!_context.IsAttached(album))
                _context.Albums.Attach(album);

            _context.SaveChanges();
        }

        public Album GetAlbumById(int albumId)
        {
            if (albumId == 0)
                return null;

            var query = from a in _context.Albums
                        where a.AlbumId == albumId
                        select a;
            var album = query.SingleOrDefault();

            return album;
        }

        public List<Album> GetAllAlbums()
        {
            var query = from a in _context.Albums
                        orderby a.AlbumName
                        select a;
            var albums = query.ToList();
            return albums;
        }
        #endregion

    }
}

4. After a successful build I then created some modules under Administration>Modules folder:
First add these lines to BaseNopUserControl.cs file:

using NopSolutions.NopCommerce.BusinessLogic.Albums;

public IAlbumService AlbumService
        {
            get { return IoC.Resolve<IAlbumService>(); }
        }

Albums.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Albums.ascx.cs" Inherits="NopSolutions.NopCommerce.Web.Administration.Modules.AlbumsControl" %>
<%@ Register TagPrefix="nopCommerce" TagName="ToolTipLabel" Src="ToolTipLabelControl.ascx" %>
<div class="section-header">
    <div class="title">
        <img src="Common/ico-content.png" alt="Albums" />
        Albums
    </div>
    <div class="options">
        <input type="button" onclick="location.href='AlbumAdd.aspx'" value="Add New"
            id="btnAddNew" class="adminButtonBlue" title="Add New" />
    </div>
</div>
<p>
</p>
<asp:GridView ID="gvAlbums" runat="server" AutoGenerateColumns="False" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="Album Name" ItemStyle-Width="40%">
            <ItemTemplate>
                <%#Eval("AlbumName")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Album Description" ItemStyle-Width="40%">
            <ItemTemplate>
                <%#Eval("AlbumDesc")%>
            </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center"
            ItemStyle-Width="20%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <a href="AlbumDetails.aspx?AlbumID=<%#Eval("AlbumId")%>">
                    Edit Album</a>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Albums.ascx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic.Albums;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class AlbumsControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            var albums = this.AlbumService.GetAllAlbums();
            gvAlbums.DataSource = albums;
            gvAlbums.DataBind();
        }
    }
}

AlbumInfo.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AlbumInfo.ascx.cs" Inherits="NopSolutions.NopCommerce.Web.Administration.Modules.AlbumInfoControl" %>



<table>
    <tr>
        <td>
            <asp:Label ID="lblName" runat="server" Text="Album Name"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        </td>
    
    </tr>

    <tr>
        <td>
            <asp:Label ID="lblDesc" runat="server" Text="Album Description"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtDesc" runat="server"></asp:TextBox>
        </td>
    </tr>

</table>
AlbumInfo.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Albums;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class AlbumInfoControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            Album album = this.AlbumService.GetAlbumById(this.AlbumId);
            if (album != null)
            {
                txtName.Text = album.AlbumName;
                txtDesc.Text = album.AlbumDesc;
            }
            else
            {
            }
              
        }

        public Album SaveInfo()
        {
            Album album = this.AlbumService.GetAlbumById(this.AlbumId);

            if (album != null)
            {
                album.AlbumName = txtName.Text;
                album.AlbumDesc = txtDesc.Text;
                this.AlbumService.UpdateAlbum(album);
            }
            else
            {
                album = new Album()
                {
                    AlbumName = txtName.Text,
                    AlbumDesc = txtDesc.Text
                };
                this.AlbumService.InsertAlbum(album);
            }

            return album;
        }

        public int AlbumId
        {
            get
            {
                return CommonHelper.QueryStringInt("AlbumId");
            }
        }
    }
}

AlbumAdd.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AlbumAdd.ascx.cs" Inherits="NopSolutions.NopCommerce.Web.Administration.Modules.AlbumAddControl" %>

<%@ Register TagPrefix="nopCommerce" TagName="AlbumInfo" Src="AlbumInfo.ascx" %>

<div>

<div class="options">
        <asp:Button ID="SaveButton" runat="server" CssClass="adminButtonBlue" Text="<% $NopResources:Admin.BlacklistIPAdd.SaveButton.Text %>"
            OnClick="SaveButton_Click" ToolTip="<% $NopResources:Admin.BlacklistIPAdd.SaveButton.Tooltip %>" />
        <asp:Button ID="SaveAndStayButton" runat="server" CssClass="adminButtonBlue" Text="<% $NopResources:Admin.BlacklistIPAdd.SaveAndStayButton.Text %>"
            OnClick="SaveAndStayButton_Click" />
    </div>
<br />

<nopCommerce:AlbumInfo ID="ctrlAlbumlist" runat="server" />

</div>

AlbumAdd.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Albums;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class AlbumAddControl : BaseNopAdministrationUserControl
    {
        protected Album Save()
        {
            Album album = ctrlAlbumlist.SaveInfo();
            return album;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Album album = Save();
                    Response.Redirect("Album.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Album album = Save();
                    Response.Redirect("AlbumDetails.aspx?AlbumID=" + album.AlbumId.ToString());
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
    }
}

AlbumDetails.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AlbumDetails.ascx.cs" Inherits="NopSolutions.NopCommerce.Web.Administration.Modules.AlbumDetailsControl" %>

<%@ Register TagPrefix="nopCommerce" TagName="AlbumInfo" Src="AlbumInfo.ascx" %>
<%@ Register TagPrefix="nopCommerce" TagName="ConfirmationBox" Src="ConfirmationBox.ascx" %>


<div class="section-header">
    <div class="title">
        <img src="Common/ico-blacklist.png" alt="Album List" />
        Album List
        <a href="Album.aspx" title="Back To Album">
            (Back To Album)</a>
    </div>
    <div class="options">
        <asp:Button ID="SaveButton" runat="server" CssClass="adminButtonBlue" Text="<% $NopResources:Admin.BlacklistIPDetails.SaveButton.Text %>"
            OnClick="SaveButton_Click" ToolTip="<% $NopResources:Admin.BlacklistIPDetails.SaveButton.Tooltip %>" />
        <asp:Button ID="SaveAndStayButton" runat="server" CssClass="adminButtonBlue" Text="<% $NopResources:Admin.BlacklistIPDetails.SaveAndStayButton.Text %>"
            OnClick="SaveAndStayButton_Click" />
        <asp:Button ID="DeleteButton" runat="server" CssClass="adminButtonBlue" Text="<% $NopResources:Admin.BlacklistIPDetails.DeleteButton.Text %>"
            OnClick="DeleteButton_Click" CausesValidation="false" ToolTip="<% $NopResources:Admin.BlacklistIPDetails.DeleteButton.Tooltip %>" />
    </div>
</div>
<p>
</p>
<nopCommerce:AlbumInfo ID="ctrlAlbum" runat="server" />
<nopCommerce:ConfirmationBox runat="server" ID="cbDelete" TargetControlID="DeleteButton"
    YesText="<% $NopResources:Admin.Common.Yes %>" NoText="<% $NopResources:Admin.Common.No %>"
    ConfirmText="<% $NopResources:Admin.Common.AreYouSure %>" />

AlbumDetails.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Albums;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class AlbumDetailsControl : BaseNopAdministrationUserControl
    {
        protected Album Save()
        {
            Album album = ctrlAlbum.SaveInfo();
            return album;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Album album = Save();
                    Response.Redirect("Album.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Album album = Save();
                    Response.Redirect("AlbumDetails.aspx?AlbumID=" + album.AlbumId.ToString());
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
                        
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.AlbumService.DeleteAlbum(this.AlbumId);
                Response.Redirect("Album.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        public int AlbumId
        {
            get
            {
                return CommonHelper.QueryStringInt("AlbumId");
            }
        }
    }
}

5. Finally I created correspondence pages, build and run albums.aspx page:

then it tells me :

The current type, NopSolutions.NopCommerce.BusinessLogic.Albums.IAlbumService, is an interface and cannot be constructed. Are you missing a type mapping?
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The current type, NopSolutions.NopCommerce.BusinessLogic.Albums.IAlbumService, is an interface and cannot be constructed. Are you missing a type mapping?

Source Error:

Line 236:        public T Resolve<T>()
Line 237:        {
Line 238:            return _container.Resolve<T>();
Line 239:        }
Line 240:


Source File: D:\Starter Kit\nopCommerce_1.90_Source\Libraries\Nop.BusinessLogic\Infrastructure\UnityDependencyResolver.cs    Line: 238


These are details of my steps. What can I do now? Please help me solving the problem.
13 years ago
Now I gave the details of my problem, but still there is no reply!!!

What else should I do to get help???
13 years ago
This forum is not really active lately with people who have knowledge about this (i don't).

Could you check this link: http://blogs.planetcloud.co.uk/mygreatdiscovery/post/How-to-extend-nopCommerce.aspx

Im trying to find another one too
13 years ago
Thanx ....but the link is about nopCommerce 1.8 . May be 1.9 works slightly different way.... Though I think my code fulfill the requirement talked in the blogpost.
13 years ago
This isn't exactly what you are after but it might help :

In version 1.9, I've added a new field to the db based on Ben Foster's guide (Adding a new property to an existing entity).

Using Ben's example and following his description:

1. add the new field to the database: nop_Category table (Stylesheet, nvchar(50), allow nulls) using NopModel.edmx update the model based on the database

2. add a new class called Category.cs - simply copy and paste the text per Ben's guide or use this:

namespace NopSolutions.NopCommerce.BusinessLogic.Categories  
{  
     /// <SUMMARY>  

     /// Represents a category  

     /// </SUMMARY>  

     public partial class Category : BaseEntity {  

         public string Stylesheet { get; set; }  
     }  
}


3. now the variation to Ben's guide: add the new Stylesheet code to the CategoryInfo.ascx.cs in Admin. by adding these lines no's (assuming you are looking at a default install):

59  this.txtStylesheet.Text = category.Stylesheet;

136  category.Stylesheet = txtStylesheet.Text.Trim();

167  Stylesheet = txtStylesheet.Text.Trim(),

This places Stylesheet between the Name and Description in the code.


4. Now if you stick this code into  CategoryInfo.ascx (should be on lines 36 to 45):

      <tr>
                <td class="adminTitle">
                    <nopCommerce:ToolTipLabel runat="server" ID="ToolTipLabel1" Text="STYLESHEET"
                        ToolTip="<% $NopResources:Admin.CategoryInfo.Name.ToolTip %>" ToolTipImage="~/Administration/Common/ico-help.gif" />
                </td>
                <td class="adminData">
                    <nopCommerce:SimpleTextBox runat="server" CssClass="adminInput" ID="txtStylesheet" ErrorMessage="<% $NopResources:Admin.CategoryInfo.Name.Required %>">
                    </nopCommerce:SimpleTextBox>
                </td>
            </tr>

5. Now compile and run you should see Stylesheet as a new field after CategoryName in the Admin/CategoryInfo screen - the tool tip above is a text version but you'd add a resource string normally.

I've not tested this fully yet, but this seems to be working OK so far. It seems the EF in v1.9 nopcom is making extending even easier - unless of course I've missed something!

Adding a new entity, which is what you're after, I am about to attempt, but it may take longer but this should point you in the right direction - with v1.9 not using the manager method it'll take a bit more reading to see how to extend v1.9 correctly.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.