New database table error. ver 3.5

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

I've tried adding a new sql table and am now an error. Please see detailed steps taken with code.

SQL Table


USE [nopdb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[CustomerExtended](
  [Id] [bigint] IDENTITY(1,1) NOT NULL,
  [BankName] [varbinary](250) NULL,
  [BankBranch] [varbinary](250) NULL,
  [BankBranchCode] [varbinary](50) NULL,
  [BankAccountHolderName] [varbinary](250) NULL,
  [BankAccNo] [varbinary](50) NULL,
  [TradeRef1CompanyName] [varbinary](250) NULL,
  [TradeRef1ContactPerson] [varbinary](250) NULL,
  [TradeRef1Telephone] [varbinary](50) NULL,
  [TradeRef2CompanyName] [varbinary](250) NULL,
  [TradeRef2ContactPerson] [varbinary](250) NULL,
  [TradeRef2Telephone] [varbinary](50) NULL,
  [TradeRef3CompanyName] [varbinary](250) NULL,
  [TradeRef3ContactPerson] [varbinary](250) NULL,
  [TradeRef3Telephone] [varbinary](50) NULL,
  [CustomerId] [int] NULL,
CONSTRAINT [PK_CustomerExtended] PRIMARY KEY CLUSTERED
(
  [Id] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[CustomerExtended]  WITH CHECK ADD  CONSTRAINT [FK_CustomerExtended_Customer] FOREIGN KEY([CustomerId])
REFERENCES [dbo].[Customer] ([Id])
GO
ALTER TABLE [dbo].[CustomerExtended] CHECK CONSTRAINT [FK_CustomerExtended_Customer]



CustomerExtended.cs class in Nop.Code.Domain.Customers


using System;
using Nop.Core.Domain.Directory;

namespace Nop.Core.Domain.Customers
{
    public class CustomerExtended : BaseEntity
    {
        public string BankName { get; set; }
        public string BankBranch { get; set; }
        public string BankBranchCode { get; set; }
        public string BankAccountHolderName { get; set; }
        public string BankAccNo { get; set; }

        public string TradeRef1CompanyName { get; set; }
        public string TradeRef1ContactPerson { get; set; }
        public string TradeRef1Telephone { get; set; }

        public string TradeRef2CompanyName { get; set; }
        public string TradeRef2ContactPerson { get; set; }
        public string TradeRef2Telephone { get; set; }

        public string TradeRef3CompanyName { get; set; }
        public string TradeRef3ContactPerson { get; set; }
        public string TradeRef3Telephone { get; set; }

        public int CustomerId { get; set; }

    }
}



CustomerExtendedMap.cs in Nop.Code.Data.Mapping.Customers


using Nop.Core.Domain.Customers;

namespace Nop.Data.Mapping.Customers
{
    public partial class CustomerExtendedMap : NopEntityTypeConfiguration<CustomerExtended>
    {
        public CustomerExtendedMap()
        {
            this.ToTable("CustomerExtended");
            this.HasKey(a => a.Id);
            
        }
    }
}


Insert method in CustomerService.cs class


        public virtual void InsertCustomerExtended(CustomerExtended customerExtended)
        {
            if (customerExtended == null)
                throw new ArgumentNullException("customer extended");

            _customerExtendedRepository.Insert(customerExtended);

            //event notification
            _eventPublisher.EntityInserted(customerExtended);
        }


In the CustomerService.cs class, I've added the following

//Fields - pasted the lines concerned with this update
private readonly IRepository<CustomerExtended> _customerExtendedRepository;


//Ctor  - only pasted the lines concerned with this update


public CustomerService(
            IRepository<CustomerExtended> customerExtendedRepository,)
        {
            this._customerExtendedRepository = customerExtendedRepository;

        }



The Error


System.NullReferenceException: Object reference not set to an instance of an object.
   at Nop.Services.Customers.CustomerService.InsertCustomerExtended(CustomerExtended customerExtended) in d:\Websites\3. eCommerce\mass\Libraries\Nop.Services\Customers\CustomerService.cs:line 925
   at Nop.Web.Controllers.CustomerController.Register(FormCollection form) in d:\Websites\3. eCommerce\mass\Presentation\Nop.Web\Controllers\CustomerController.cs:line 1830System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Implicit conversion from data type nvarchar(max) to varbinary is not allowed. Use the CONVERT function to run this query.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
   at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at Nop.Data.EfRepository`1.Insert(T entity) in d:\Websites\3. eCommerce\mass\Libraries\Nop.Data\EfRepository.cs:line 63
   at Nop.Services.Customers.CustomerService.InsertCustomerExtended(CustomerExtended customerExtended) in d:\Websites\3. eCommerce\mass\Libraries\Nop.Services\Customers\CustomerService.cs:line 927
   at Nop.Web.Controllers.CustomerController.Register(FormCollection form) in d:\Websites\3. eCommerce\mass\Presentation\Nop.Web\Controllers\CustomerController.cs:line 1830



Your help is greatly appreciated.
6 years ago
Problem solved :D

I almost feel like kicking myself lol  the error is right there.  had my table fields as varbinary instead of nvarchar :/    silly me.

well all good. at least my post can be used as an example for setting up a new table :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.