user1766397
user1766397

Reputation: 11

"Failed to convert parameter value from int64 to int32"

I'm inserting few values into a SQL Server table. When I'm getting the values from the front end they are strings. So I need to convert them to int first, before I can insert them into the table.

I'm using this code:

Int64 x = Convert.ToInt64(some string value);

This x I want to insert into the table, into a column of type numeric(18,0).

When doing this conversion, I get an error

Failed to convert parameter value from int64 to int32

while my all the values are just like 1000 around.

Upvotes: 1

Views: 9698

Answers (3)

Dennis Traub
Dennis Traub

Reputation: 51634

Use int x = Convert.ToInt32(some string value) and it should work.

Your db expects a value of type Int32. SQL server won't implicitly convert a 64-bit number to a 32-bit number because of the potential loss of information.

Upvotes: 4

Neil Knight
Neil Knight

Reputation: 48547

An Int64 ranges from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, which as you can see is 19 characters long (excluding the commas) so it wouldn't fit in you numeric range of 18. If you really want to use an Int64 for small numbers like 1000, you should amend your column to the datatype bigint.

Upvotes: 2

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

For System.Int64 (long) in sql server side you must use bigint data type. http://msdn.microsoft.com/en-us/library/ms187745.aspx

Upvotes: 0

Related Questions