Mike Payne
Mike Payne

Reputation: 107

'The conversion of the nvarchar value '267915267915' overflowed an int column.'

I've searched far and wide for a solution to this problem(including here on StackOverflow), and I do not seem to find one with the same scenario. The error I am getting is:

The conversion of the nvarchar value '267915267915' overflowed an int column.

The problem is that I do not see where I am converting to an int column at all. Here is my C# code:

SqlCommand mothershipCommand = new SqlCommand("SELECT [ID] FROM [Order] 
                                               WHERE [OrderNumber] = @OrderNumber", mothershipConnection);
mothershipCommand.Parameters.AddWithValue("@OrderNumber", line.SONumber);
string orderID = mothershipCommand.ExecuteScalar().ToString();

Some background info:

The last line that begins with string orderID originally was an int, but after getting this error I changed it to a long, and that didn't help, so finally I tried changing it to a string to make sure the error wasn't on my end.

I do not see where the overflow is happening, and I have no clue where the 2679... number in the error message is coming from. Is there something I'm missing?

Upvotes: 0

Views: 3859

Answers (1)

Piotr Palka
Piotr Palka

Reputation: 3159

The code contains:

AddWithValue("@OrderNumber", line.SONumber)

Because you don't specify data type for @OrderNumber parameter, it will use type from line.SONumber which I assume is integer.
So your query will be executed as

WHERE CAST(line.SONumber AS integer) = 319279

Simplest solution will be to use:

.Parameters.Add("@OrderNumber",SqlDbType.VarChar,30).Value=line.SONumber.ToString();

Upvotes: 2

Related Questions