.NET Examples

This page includes a list of .NET programming examples, with source code.


Office Stuff

Using ADO.NET with Excel

This is a console app that uses ADO.NET to generate a new .xls file. No automation.
Sept 2003

Source code

Using Automation to insert SQL data into Excel

This is a console app that uses automation (via the Interop Assemblies) to add QueryTable that inserts data into Excel from SQL Server.
Sept 2003

Source code

Web services

Zipcodes

A web service that maps US zipcodes to city+state pairs. Implemented in .NET. Source code is available, as is a working example.
April 2003

Zipservice home page

Book Info

A web service that does keyword searches for book titles, and also maps from ISBN codes to titles, and does lookups on pricing at online book sellers.
April 2003

Book service home page

Windows Forms

IE Cache Viewer

This is a WinForms app that uses pInvoke to interrogate and modify the contents of the IE Cache. This sample illustrates some interesting winforms things:
2005 January

Download the VS2003 project
Screen shot

Downloadable Control

This is a UserControl that can be hosted in a Windows Forms smart client standalone, or within IE. The control embeds a RichTextBox, and upon loading, the control tries to interrogate the CodeBase and Location of its assembly, and display those results. This interrogation may be prohibited by .NET security settings.
April 2003

Source code
Working example - hosted in IE
A second example - with a versioned control, hosted in IE

WMI

Set Default Printer

This is a small sample that illustrates how to use WMI to get and set the default printer on Win32. This is a console app but the code can easily be adapted into a winforms app.
Sept 2003

Source code

XML Things

Consuming XML from an HTTP feed

This example shows how to consume XML obtained from an HTTP URL, within an ASP.NET page. The XML can be generated by "anything." In this example, an ASP.NET page consumes XML generated by one of (a) a locally hosted ASP.NET page, (b) a remotely-hosted ASP.NET page, or (c) a remotely-hosted JSP page. There was a prior version of this example that used the NASDAQ xml quote service, but apparently Nasdaq.com took that service offline (as of February 2004). In this example, the XML is generated from a query on a SQL Server database. The consuming ASP.NET page retrieves the XML, does a XSL Transform on it, then displays the resulting HTML. As an illustration, there is also an example that does the converse: in other words it's a JSP page that consumes XML from an ASPX.


2004 March

Run the sample
View the source

MQSeries

MQ Load

Simple console app that creates an MQ Queue, then loads 500 messages onto it.
published February 2006

source code

MQ Inquire Queue Names

This is a tiny command-line app, implemented in C#, that uses PCF in the MQ Classes for .NET, to inquire the queue names on a MQ QM. PCF is not supported in the MQ Classes for .NET for MQ v6.0, but it works anyway.
published February 2006

source code

MQ ADSI Explorer

This is a simple WinForms explorer app that uses ADSI (via COM interop) to explore MQSeries. Starting with version 5.1? of IBM MQSeries, Mq exposes an ADSI provider. By this means, MQ objects, including queues, channels, and connections, are queryable and settable via ADSI. This app illustrates how to do it.
originally published April 2003, updated November 2004

Source code (VS 2003 project)
Screen shot image

MQ Postcard

MQSeries ships with a "postcard" app that can be used to verify the installation of mqseries. This is an implementation of the MQ postcard app in C#, using the "MQ Classes for .NET" class libary, that originally shipped in MQ 5.3 CSD05 on Windows in October 2003. (The ma7p supportpac from IBM, published in February 2003, was the precursor of this class library). This postcard app interoperates with the postcard apps IBM has supplied (one written in Java, and another shipped only in binary form).
December 2005

Source code (VS 2003 project)

MQ PCF

IBM's "MQ Classes for .NET" class library that ships with MQ includes undocumented PCF support. This example shows how to check for the existence of a queue, and create a queue, programmatically, using that PCF stuff.
December 2005

Source code

Miscellaneous Others

(if you are looking for Ionic's Isapi Rewriting Filter, click here)


PDF Creation in .NET

Creating PDFs can be handy. Doing it from .NET seems like a broad need. Here are some examples for how to do it, using the itextsharp library.

published February 2006

Source code for simple example (console app)
Source code for nested tables (console app)
SudokuToy (Winforms app, VS2003 Solution), produces Sudoku puzzle in PDF.

Morpho

Didya ever wonder?... why DataGrids and other data-bound controls can use arrays of objects as data sources, but they display only public properties (not fields)? On the other hand, types generated by wsdl.exe generally export fields (not properties).

Suppose you want to invoke a web services method and display the result in a databound control. Or suppose you want to use XML Serialization, and display the result into a data-bound control. What are you gonna do?

But wait! There is a better way.

This sample shows a general solution for how to morph types with public fields - something you might get from a web service or from XML Serialization - into types with public properties - something a data-bound control can use. The approach is to dynamically generate a type, at runtime, and then convert the array of type1 into an array of type2, and use type2 as the DataSource for the data-bound control. eg,

// get array of types with public fields glue.webservice.OrderInfo[] oia= wsproxy.getOrdersForCustomer(TextBox1.Text); // generate a morphed array suitable for use in a DataGrid: DataGrid1.DataSource= MorphArray(oia);

Because this example uses System.CodeDom to generate the type, it also serves as a short illustration of how to use that nifty technology.

December 2002

Source code

Regular Expression Verifier

When I build Regex's , I use a visual tool like

But still, the testing of all of the combinations of input is not easy with this sort of visual tool. A generic regex test harness is useful. This is that tool. The output is in XML to allow scripting.

October 2003

Source code

Wake-On-Lan

This console app sends a Wake-on-LAN Magic packet to the given MAC address.

October 2003

Source code

Assembly Interrogator

This simple Windows Forms app loads an assembly from a local DLL or from a URL, then interrogates the assembly for its version, culture, codebase, location, etc; and then displays the results. It also itemizes the types available in the assembly.

April 2003

Source code

High Resolution Timer

If you rely on System.DateTime to measure something like an RPC call or a web service invocation, you will be disappointed in the resolution of the tick. This utility class shows how to use PInvoke to call into the kernel32.dll and query the performance counters to get a hi-res timer in managed code.

December 2002

Source code

Data Copier

Did you ever come upon the situation where you want to fill a DataSet from one database, but then update another database with the data? This simple example shows how to associate different connections to the Update command and the Select command for a single DataAdapter, thereby enabling this scenario. The example works with SQL Server, but could be modified to work with any 2 databases that share a common DataAdapter (eg, they are both accessed from the OledbDataAdapter).

You could also modify this sample to use 2 distinct DataAdapters.

February 2003

Source code

This page was last updated Friday, 2 December 2005
This page has been accessed 24319 times since 18 September 2003