Posted on

Sharing a Class In ASP.NET Pages

It’s easy enough to create additional classes that can be used in the code behind files of an ASP.NET page, but what if you want to share a class among the ASP pages themselves? It’s quite common and easy to develop generic methods and event handlers that can reduce repetition of code and simplify the process of creating the different parts of your UI. In a recent project, I needed to create such a class and make it available to all my ASP pages. The class contained two methods that were meant to be called by the DataBind event of a GridView or DetailView in order to assign Javascript confirmations to all the Delete Commands and looked something like this…

namespace ProjectNameSpace {

 public class AdminUI {

 public void add_delete_confirmations_to_grid_view(object sender, EventArgs e)
 {
 // Do stuff
 }

 public void add_delete_confirmations_to_detail_view(object sender, EventArgs e)
 {
 // Do stuff
 }
 }
}

Now that we have the class, we need to find the way to make it available to our ASP pages. The usual way to do this is via an entry in the web.config that makes the namespace available like this….


 
 
 

…which works fine if you intend on calling the method using ASP tags…

<% AdminUI.add_delete_confirmations_to_grid_view(); %>

…but the methods above aren’t mean to be used in ASP tags. They are meant to be assigned directly to events of ASP Controls like so…

<ASP:GridView OnDataBoundEvent="AdminUI.add_delete_confirmations_to_grid_view">

…which sadly, does not work. This seems to be something that was overlooked in ASP.NET. The former compiles, but the latter generates a “AdminUI namespace not found” type of error.

So in order to get around that, we are going to have to make use of inheritance.

Normally, the code behind classes for an ASP page are children of the System.Web.UI.Page like this….

public partial class Users : System.Web.UI.Page { }

Therefore, I can construct my AdminUI class as a middle layer between my ASP classes and System.Web.UI.Page by changing AdminUI to…

public class AdminUI : System.Web.UI.Page {}

… and all of my ASP classes to something like this…

public partial class Users : AdminUI { }

… and the assignment of the methods to their corresponding events becomes this…

<ASP:GridView OnDataBoundEvent="add_delete_confirmations_to_grid_view">

…and it works perfectly. All the methods that normally would have to be duplicated in multiple code behind files are in a centralized location and inherited by all of the classes so they can be used directly in the ASP pages without triggering a compiler error.

Posted on

Detecting USB Insertion/Removal in C# .Net 4.0

We need your help!


Cyber Sprocket is looking to qualify for a small business grant so we can continue our development efforts. We are working on a custom application builder platform so you can build custom mobile apps for your business. If we reach our 250-person goal have a better chance of being selected.

It is free and takes less than 2 minutes!

Go to www.missionsmallbusiness.com.
Click on the “Login and Vote” button.
Put “Cyber Sprocket” in the search box and click search.
When our name comes up click on the vote button.

 

And now on to our article…

C# Programming

Introduction

While coding a new Windows desktop app for a client we ran into something that we thought we be fairly simple.  It has turned out to be a rather complex task. The goal: detect when a SmartDongle USB key was being inserted or removed & update our application interface at that time.

Our assumption was that a few minutes of searching the Internet and we’d find a dozen examples of people that have done it already or we’d just use the .Net 4.0 classes for device detection. Wrong on both counts. As ubiquitous as USB devices are these days, it appears that .Net 4.0 still does not have direct support via easy-to-use classes that you can just hook into. Searches of the Internet turned up a lot more people asking questions than viable answers. The few answers we found were outdated or half-answers.

Now that we’ve got basic USB insertion/removal detection working we decided we’d share that part of the solution. Our next challenge is to determine that we’ve inserted the SmartDongle and not some other USB device such as a keyboard, mouse, flash drive, or other USB item. Unfortunately the message handler we’ve got thus far only tells us that a device has changed (windows message WM_DEVICECHANGED), but does not hand off the parameters that would let us query the message stack to find out WHICH device changed. That will be a solution for another post I guess.

Our Environment

We are coding in C# using Visual Studio 2010 (VS2010) with the .Net 4.0 framework helping us along. We are coding a WPF main window, not a form, which can make a difference.

Our Solution

Here is the simplified way to detect that a USB device was inserted or removed from the system, and our call to a ReadDongle method that we wrote to get information from the dongle header (or not, if it was removed). You can call whatever function you’d like, or put your processing loop right inside the windows process handler.

using System.Windows.Interop;
...
public partial class MainWindow : Window
 {
    ...
    public MainWindow()
    {
    ...
    }

    //============================================================
    // WINDOWS MESSAGE HANDLERS
    // 

    private const int WM_DEVICECHANGE = 0x0219;  // int = 537
    private const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 0x00000004; 

    /// <summary>
    ///
    /// </summary>
    /// <param name="e"></param>
    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);
    }

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == WM_DEVICECHANGE)
        {
            ReadDongleHeader();
        }
        return IntPtr.Zero;
    }

}

Summary

That’s it, now we know when a device is inserted or removed. We are overriding the OnSourceInitialized method for our main window, which basically is saying “hey, when the window is initialized and ready to accept external messages from the operating system, add a hook that latches onto the window’s message processing method (WndProc). We can then perform operations by sniffing out specific types of messages like “device changed” (WM_DEVICECHANGE) and doing our thing. When the WndProc override is done it drops back to the default method for further processing. We could override that by setting handled to true, but we don’t want to do that in our app… not yet anyway.

Hope our article helps provide a quick shortcut for someone that is googling stuff like “C# wpf usb insertion/removal”.

Posted on

Logistics & Inventory Management with Alutiiq

Alutiiq recently award Cyber Sprocket a 1-year teaming agreement, making this our 4th year of working with Alutiiq on developing, supporting, & maintaining their military logistics & inventory management application.  We are very excited to be working with Alutiiq for another year.  The upcoming year will bring some exciting new possibilities for follow on projects that augment the system already in place.  We’re looking forward to being part of the design & development team and supporting our clients and our country both at home and abroad.

Technical Overview

Services Provided

  • Web Application Programming
  • Database Design
  • Database Maintenance
  • System Architecture
  • Network Support

Platform Details

Posted on

Microsoft .Net Framework

Cyber Sprocket and the .Net Framework

Many of the Cyber Sprocket desktop applications require the Microsoft .Net Framework.

Older windows computers do NOT have the .Net Framework installed. All new windows programs that access the Internet will require the .Net Framework, and thus it is a component of newer Windows operating systems. If you are running an older PC and have Windows updates enabled you most likely have the .Net Framework. New XP, Vista and Windows 7 computers come with the .Net Framework by default.

If you are not sure if you have the .Net Framework installed you can skip this step and just begin the program installation, it will tell you if you need to install the .Net Framework during the setup process.

The .Net Framework has several versions, the minimum version required for most Cyber Sprocket applications is version 1.1. We recommend installing version 2.0.

What Is The .Net Framework?

The Microsoft .NET Framework is a software component included with the Microsoft Windows operating system. It provides a large body of pre-coded solutions to common software development requirements, and manages the execution of programs written specifically for the framework. The .NET Framework is intended to be used by most new applications created for the Windows platform.

The pre-coded solutions that form the framework’s Base Class Library cover a large range of programming needs in areas including: user interface, data access, database connectivity, cryptography, web application development, numeric algorithms, and network communications. The class library is used by programmers who combine it with their own code to produce applications.

Programs written for the .NET Framework execute in a software environment that manages the program’s runtime requirements. This runtime environment, which is also a part of the .NET Framework, is known as the Common Language Runtime (CLR). The CLR provides the appearance of an application virtual machine, so that programmers need not consider the capabilities of the specific CPU that will execute the program. The CLR also provides other important services such as security mechanisms, memory management, and exception handling. The class library and the CLR together compose the .NET Framework.

The .NET Framework is included with Windows Server 2003, Windows Server 2008 and Windows Vista, and can be installed on most older versions of Windows.

.Net v1.1

If you do not have the .Net Framework already installed on your PC you can obtain the program in one of two ways:

  1. Download the dotnetfx.exe file from Cyber Sprocket, then double click the dotnetfx.exe that has been downloaded.
  2. Visit the Microsoft Download Center to download the latest version.

.Net v2.0

If you do not have the .Net Framework already installed on your PC you can obtain the program in one of two ways:

  1. Download the dotnetfx.exe file from Cyber Sprocket, then double click the dotnetfx.exe that has been downloaded.
  2. Visit the Microsoft Download Center to download the latest version.

Issues With Microsoft .Net Framework

Repair May Be Needed

The .Net Framework Indicates Repair May Be Needed
The following is documentation from the Microsoft .Net Framework:

Installation Repair

You may need to repair your installation of the .NET Framework after upgrading your operating system or if the system becomes corrupted.

To repair the .NET Framework

  1. Obtain the original installation source. For example, if you installed the .NET Framework from CD or DVD, insert the disk. Or, if you downloaded the .NET Framework, download again and choose to save to disk. If you installed from a network share, reconnect.
  2. On the Start menu, choose Run.
  3. For Windows 98 and Windows Me type:
    command

    For Windows NT, Windows 2000, Windows XP or later, type:

    cmd
  4. In the command window, type the following:
    n:<Installation Source>dotnetfx.exe /t:%temp% /c:"msiexec.exe /fvecms %temp%netfx.msi"

    For example:

    d:dotNetFrameworkdotnetfx.exe /t:%temp% /c:"msiexec.exe /fvecms %temp%netfx.msi"

To repair a .NET Framework Language Pack

  1. Obtain the original installation source. For example, if you installed the .NET Framework from CD or DVD, insert the disk. Or, if you downloaded the .NET Framework, download again and choose to save to disk. If you installed from a network share, reconnect.
  2. On the Start menu, choose Run.
  3. For Windows 98 and Windows Me type:
    command

    For Windows NT, Windows 2000, Windows XP or later, type:

    cmd
  4. In the command window, type the following:
    n:<Installation Source>langpack.exe /t:%temp% /c:"msiexec.exe /fvecms %temp%langpack.msi"

    For example:

    d:dotNetFrameworklangpack.exe /t:%temp% /c:"msiexec.exe /fvecms %temp%langpack.msi"

Determining Your .Net Framework Version & Service Pack Level

The following is from the http://support.microsoft.com/ .Net Framework knowledgebase:

Microsoft Support Article 318785

SUMMARY

This article describes how to determine whether service packs are installed for your Microsoft .NET Framework installation. For additional information about .NET Framework service packs, click the following article number to view the article in the Microsoft Knowledge Base: 318836 How to obtain the latest .NET Framework service pack

MORE INFORMATION

Use MMC 1.2 to determine the version of the .NET Framework that is installed

The .NET Framework Configuration tool, a Microsoft Management Console (MMC) snap-in, exposes the full version number, including service packs of the existing installation of the .NET Framework. This tool is only available on operating systems that have MMC 1.2 and later versions.

If your operating system is Microsoft Windows 98, Microsoft Windows Millennium Edition (Windows Me), or Microsoft Windows NT 4.0, you can download MMC 1.2 from the Microsoft Download Center.

The following file is available for download from the Microsoft Download Center:

Download the Immc.exe package now.

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to obtain Microsoft support files from online services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

If MMC 1.2 or a later version is not installed, you can locate the version number of the .NET Framework by viewing the file versions. For more information, see the “Determine the version of the .NET Framework without MMC 1.2” section.

Determine the version of the .NET Framework on a computer that is running Microsoft Windows XP
  1. Click Start, and then click Control Panel.
  2. In Classic view, double-click Administrative Tools.
  3. Double-click either Microsoft .NET Framework Configuration or Microsoft .NET Framework 1.1 Configuration.
  4. In the .NET Framework Configuration window, click About .NET Framework Configuration on the Help menu.

The .NET Framework version number appears in the About .NET Framework Configuration dialog box. If the version number is 1.0.3705.0, you have version 1.0 without any service packs installed.

Note A bug exists in the installation process of the .NET Framework 1.1 SP1. The information in the detail pane and in About .NET Framework Configuration on the Help menu is not updated. It appears as if SP1 was not installed. To verify the installation of SP1, follow the steps in the “Determine the version of the .NET Framework without MMC 1.2” section.

Determine the version of the .NET Framework on a computer that is running Microsoft Windows 2000
  1. Click Start, and then click Run.
  2. In the Open box, type mmc.exe, and then press ENTER.
  3. In the Console1 window, click Console, and then click Add/Remove Snap-in.
  4. In the Add/Remove Snap-in dialog box, click Add.
  5. In the list of available snap-ins, select .NET Framework Configuration or .NET Framework 1.1 Configuration.
  6. Click Add, and then click Close.
  7. In the Add/Remove Snap-in dialog box, select .NET Configuration in the list of added snap-ins. The About button becomes available.
  8. Click About.

The .NET Framework version number appears in the About .NET Framework Configuration dialog box. If the version number is 1.0.3705.0, you have version 1.0 without any service packs installed.

Note A bug exists in the installation process of the .NET Framework 1.1 SP1. The information in the detail pane and in About .NET Framework Configuration on the Help menu is not updated. It appears as if SP1 was not installed. To verify the installation of SP1, follow the steps in the “Determine the version of the .NET Framework without MMC 1.2” section.

Determine the version of the .NET Framework without MMC 1.2

If your operating system does not have MMC 1.2 installed, follow these steps to verify the version of the .NET Framework that is installed. To check the service pack level of the MSI version of the .NET Framework 1.0, start Registry Editor, and then locate the following registry key:

Key Name: HKEY_LOCAL_MACHINESoftwareMicrosoftActive SetupInstalled Components{78705f0d-e8db-4b2d-8193-982bdda15ecd}
Value: Version
Data type: REG_SZ
The OCM version of the .NET Framework is included with Microsoft Tablet PC, Microsoft Media Center, and Microsoft Windows XP Embedded only. For the OCM version of the .NET Framework 1.0, start Registry Editor, and then locate the following registry key:

Key Name: HKEY_LOCAL_MACHINESoftwareMicrosoftActive SetupInstalled Components{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}
Value: Version
Data type: REG_SZ
For both these registry values, the data is in the following format:

1,0,3705,x
The x in this data represents the service pack level.

The .NET Framework version 1.1

With the .NET Framework 1.1, a new registry hive has been created specifically to make it easier to find the service pack level. Start Registry Editor, and then locate the following registry key:

Key Name: HKEY_LOCAL_MACHINESoftwareMicrosoftNET Framework SetupNDPv1.1.4322
Value: SP
Data type: REG_DWORD
The data in the SP value tells you which service pack is installed for the .NET Framework 1.1. For example, if the value of SP is 0, no service pack is installed for the .NET Framework 1.1. If the value is 1, Service Pack 1 for the .NET Framework 1.1 is installed.

Note that this method cannot be used to detect if any hotfixes are installed for the .NET Framework 1.1.