1

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.

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i used your code with few changes "int x = (int)Convert.ToInt64(some string value)" and now its working fine. anyway thank you so much for your post. regards- ammy
@user1766397 Glad I could help. Please accept the answer by activating the checkmark on the left if the answer was helpful. And welcome to stackoverflow!
2

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.