Float, Double, Decimal in C#

Possibly save 1 hour of your time: There are times where you have to do some calculations on your data in .NET. If the data are based on different data types such as float, double or decimal, you will get error when you do calculations and storing the result in a certain datatype.

The best is to find out which of these fields have the most precision required or basically what is the biggest number possible.

Float, Double, Decimal is the biggest from the left to right respectively.

In my case, there are some big numbers so I just store them as decimals in both .NET and SQL.

Here is a good explanation on how SQL datatypes map to .NET datatypes:

https://msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx

(SQL -> .NET)

float -> double

decimal -> decimal

 

Error: System.InvalidOperationException : The specified cast from a materialized ‘System.Double’ type to the ‘System.Decimal’ type is not valid.

This happens when my SQL datatype is float but in .NET the datatype is decimal. Since I need to do calculations using divide operator, I will change SQL datatype from float to decimal(30,15).

 

Advertisements

Add nested dependent web.config transformation

Possibly save 1 hour of your time: When you want to do a continuous integration build, you may want to specify different web.config or app.config used for the transformation based on a specific environment configuration.

In my environment, we used web.config for local use. Then we have SYST for system integration environment (team check in), UAT for user acceptance testing environment and finally PROD for production environment.

So you add a web.SYST.config and it is not nested under the web.config. It should look like this.

web.config

Some people suggest right click on the Web.config and find Add Config Transform. That did not show up for me.

The alternative is to go into the csproj file directly and edit the include to have DependentUpon element.

content include.png

You also need to right click on the Solution and choose Configuration Manager. Under Active solution configuration, you can select New to create your own configuration. The name used here should also reflect the name in the config file.

configuration manager.png

Book:Murach’s ASP.NET 4.6 Web Programming with C 2015

Turn on https with SSL self signed certificate on local IIS environment

Possibly save 1 hour of your time: There are times when you have to set up your website during development to use https. Please see a reason from my previous article Chrome requires https for input text fields.

Create self signed certificate

  • Under IIS > select main instance > select Server Certificates in the middle pane
  • Right click and choose Create Self-Signed Certificate…
  • Enter friendly name for the certificate.

selfsignedcertificate.png

localtestingcertificate.png

Bind your site to the self signed certificate

  • Under IIS > Sites > select your website > on the right pane Actions select Bindings
  • On Site Bindings window > click Add
  • Add Site Binding

addsitebinding.png

That is it. You should be able to hit https on your website. You will still get unsecure message from the browser as it is not a real certificate.

IIS 10: Essentials for Administration (IT Pro Solutions)

Chrome requires https for input text fields

Possibly save 1 hour of your time: In October 2017, Chrome will be enforcing security warning for non https website that has input text fields. Your client may need to know this and decide to comply so their traffic will no face with not secure warning.

You will need to buy and bind the SSL certificate to your website.

See reference link for more details on how to setup for Azure website:

https://docs.microsoft.com/en-us/azure/app-service-web/app-service-web-tutorial-custom-ssl

Finally, you want to enforce https on your website so all HTTP requests will be redirect as a HTTPS request. One way to do that is through web.config but you will have to install URL Rewrite extension.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- BEGIN rule ELEMENT FOR HTTPS REDIRECT -->
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<!-- END rule ELEMENT FOR HTTPS REDIRECT -->
</rules>
</rewrite>
</system.webServer>
</configuration>

C 6.0 and the .NET 4.6 Framework

Caching big data in application using MemoryCache

Possibly save 2 hours of your time: I have only encountered this more recently in an application that deals with big dataset.
In this scenario, the application needs to provide a feature to download full datasets. And this dataset is huge (~75MB to ~125MB) in size. And there are many datasets.
It took awhile to query this data and do some calculation and send it back to the client app. So we decide to cache this data at the application level using System.Runtime.Caching.MemoryCache for subsequent faster performance. As you can see, the problem can become bigger as more datasets are downloaded and cached.
There are few options in no particular order:
  1. More expensive but easiest solution may be to increase memory on physical server to account for the size of the cache that can grow to.
  2. Recycle the application pool after reaching maximum virtual memory usage. See here for specifics.
  3. Set the CacheMemoryLimit on the MemoryCache object. This sets the amount of memory on the computer, in bytes, that can be used by the cache.