Category Archives: C#

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<Person> list;
    	public Persons()
            list = new List<Person>()
                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<Person> 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 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 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

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

//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

BackgroundWorker Part 2 – Reporting Class Instance Status (C#)

About long time ago, I already wrote about basic of BackgroundWorker component, BackgroundWorker Part 1.
It contained simple example on how to use BackgroundWorker and also ProgressBar. If you want to look then go to this url

This part 2 will go for more advance implementation example. This means application itself is simple but its coding technique is more advance.

As you may know by now that BackgroundWorker makes procedure run asynchronously. For example I have a procedure to run and the progress change of running procedure is reported.
This is possible because of this procedure run asynchronously by BackgroundWorker.

In order to run a BackgroundWorker, we have to use DoWork event handler function. So we need to write a procedure we want inside that DoWork function.
Most of examples show how to just write a whole codes in DoWork. For example below code:

void backgroundworker1_DoWork(object sender, DoWorkEventArgs e)
		for (int i = 1; i <= numericUpDown1.Value; i++)
			if (backgroundworker1.CancellationPending)
				e.Cancel = true;

So it shows simple looping and send its report progress but the whole iteration coding is inside DoWork. When more complex requirement is needed then the coding become cumbersome.

We need a way to separate main procedure from DoWork function. So, it will also make loose coupling.
This procedure inside DoWork must have asynchronous capability. To accomplish this, I use Thread object. Thread will make asynchronous process.
Continue reading

Abstract Factory Pattern Example With C#

Abstract factory pattern is to “provide an interface for creating families of related or dependent objects without specifying their concrete classes.”

For example a drawing & printing machine can process Low and High Resolution. Both have its own resolution drivers. Those drivers referred to concrete class with draw / print function. However the client implementation code asks the factory object class (not the concrete class) for a driver instance with provides information about resolution required. So this factory object return instance of concrete class.
The factory object implements an abstract class. It can instantiates a concrete class and return to client with casted to factory’s parent which is abstract Resolution driver.
The client uses the resolution driver as abstract without being aware about their concrete implementation.

Benefit of using factory pattern is in client code implementation, new resolution can be added with just providing that new resolution info as argument. So no change in client code.
Continue reading

Fill DataGridView and cancel process using DataAdapter and BackgroundWorker


Filling data to DataGridView with SqlDataAdapter is a common task but to cancel it in the middle of fill process need a tricky way. Especially when SqlDataAdapter returning large DataSet then no progress status informed to user since SqlDataAdapter need time to fetch DataSet. Also this project does not need to have button like “More records” to fetch more dataset. We can retrieve as much records you like and cancel it any time.


We need to show records immediately and user can cancel the retrieval process whenever they like. The basic idea is using int recordcount = SqlDataAdapter.Fill(int startrow, int maxrecords, DataTable dt) method at BackgroundWorker.DoWork event. That Fill method is similar to paging in web page. BackgroundWorker execute SqlDataAdapter.Fill in loop until it has no more record to fetch and we can cancel the process before or after SqlDataAdapter.Fill. Continue reading