简体   繁体   中英

The specified Cast is not a valid

I would appreciate some help debugging this please. I have a stored proc as defined below

ALTER PROCEDURE [dbo].[usp_ProcessBoxNumberPaymentInfo_Test]
    -- Add the parameters for the stored procedure here
     @boxNumber int, 
     @ProcessDate DateTime,
     @BoxType Varchar(50),
     @PaymentProcessDate DateTime,
     @Identity int OUTPUT  

BEGIN 
    Declare @NewID as int 
  -- INSERT CODE GOES HERE

   Set @NewID = SCOPE_IDENTITY()
  -- ANOTHER INSERT CODE GOES HERE
       SELECT @Identity = SCOPE_IDENTITY()  

END

On the code side, I have the following method in my DataContext class to execute my storedproc, passing the valids specified.

 [global::System.Data.Linq.Mapping.FunctionAttribute(Name = "dbo.usp_ProcessBoxNumberPaymentInfo_Test")]
        public int GetValidPaymentBoxNumberLogID_Test(
            [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "boxNumber", DbType = "Int")] string boxNumber,
            [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "ProcessDate", DbType = "DateTime")] DateTime ProcessDate,
            [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "BoxType", DbType = "VarChar(50)")] string BoxType,
            [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "PaymentProcessDate", DbType = "DateTime")] DateTime PaymentProcessDate,
            [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "Identity", DbType = "Int")] ref System.Nullable<int> identity)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), boxNumber, ProcessDate, BoxType, PaymentProcessDate, identity);
            identity = ((System.Nullable<int>)(result.GetParameterValue(4)));
            return ((int)(result.ReturnValue));
        }

Problem I am having is when I run the code, I get the following exception

The specified Cast is not a valid

This error is thrown at line return ((int)(result.ReturnValue)); I am not entirely sure what type is missing and where. Really, any help figuring this out will be greatly appreciated. Thanks in advance.

(Name = "boxNumber", DbType = "Int")] string boxNumber

看起来您正在尝试将字符串转换为int吗?

You can not use here Unboxing:

return ((int)(result.ReturnValue));

see http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx

Shoul be

return ((Int32.Parse(result.ReturnValue));

But if the result.ReturnValue is null it also will throw an exception.

safer

int a=0;
Int32.TryParse(result.ReturnValue, out a);
return a;

or check result.ReturnValue for nulls explicitly

When you call the store procedure try this:

ObjectResult<Nullable<int>> tmp;

tmp = GetValidPaymentBoxNumberLogID_Test (...

int identity = Convert.ToInt32(tmp.FirstOrDefault());

or just

int identity = Convert.ToInt32(GetValidPaymentBoxNumberLogID_Test (...).FirstOrDefault());

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