.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:
- P/Invoke to the Wininet.dll
- Using a custom class to allow sorting and filtering of an
arraylist bound to a datagrid (Brendan Tompkins' CollectionView)
- dynamically re-sizing a DataGrid
- custom DataGrid that exposes the vertical scrollbar for
programmatic movement (auto-scroll)
- master/detail datagrids
- Splitter
2005 January
Download the VS2003 project
Screen shot
Downloadable Control
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
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?
-
Manually create DataRows and Datacolumns? Yuck! only if you get
paid per line of code.
- Use a DataSet?
Nope, for web services, that only works in .NET-to-.NET calls
(have you heard of Java?), and it won't work with XML Serialization.
- Ok, then I guess we should Hand-code a new
custom type for every different type? (this is the approach taken in this MSDN article from January 2003). This works, but involves writing code for every new scenario.
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