5

I'm not able to insert decimal values into Sql Server table.

What i'm trying to do is self explanatory through these lines of code:

long fileSizeInBytes = ComponentUploadControl.FileContent.Length;
Decimal fileSizeInMB = Convert.ToDecimal(fileSizeInBytes) / (1024.0m * 1024.0m);
Decimal fileSizeInMBRounded = Math.Round(fileSizeInMB, 2);
cmd.Parameters.Add("@fileSize", SqlDbType.Decimal).Value = fileSizeInMBRounded;

The value that is getting inserted into database is stripped of the decimal places. To be more specific if the fileSizeInMBRounded is 11.73, the value that is getting inserted into database is 11,00

Please help me

Thanks in anticipation

2
  • Why not simply store fileSizeInBytes out of interest? Commented May 2, 2011 at 14:33
  • The mistake is in the stored procedure i was specifying the parameter type as simply decimal instead it should be decimal(18,2). Any ways now it works like a charm after i corrected the mistake Commented May 2, 2011 at 15:17

4 Answers 4

9

My suspicion is that your decimal field is set up wrong in SQL. What you want is a decimal(18,2) field which allows for 2 decimal places. My suspicion is that you declared the field as decimal(18,0) (the default), which does not allow for any decimal places.

If you can use SQL Server Profiler to verify the contents of the INSERT going to your DB, it would be easier for you to determine whether a problem like this is due to your code or something on the SQL server.

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

3 Comments

Here is the mistake when i was inserting the data using the stored procedure,Create procedure [dbo].[NewComponentInsertion] (@componentName varchar(max), @fileSize decimal, @filePath varchar(max), @price decimal, @description varchar(max), @totalDownloads int, @disableFlag int) as insert into [dbo].[Components]([Component Name], [Size (in MB)],[Total Downloads], [FilePath], [Price (in SEK)], Description, DisableFlag) values (@componentName, @fileSize, @totalDownloads, @filePath, @price, @description, @disableFlag) I was inserting the decimal plainly without specification
it should be instead decimal(18,2), so the altered stored procedure is ,Create procedure [dbo].[NewComponentInsertion] (@componentName varchar(max), @fileSize decimal(18,2), @filePath varchar(max), @price decimal(18,2), @description varchar(max), @totalDownloads int, @disableFlag int) as insert into [dbo].[Components]([Component Name], [Size (in MB)],[Total Downloads], [FilePath], [Price (in SEK)], Description, DisableFlag) values (@componentName, @fileSize, @totalDownloads, @filePath, @price, @description, @disableFlag)
I see, so it was the parameter that was not declared correctly. Same concept though. Glad I could help!
4

Try to set the SqlParameter.Scale to 2.

SqlParameter parameter = new SqlParameter("@fileSize", SqlDbType.Decimal);
parameter.Scale = 2;
parameter.Value = fileSizeInMBRounded;
cmd.Parameters.Add(parameter);

Also look into Precision property as suggested by Bruno (default is 0).

1 Comment

as pointed out that was the error. Thanks for the advice though.
1

You should set Scale and Precision.

I think that is the problem.

Comments

1

Your parameter looks fine. I would check the data base column data type

1 Comment

You are right. Here is the mistake when i was inserting the data using the stored procedure,Create procedure [dbo].[NewComponentInsertion] (@componentName varchar(max), @fileSize decimal, @filePath varchar(max), @price decimal, @description varchar(max), @totalDownloads int, @disableFlag int) as insert into [dbo].[Components]([Component Name], [Size (in MB)],[Total Downloads], [FilePath], [Price (in SEK)], Description, DisableFlag) values (@componentName, @fileSize, @totalDownloads, @filePath, @price, @description, @disableFlag) I was inserting the decimal plainly without specification

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.