Tag Archives: C#

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

Asp.Net & ORM Tool NHibernate For Dummy Tutorial – Select & Insert Operation

NHibernate is a open source Object-Relational Mapping (ORM) tool. This ORM tool hides sql query complexities by replacing query with mapping and class object.
Every .Net Project can use Nhibernate to work with database.

The purpose of NHibernate is to save developers work time thus increasing productivity. Large application may get more benefit from NHibernate than small app.

This blog post gives Asp.Net & NHibernate dummy examples. These examples cover select / retrieve records and insert a new record.

First thing first, we need to download Nhibernate files. Please download the zip file from http://sourceforge.net/projects/nhibernate/

As a brief, we need xml mapping file, a class which representates DB table, Nhibernate dll file, NHibernate xml configuration file, and Asp.Net file it self that contains NHibernate configuration object, NHibernate session etc to provide data to UI.

I use VS 2010 Express & SQL Server 2008 R2 to build this example.
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