August 15th, 2016

Generalize a method to read any master table with C# and Dapper

On a web page or application form, it is common to have different master data like Country, State…etc. available in dropdownlist, listbox or any other control. It is needed to read all master data from database and bind to proper controls. This post explains different ways to read master tables with C# and Dapper.

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:

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.

May 21st, 2015

A Better Way To Manage ASP.NET Session With Generic Wrapper

Before we get started, let us see the general way to use Session in ASP.NET

if (Session["UserName"] == null) 
{ 
    LabelUserName.Text = "Anonymous"; 
} 
else 
{ 
    LabelUserName.Text = (string)Session["UserName"]; 
} 

You can feel some drawbacks e.g. to check null reference, type safety, to cast object..etc.

Now add following class and see how it solves the issues:

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?

February 8th, 2014

Get Browser Height and Width on Server Side in ASP.NET

A Responsive Web Design is not only UI changes but also to optimize server side logic for performance. Suppose you are displaying bulk data in desktop environment. But if same data are sent to mobile device, it will make website too slow even the layout is made adaptive on client side. To implement server side logic, we need client side information like browser width and height. In this article, we will implement to get browser height and width on server side in ASP.NET.

September 10th, 2013

Combine Images into Facebook Style Collage in ASP.NET Web Forms

Facebook home page displays a collage of random profile photos which makes it something special. So I decided to implement the same thing in ASP.NET. In this tutorial, we will do following things:
1. To get random image names from database using Entity Framework Database First approach.
2. Calculate the total needed images for a given width and height. Assuming all profile images have same size.
3. Combine multiple images into collage using C# graphics
4. Add borders to differentiate images
5. Create linear horizontal opacity gradient for the collage and add it in a page similar to Facebook.

June 8th, 2013

C# LINQ: Combine Multiple Sequences In Parallel

In this article, we will see how to join multiple arrays, lists or collections by order with LINQ. In other words, we can say Zip operation on multiple sequences. Assuming we have following arrays:

            int[] numbers = { 1, 2, 3 };
            string[] abc = { "a", "b", "c" };
            string[] pqr = { "p", "q", "r" };
            string[] xyz = { "x", "y", "z" };
            string[] words = { "one", "two", "three" };
January 28th, 2012

Grouping Data in ASP.NET Gridview Control

In my previous post, I explained how to group HTML table rows with jQuery. This post explains How to do same thing using ASP.NET Gridview control on server side. The logic is same to check row by row. If data is same in next row’s cell, hide next row’s cell and increase rowspan of current row’s cell.