简体   繁体   中英

Failed to convert parameter value from a String to a Decimal?

I have a stored procedure like this:

ALTER PROCEDURE [dbo].[usp_CSR_UpdateDailyCustomerWithLCDHistory]
    -- Add the parameters for the stored procedure here
    @person_id numeric(18, 0),
    @last_contact_date datetime,
    @source nvarchar(50),
    @callLogId int,
    @createdBy nvarchar(50)
AS
BEGIN...END

In my code I call this stored procedure:

public void UpdateDailyCustomerWithLCDHistory(string callerId, 
    int callLogId, string csrName)
{
    try
    {
        Database db = DatabaseFactory.CreateDatabase
           ("MarketingWriteConnectionString");
        DbCommand dbCommand = 
           db.GetStoredProcCommand("usp_CSR_UpdateDailyCustomerWithLCDHistory");
        dbCommand.CommandTimeout = AppSetting.Instance.GetCommandTimeout();

        db.AddInParameter(dbCommand, "person_id", DbType.Decimal, callerId);
        db.AddInParameter(dbCommand, "last_contact_date", DbType.Date,
            DateTime.Now.ToShortDateString());
        db.AddInParameter(dbCommand, "source", DbType.String, "CSR");
        db.AddInParameter(dbCommand, "callLogId", DbType.Int32, callLogId);
        db.AddInParameter(dbCommand, "createdBy", DbType.String, csrName);

        db.ExecuteNonQuery(dbCommand);
    }
}

And this is my table's structure:

CREATE TABLE [dbo].[tblDailyCustomerLastContactDateHistory](
    [person_id] [numeric](18, 0) NOT NULL,
    [source] [nvarchar](50) NOT NULL,
    [callLogId] [int] NULL,
    [createdBy] [nvarchar](50) NULL,
    [last_contact_date] [datetime] NOT NULL,

After I deploy my application, I got the error

Failed to convert parameter value from a String to a Decimal

quite often, not all the time though. Can anybody tell me what could be wrong?

public void UpdateDailyCustomerWithLCDHistory(string callerId, int callLogId, string csrName)
{
    try
    {
        Database db = DatabaseFactory.CreateDatabase("MarketingWriteConnectionString");
        DbCommand dbCommand = db.GetStoredProcCommand("usp_CSR_UpdateDailyCustomerWithLCDHistory");
        dbCommand.CommandTimeout = AppSetting.Instance.GetCommandTimeout();

        db.AddInParameter(dbCommand, "person_id", DbType.Decimal, callerId);

the callId is a string, you need convert it the decimal before you assign to the value.

you can use Decimal.TryParse() to convert string to Decimal number. http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx

I would advise you to use an INT for an Id instead of NUMERIC (decimal) field, if you want a numeric identifier. Unless you'll be having Ids that are 1.2, 1.3, etc.

Also, take a look at AddWithValue - you won't have to specify a type in your .NET code. The framework will handle that for you.

Lastly, if callerId isn't a matching type, you should either change it or at the least, convert it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM