5

I have database column as "Salary". I'm keeping salary records of employees in this column as smallMoney. With C# I can insert this column value like "2000". After that I saw that columns value is "2000,00" in database. I need to get this value back like "2000" as string to update in my program. Because my program works like this. So, to use this value again, how can I convert this smallMoney to something to use effectively for my program? (Int "2000" or String "2000" is ok for my program)

I researched but couldn't find solution. Could anyone say how I do this?

  • There is a very thorough answer to that question here. [C#-SQL Server data types](http://stackoverflow.com/questions/425389/c-sharp-equivalent-of-sql-server-datatypes) – RoninEngineer Dec 14 '15 at 00:27
  • Try to add code like this in app_start, hope this will help ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); – Bhasyakarulu Kottakota Dec 14 '15 at 00:33
  • As it's your program, you do not really *need* the value as a string. You can accept any input and [convert](https://msdn.microsoft.com/en-us/library/ms173105.aspx) it internally whatever way you like. – Andrew Savinykh Dec 14 '15 at 00:43

1 Answers1

6

The SQL smallmoney is equal to a C# decimal. So after retrieving it from the database, you could convert it to an int. (You will lose the any decimal numbers though) OR you may use string formatting to only show the whole numbers.

For example:

// Create example value from database
decimal dSalary = 2000.00m;

int iSalary = Convert.ToInt32(dSalary);

Or you could keep it a decimal and just use string formatting, like this:

string Result = dSalary.ToString("0");
Moon
  • 1,141
  • 2
  • 11
  • 25