August 15th, 2016

ASP.NET Core Identity: Allow Login with either Email or Username

Do you want to allow user to login with either Username or Email address? ASP.NET Identity uses Username for Signin verification and In default Core template, Email is used as Username. In this post, we will change ASP.NET Core Identity setup to allow both Username and Email on login.

1. Username is unique for each user. It is either input by user or generated by application on registration.
2. No @ symbol allowed in Username.

July 17th, 2016

ASP.NET Core MVC: Enums with Select TagHelper

In this post, we will see how to use select tag helper in ASP.NET Core MVC and how to bind select tag helper to model data or enum values. The following environment is used for this post:

ASP.NET Core 1.0.1
Dapper 1.50.2
Visual Studio 2015 update 3

For the demo, let us assume we have following Department enum

July 17th, 2016

PostgreSQL Enum, C# and Dapper

Error: column "Gender" is of type enum but expression is of type text.

Are you getting this error when you are using enum datatype in PostgreSQL database? This blog post explains how to fix the error with C# and Dapper. The following environment is used for this post:

March 22nd, 2016

How to display SSRS Report in ASP.NET MVC Application

Today, we will explain how to show SSRS reports in ASP.NET MVC application. Suppose you already created awesome SSRS reports and want to use MVC as Report viewing application. Here is your solution.
Open existing application or create new MVC application. You can install reporting package from NuGet.
Go to Tools -> Library Package Manager -> click on Package Manager Console and type following command into command window.

November 5th, 2015

Export Data to Excel (.xls, .xlsx) in ASP.NET – C#

In this article, we will see different ways to export data to Excel from a web application. It is very common task for web developers. Here are different approaches to do it in ASP.NETC#:

Approach 1:

Using the Excel PIA (primary interop assemblies) to generate a spreadsheet server-side.

It needs Microsoft Office suite installed on your server which is not good. So it is NOT recommended.

August 27th, 2015

CRUD with Multiple File Upload, ASP.NET MVC 5 and EF 6

This tutorial explains the following things in ASP.NET MVC:
1. How to upload multiple files using HTML5 multiple file input with additional model data.
2. How to save files to a specific path on the server (Not in database).
3. How to allow to upload file with same name. Different users can upload files with same name.
4. How to download the uploaded file.
5. How to manage files in edit mode.
6. How to use Entity Framework for CRUD operations with parent child table structure.

March 1st, 2015

How React.js is good for SEO (Server-side React with ASP.NET)

The problem with the javascript frameworks is that they are NOT perfect search engine friendly. Although Google can crawl and index Ajax content but it is not so reliable and need to follow the guidelines. The advantage of React.js is that you can run it on the server and the virtual DOM will be rendered and returned to the browser as a regular web page. In this post, we will implement a sample in ASP.NET to pre-render the initial state of your React components server-side and the outcome is already rendered page of markup. So it will be indexed just like any other static page by search engine.

If you are new to React.js, I strongly recommend to read following tutorial:

React.js: Introduction and Hello World with ASP.NET MVC 5

February 3rd, 2015

Realtime Voting App with ASP.NET SignalR, React.js and D3.js

This tutorial shows how to build a survey or voting app that displays poll results in real-time using ASP.NET SignalR, React.js and D3.js.

ASP.NET SignalR: to build the real time voting app, then broadcast the live result as votes come in.
D3.js: For data visualization. (Creating Pie chart to show the result)
React.js: To update UI in a very efficient manner for real time changes.

If you are new to React.js, I strongly recommend to read following tutorial:
React.js: Introduction and Hello World with ASP.NET MVC 5

January 19th, 2015

Bulk Insert – Save DataTable to MongoDB – C# MongoDB Driver 2.1

Do you want to import csv or excel (xls, xlsx) file in MongoDB? You can read the file, get the data in C# datatable and save it to MongoDB using InsertBatch method. This article explains how to save C# DataTable to MongoDB for inserting a large amount of data.

To install Official .NET driver for MongoDB, run the following command in the Package Manager Console

Install-Package MongoDB.Driver

January 17th, 2015

Upload and Read CSV File in ASP.NET MVC

To read CSV file doesn’t mean to use String.Split(). CSV files may contain commas, carriage returns, speechmarks…etc within strings. In this post, we will learn how to upload and read CSV File in ASP.NET MVC WITHOUT using Jet/ACE OLEDB provider. It is helpful when you have to deploy your code on shared hosting, Azure website or any server where ACE Database engine is not available. We will use a fast CSV Reader by Sebastien Lorien.

Read Also: Upload and Read Excel File (.xls, .xlsx) in ASP.NET MVC.

January 15th, 2015

Upload and Read Excel File (.xls, .xlsx) in ASP.NET MVC

Are you using traditional way to read .xls and .xlsx files in ASP.NET/C#? If yes, you might be using following providers:
Microsoft.Jet.OLEDB.4.0 provider for Excel 97-2003 format (.xls)
Microsoft.ACE.OLEDB.12.0 provider for Excel 2007+ format (.xlsx)

and you may get the following common errors:

The ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered on the local machine.
The ‘Microsoft.ACE.OLEDB.12.0’ provider is not registered on the local machine.

and probably, you would go for following approach to fix them

1. Installation of Office 2007 Data Connectivity Components or Office 2010 Database Engine on the Server.
2. The application pool that the site is running set “Enable 32-bit applications” to “True
3. In project, Change Platform target from: Any CPU to x86 and Rebuild it.

What would happen if you have shared hosting or Azure Website hosting and You are not allowed to install ACE Database engine on the server?

November 30th, 2014

N-Layered App with Entity Framework, Autofac, ASP.NET MVC and Unit Testing

In my recent post, I explained how to implement a decoupled, unit-testable, N tier architecture based on Generic Repository Pattern with Entity Framework, IoC Container and Dependency Injection in ASP.NET MVC, then I got feedback against the repository/Unit of Work pattern. On googling, I found some more posts:

Say No to the Repository Pattern in your DAL
Repositories On Top UnitOfWork Are Not a Good Idea
Why Entity Framework renders the Repository pattern obsolete?

Entity Framework already implements a repository pattern. Implementing another layer on top of this is not only redundant, but makes maintenance harder. You might want to mock your Entity Framework context rather than using the repository pattern. This post explains how you can implement N Layered app without repository/unit of work pattern on top of EF.