Tag Archives: C#

C# Async Await Implementation Examples on Console App, Win Form and Asp.Net Web App

This post gives examples to use async and await in order to process something asynchronously.

Examples will be presented as Console App, Win Form and Asp.Net Web App.

Taking a definition reference of Async & Await from https://msdn.microsoft.com/en-us/library/hh191443(v=vs.120).aspx :
You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming. However, traditional techniques for writing asynchronous applications can be complicated, making them difficult to write, debug, and maintain.

Visual Studio 2012 introduces a simplified approach, async programming, that leverages asynchronous support in the .NET Framework 4.5 and the Windows Runtime.
The compiler does the difficult work that the developer used to do, and your application retains a logical structure that resembles synchronous code.
As a result, you get all the advantages of asynchronous programming with a fraction of the effort.

Continue reading


Basic Understanding of Using JQuery DataTable Server Side And Asp.Net


JQuery DataTable definition taken from https://datatables.net/ that DataTables is a plug-in for the jQuery Javascript library.
It is a highly flexible tool, based upon the foundations of progressive enhancement, and will add advanced interaction controls to any HTML table.

Common example of basic usage on JQuery DataTable is not Server side processing. You can imagine it is a data collection method that retrieve all data represent as Json string and stored in the DataTable.
So paging, searching, ordering are fully client side processing. This is handy if the data is not large enough.

On the other hand, Server Side processing in JQuery DataTable take data partially based on user request.
Pagination queries data in between start & end records in that page.
Searching will search data on fields usualy with Sql Like also Ordering will do Sql Order by.
All data taken from database server dynamically.

However, this server side processing could be confusing for someone who just exploring JQuery DataTable.
So this post explains jQuery DataTable server side basic understanding with a simple example.
We will use Newtonsoft component to convert class object to Json string. You need to install it using Nuget package manager.


First of all, we need to include CSS, jQuery and jQuery DataTable javascript in aspx file.

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

And then the html table as DataTable. This table has id example

<table id="example" class="display" cellspacing="0" width="100%">
			<th>Full Name</th>
			<th>Phone Number</th>
			 <th>Fax Number</th>
			<th>Email Address</th>


Continue reading

Upload file to Azure Cloud Storage Via Asp.Net Web App



Micrososft WindowsAzure is a rock solid Cloud platform that enables you automatically scales in/out your web application.
Not only scaling capabilites but also storing your files whether they are big or small in Azure Cloud Storage makes your mind free from conserning your own reliable storage on premise.
I give you basic example how to upload a file to Azure Cloud Storage via Web App using Asp.Net C#. Also to provide a link for user to download that uploaded file.

Create Storage Account or Use Local Development Storage

If you are still under development and run the web app under local PC/Laptop then you can use Development Storage.
Install necessary Azure SDK using Visual Studio 2013 or 2015 or Microsoft Web Platform Installer. After the installation done you should have Compute Emulator and Storage Emulator.
The storage emulator must be started properly before hand in order to run your Storage application in your local environment.

To access your storage you must supply Storage Connection String. Since we are using local environment then the Storage Connection String is UseDevelopmentStorage=true.
Please pay attention of it is a case sensitive.

After you finish your development and want to publish it on the cloud then you must register for Storage Account withing Azure portal and change your Storage Connection String properly in your code or configuration file.
Continue reading

Creating Custom Asp.Net Web API using Handler .ASHX Also Example On Using It With JQuery And WebRequest

This blog post is about custom Asp.Net Web API and it is not like standard Web API using Web Api Controllers, Url routing etc.
I use general term or concept of Web API that is simply programming interface in a web. This can be done with many ways. Later on, the standard Microsoft Asp.Net Web API will be presented in this blog. API is just an additional layer in application usually to access data. Web API can be consumed by any device or method that support HTTP. With that concept, I use Handler .ASHX to make an Web API. This post shows custom Web API used by Client script with JQuery also by Asp.Net Server Control. I give those examples in below list. Steps for creating custom Web API using .ASHX:

  1. Create a Web Project
  2. Add new class as Model to represent data: (Persons.cs)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    public class Persons
        List&lt;Person&gt; list;
    	public Persons()
            list = new List&lt;Person&gt;()
                new Person() { Id=1, FirstName="John", LastName="Doe", Department="A" },
                new Person() { Id=2, FirstName="Jane", LastName="Voltus", Department="A"},
                new Person() { Id=3, FirstName="Joseph", LastName="Chow", Department="B"}
        public List&lt;Person&gt; getPersonList() {
            return list;
        public void addPerson(Person p)
        public void deletePerson(int Id)
    		//code to delete
    public class Person
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }

    Off course you can change to use database connection and so on.

Continue reading

Using Asp .Net TabContainer To Create Professional Entry Form/View

Asp .Net TabContainer is useful for multi view purpose via clicking its Tab Panels.
The idea is using two TabPanels as containers for Entry form and list view. The first Tab Panel contains Entry Form and second for List View as we can see at above screen shots.

I use simple Northwind Database and Products Table to create Entry and View to this table.

Asp .Net TabContainer Basic

We must add reference to Asp .Net Ajax Control Toolkit since TabContainer Control is inside it.

  • Add ToolkitScriptManager Control inside form tag
  • Add TabContainer Control after ToolkitScriptManager.
     <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    	<asp:TabContainer ID="TabContainer1" runat="server" >
    		 <asp:TabPanel ID="tabpanel1" runat="server" HeaderText="Entry">
    		 <asp:TabPanel ID="tabpanel2" runat="server" HeaderText="View">

Basic TabContainer is already created with simple above Asp .Net Script. You can switch between TabPanels but these still contains nothing.
Continue reading

Applying Report Viewer 2010 Control With RDLC file In Visual Studio 2010 Express Edition (C#)

I use Report Viewer 2010 or version 10 instead of 8 or 9, so that more updated of Report Definition file can be viewed.

First of all, please download Report Viewer 2010 control (Report Viewer Version 10) from this link http://www.microsoft.com/en-us/download/details.aspx?id=6442 and install it since VS 2010 Express was shipped with Report Viewer Version 8 and/or 9.

After you install it, that component will be registered on C:\Windows\assembly folder. If you browse to C:\Windows\assembly using Windows Explorer then you will see custom shell extension view.

Please take note that even though we already install Report Viewer 2010 but VS 2010 Express Edition still not recognized version 10. It still has version 9. We need to copy required library files and add them as project reference.

However, you can not just copy those Assembly’s library with just right click. We need to copy installed 3 library files comes from Report Viewer 10 installation which are Microsoft.ReportViewer.Common,
Microsoft.ReportViewer.ProcessingObjectModel and Microsoft.ReportViewer.Winforms to our own Folder as usual file system.
Continue reading

Create Excel Report Programmatically Without Installing MS Excel. Asp.Net & Win Form C# Using WebService Example

Assuming you only have open source spreadsheet software i.e OpenOffice.org Calc and you want to make Excel report file programmtically.
You dont have MS Office installed in your computer or server. Sure you can do it using ExcelLibrary library from Google Apps.

In order to try examples in this blog post, please download ExcelLibrary library from this url https://code.google.com/p/excellibrary/downloads/list

Here, I give examples implementing it by creating a WebService so that Asp.Net Website or Win From C# can use this WebService.
This makes ExcelLibrary file only have to copy in WebService’s server without distribute it to local / client box.

First of all we need to know about basic of ExcelLibrary usage. Taken from https://code.google.com/p/excellibrary

//create new xls file
string file = "C:\\newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell("Text string");
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;

// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

 // traverse cells
 foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
     dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;

 // traverse rows by Index
 for (int rowIndex = sheet.Cells.FirstRowIndex; 
        rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
     Row row = sheet.Cells.GetRow(rowIndex);
     for (int colIndex = row.FirstColIndex; 
        colIndex <= row.LastColIndex; colIndex++)
         Cell cell = row.GetCell(colIndex);

Continue reading