Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:48:45+00:00 2026-06-10T17:48:45+00:00

public class Customer : BaseClass<Customer> { public string Name { get; set; } public

  • 0
public class Customer : BaseClass<Customer>
{   
    public string Name { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public string TelephoneNumber { get; set; }
    public string InsuranceProvider { get; set; }
    public int? PolicyNumber { get; set; }
    public byte [] Photo { get; set; }
}

This message shows up:

enter image description here

Or, this:

enter image description here

How to modify this code to be able to persist data?

CustomerDAO.cs

class CustomerDAO
{
    .....
    public int Save(ITransactionManager tm, Customer item)
    {
        int count = -1;

        try
        {
            ISqlQueryExecutor<Customer> queryExecutor = new SqlQueryExecutor<Customer>(tm);

            count = 
                queryExecutor.
                ExecuteNonQuery(@"INSERT INTO Customer(
                                       ID
                                      ,Name
                                      ,TelephoneNumber
                                      ,DateOfBirth,
                                       InsuranceProvider,
                                       PolicyNumber)
                                  VALUES(
                                       @ID
                                      ,@Name
                                      ,@TelephoneNumber
                                      ,@DateOfBirth,
                                       @InsuranceProvider,
                                       @PolicyNumber)",
                                      item.ID,
                                      item.Name,
                                      item.TelephoneNumber,
                                      item.DateOfBirth,
                                      item.InsuranceProvider,
                                      item.PolicyNumber,item.Photo
                                      );
                                  //new DbParameter(item.ID, DbType.Int32),
                                  //new DbParameter(item.Name, DbType.String),
                                  //new DbParameter(item.TelephoneNumber, DbType.String),
                                  //new DbParameter(item.DateOfBirth, DbType.DateTime),
                                  //new DbParameter(item.InsuranceProvider, DbType.String),
                                  //new DbParameter(item.PolicyNumber, DbType.Int32)
                                  //new DbParameter(item.Photo, DbType.Binary)
                                  //);



            string str = string.Empty;
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return count;
    }
    .... ....
}

CustomerBLL.cs

class CustomerBLL
{
... ... ...
    public int Save(Customer item)
    {
        int newId = 0;

        ITransactionManager tm = ApplicationContext.Get(DBNameConst.ActiveConnStringName);

        try
        {
            tm.BeginTransaction();

            item.ID = newId = PivotTable.GetNextID(tm, "Customer").Value;

            customerDao.Save(tm, item);

            PivotTable.UpdateNextIdField(tm, "Customer", newId);

            tm.CommitTransaction();
        }
        catch (Exception ex)
        {
            tm.RollbackTransaction();

            throw ex;
        }

        return newId;
    }
    ... ... ...
 }

ASqlQueryExecutor.cs

public abstract class ASqlQueryExecutor<T> : ISqlQueryExecutor<T>
{
 public virtual int ExecuteNonQuery(string queryString, params object[] parameters)
    {
        int count = -1;

        try
        {
            Command = ParameterAttacher.AttachSaveParameters(TransactionManager, queryString, parameters);
            Command.CommandText = queryString;
            count = Command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return count;
    }      

ParameterAttacher.cs

class ParameterAttacher
{
    public static IDbCommand AttachSaveParameters(ITransactionManager tm, string queryString, params object [] argumentsList)
    {
        IDbCommand command = new DbObjectInstantiator(tm.ProviderName).CreateCommand();
        command.Connection = tm.Connection;
        command.Transaction = tm.Transaction;

        IList<string> parameterNamesList = new List<string>(ParameterParser.Parse(queryString));

        if (parameterNamesList.Count > 0 && argumentsList.Length == argumentsList.Length)
        {
            int i = 0;

            foreach (string paramName in parameterNamesList)
            {
                Attach(command, paramName, argumentsList[i]);

                ++i;
            }
        }

        return command;
    }

    public static void Attach(IDbCommand command, string paramName, object dbParam)
    {
        IDbDataParameter param = command.CreateParameter();

        param.ParameterName = paramName;
        param.Value = (dbParam==null) ? ((object)DBNull.Value) : dbParam;
        //param.DbType = dbParam.DbType;

        command.Parameters.Add(param);
    }
}

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:48:47+00:00Added an answer on June 10, 2026 at 5:48 pm

    string or binary data would be truncated. the statement has been terminated

    This could occur because you’re trying to store too much data into a column. For example, if you have an nvarchar(5) column in a database and you try to store “this is a string” in there, you might get that error because 5 characters can’t hold all of “this is a string”.

    You can avoid this problem by limiting the fields in your UI to the same length as those in the database. Or, you can perform a check in the validation methods.

    Implicit conversion from data type nvarchar to binary is not allowed.

    This seems fairly obvious: you’re trying to store a character value in a binary column. You haven’t provided your database schema, so I can’t tell for sure where this could be; but, if you have a column or sproc parameter in the database set as binary but your C# details it as DbType.String you might get this error.

    UPDATE:

    You never set your DbType in ParameterAttacher.Attach. This means the Parameter will default to DbType.AnsiString for the parameter type. If you pass it a byte[] it may convert that to an ansi string, but when the parameter is given to ADO, it will see DbType.AnsiString and compare that to varbinary(50) (or money, or datetime, or int, etc.) and throw an exception detailing that it doesn’t know how to convert to binary (e.g. an implicit conversion).

    Also, do yourself a favour and get rid of:

    catch(Exception ex)
    {
      throw ex;
    }
    

    That will just force you to loose the real location of the exception and cause you to waste time trying to figure out where the real problem is.

    When you must catch (e.g. when you want to rollback the transaction, just throw, don’t throw ex. Just throw won’t lose the stack information and you can track down the location of the exception. For example:

        catch (Exception ex)
        {
            tm.RollbackTransaction();
    
            throw;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Customer { public int CustomerId { get; set; } public string FirstName
given: public class Customer { public int Id { get; set; } public string
I have a class like that: public class Customer { public string Name {get;
public class Address { public string ZipCode {get; set;} } public class Customer {
Case: public class customer { public string Adress { get; set; } } Xaml:
I have a class declaration public class Customer { public string id { get;set;}
public class MyClass { public string MyProperty{ get; set; } Now, I would like
Given the model: public abstract class Person { public int Id {get;set;} } public
Here is one of my Entityclasses: public class Customer { public int Id {
This is an extract of the JPA entities schema @Entity @Table(name=customer) public class Customer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.