I am getting the error:
Failed to convert parameter value from
a Int64 to a Byte[].
Here is what I am doing…
I have a table in my database with the column:
sessionid | binary(32) | null
When I assign to it, I do this:
user.LastActivityDate = DateTime.Now;
user.SessionId = user.LastActivityDate.ToBinary();
SessionId is an inherited property from an interface:
long? SessionId { get; set; }
And here it is being accessed in my User class:
[SettingsAllowAnonymous(false), CustomProviderData("SessionId;string")]
public long? SessionId { get { return base["SessionId"] as long?; } set { base["SessionId"] = value; } }
In my custom profile provider, the following command assigns the value:
sqlCommand.Parameters.Add("sessionid", SqlDbType.Binary, 32).Value = settingsPropertyValue.PropertyValue;
When the “sqlCommand.ExecuteNonQuery()” command executes an update stored
procedure is executed with the parameter:
@sessionid binary(32)
And the result is the error:
“Failed to convert parameter value
from a Int64 to a Byte[].”
I have tried:
- … = Convert.ToByte(settingsPropertyValue.PropertyValue);
- … = Convert.ToInt64(settingsPropertyValue.PropertyValue);
Any ideas? Thanks.
SqlDbType.Binaryis not correct (when you add the parameter) — that’s for extended binary data of arbitrary length. You want to just represent it as a 64-bit integer, which isSqlDbType.BigInt.By the way, why are you doing this in the first place? SQL has a date-time type; you can just store it like that.
(You could go about it in the opposite, perverse way and instead convert the long to an eight-byte array and pass that, but there’s no conceivable reason you would want to store it in that manner.)