Posts tagged .Net


FancyBox – Make your images dance!


…Well not literally but hey a little bit of hyperbole never hurts right 😀

Well on to the topic, I was making a form which requires me to have a GridView (it sure is a great boon for dotNet developers) which has a few columns and one of them is a TemplateColumn containing an Image control which is bound to a column from a database having the image name.

Now if you have worked with GridView anytime, then you would remember, it would look ugly if the image size is kept big and it would just not fit the purpose if kept small. So we need a mechanism which would allow us to zoom the image some way and thats exactly where FancyBox comes in picture.

FancyBox is tool for displaying images, html content and multi-media in a Mac-style “lightbox” that floats overtop of web page. It was built using the jQuery library.

That was straight from the horse’s mouth. If you still havent got wat is it all about then click on the image below..

Isnt she a cutie?

Isnt she a cutie?

Now wasnt that a nice effect to have on a image? Well you can have it on your webpages too and that to Very Easily. Ya and I will prove it to you 🙂

Download the latest version of FancyBox from here. It includes the latest version of JQuery scripts so you wont need to download them.

Lets do a step-by-step tutorial shall we?

I wont be showing on how to do it with normal html pages. Thats very simple and can be learn from here.

If you are not familiar with jQuery, please, read at least this tutorial for beginners

Now for using FancyBox in a GridView in an ASP.NET form, lets start by creating a simple form.

The output might be something like this…

< %@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="WebRnD1._Default" %>

    Untitled Page


The codes give here are just examples, and your codes can vary. We have created an Form stating Now go and add the below code in between the form tags.


We have just added a GridView control to our form having two columns, both of them acting as TemplateFields. The first displays a hyperlink wrapping around an image and second column displays a label for the image.
Our concern is the first column which contains…

Though looks so simple, this is our key for the FancyBox. We are simply creating a hyperlink which will be searched by the FancyBox script and on clicking the image it will popup with the image linked using the href attribute. Aint that so simple?? Yeah, the form is ready, all we need to do is add the script. Lets do that…

Add a script tag in the head element like this..

    Untitled Page

In the above snippet, we are adding a reference to two script files, one is the jquery script which allows us to fantastically work with the elements of our page and the second is the fancybox script which will work its magic on the hyper-images. We are also adding a reference to a css file which manages how the popup is displayed i.e. at the centre of the screen.
Then comes our main code which makes it all work. Lets debug it step by step.

$(document).ready(function() {......});

The above line is instructing to get the document object and when its ready call and anonymous function which will…

$("a#oneimg").fancybox({ ...... });

Get all the anchor tags with id as single and apply the fancybox function on them with the specified parameters. To know what these parameters do and what can be used refer here.

Thats it, we are done. Put it all together and run and you will get a effect like the one which you saw above. (Btw, I hope Miley Cyrus likes that dance :D)

Another problem which I have faced is when you are databinding a GridView with lots of rows containing images. In that case the fancybox works on the first image but not on the rest. The reason is because when you query for an anchor tag using ID you will get only one elements. Thats what ID’s are for right. To keep the elements unique. But what if you want to apply the effect on all the images? Simply apply a dummy class attribute and use the class to get elements in jquery. Here is how…

Look closely, and the only thing which I have changed here that instead of id, now I am using class attribute and also used the part of databinding the value.

After that you need to modify your jQuery a bit by asking it to get elements using the class and not id like this…

$("a.oneimg").fancybox({ ...... });

Yeah, that does the trick!! If you are looking for to implement this in your WordPress blog then you can get this plugin : FancyBox for WordPress

Now let the party begin!! Who let the dogs out, woof woof woof 😀


Working with ServiceController in .NET


Operating systems that are member of the Windows family, especially Windows NT, Windows 2000 and now Windows XP and .NET Server, have many different tasks running as services within them. These services have various states i.e. running, stopped, paused, etc. and depending upon the need the status of these services can also be altered. In this article I will be introducing you to the ServiceController class in the .NET framework, which allows developers to create instances and then interact with a specific Windows service.

Introducing the ServiceController class

The complete namespace for the ServiceController class is System.ServiceProcess.ServiceController. Initiating an object of this class creates a connection between the Windows service and the object which can then be used to start, stop and interact with the underlying Windows services.
The ServiceController object can also be used to send custom command to the services, through a Web Form. This means that Windows and ASP.NET developers can easily start, stop or do other types of interactions with the Server Services. However, one thing should be brought to notice and that is that the ServiceController object is not itself actually responsible for starting, stopping, pausing or implementing any other action on the service. This object passes on the request to the Service Control Manager and returns. The Service Control Manager is then responsible for actual implementation of the action or command sent. Therefore errors that are to be concerned with the failure of starting a service or stopping it and etc. must be handled within the service itself.

Creating a ServiceController object

Initiating a ServiceController object is similar to initiating of other objects in .NET. However the ServiceController object requires two properties to be set. These properties are of MachineName and ServiceName. The following listing shows how to create an instance of the ServerController class.

Dim objServiceController As New      ServiceController
'Setting      properties of the ServiceController object
objServiceController.MachineName = "Impact"
objServiceController.ServiceName = "Spooler"

Here the first property contains the name of the machine i.e. the computer on which you want to interact with the service. This could be the name of the local computer as well as a remote computer. Since my computer’s name is Impact I have set the machine name to it. If this property is not set the default is local, which means that your local computers services will be available to the object. The next property ServiceName requires you to provide a valid name of the service with which you wish to interact. This service must be available in the Service Control Manager. You can view a list of services from the Service Control Manager(SCM), Microsoft Management Console (MMC) snap-in. Here in the listing above, I have provided the name of spooler service.
Having done this, you have created object that now refers to the Spooler service in the operating system. You can now interact with it in any desired way. For demonstrating a simple example I will now find out the status of this service, to see either its running, stopped or paused. For that I have created the following method.

Private Function      GetServiceStatus(objService As ServiceController) As String
Select Case objService.Status
Case ServiceControllerStatus.Stopped
Return "Service is stopped."
Case ServiceControllerStatus.Paused
Return "Service is paused."
Case ServiceControllerStatus.Running
Return "Service is running."
Case ServiceControllerStatus.ContinuePending
Return "Service is continue pending."
Case ServiceControllerStatus.PausePending
Return "Service is pause pending."
Case ServiceControllerStatus.StartPending
Return "Service is start pending."
Case ServiceControllerStatus.StopPending
Return "Service is stop pending."
End Select
End Function

The complete code listing would now be as follows.

<!--      Reference to the Assembly -->
<%@ Assembly Name="System.ServiceProcess" %>

<%@      Import Namespace="System" %>
<%@ Import Namespace="System.ServiceProcess.ServiceController" %>

<Script Language="VB" Runat="Server">

Public Sub Page_Load(Source As Object, Sender As EventArgs)

Dim objServiceController As New ServiceController

'Setting properties of the      ServiceController object
objServiceController.MachineName = "Impact"
objServiceController.ServiceName = "Spooler"

'Call the function to display status of the service
Label1.Text = GetServiceStatus(objServiceController)

End Sub

Private Function GetServiceStatus(objService As ServiceController) As String


End Function


<FORM Runat="server">
<asp:Label id="Label1" runat="server"/>

I have created the GetServiceStatus( ) function to retrieve the status of the service in a descriptive text format, otherwise you would get numerical values for calling the Status property of the ServiceController object since its an enumeration.
Save this page as ServiceController.aspx and run it. The following figure shows the resultant output in the browser.

(Figure displaying the status of the Spooler Service)

This article introduced you to the System.ServiceProcess.SystemController class. You were told about how you can create an instance of the class and using it interact with the Windows operating system services. You were also demonstrated through an example to create a SystemController object and then find out the status of the Service to which that object referred.


A little about Reflection in .NET


Reflection in .NET is an exciting new tool for most developers. This article shows you how to use reflection to discover objects at runtime that you did not know existed at design time, create an instance of those objects, and execute methods of those objects.

What Is Reflection?
Reflection is a means of discovering information about objects at runtime. This information can be used to execute methods and retrieve property values of these objects in a dynamic manner. Code written for the .NET Framework automatically reflects, or describes, itself. It does so via compiler-generated metadata. You can therefore use reflection in your applications to query managed code about its types (including class names, methods, and parameters) and then interact with this information—this includes executing the discovered code. You can also use reflection to generate code at runtime, compile it, and run it.

What’s the Point of Reflection?
The most obvious example of reflection is a type or object browser. Think of the Visual Studio .NET object browser. This utility can display classes exposed by an assembly, methods on those classes, parameters for those methods, etc. (see Figure 1 for an example). In the past, some of this information was available to us via COM type libraries. With .NET this application data is available from the assembly itself. All .NET assemblies are self-describing—that is, they contain metadata about their types. You can query this metadata to learn about a given object.

Reflection in .NET

Reflection in .NET

Looking around the .NET Framework and the various .NET tools it quickly becomes apparent that .NET itself makes extensive use of reflection. The compilers use it; other namespaces such as Remoting use it, etc. If you are like us though, you’re wondering, “What can I do with reflection in my applications?”

There is no doubt that reflection does not solve everyday tasks associated with creating your average business application. You will not find yourself importing (or using) the Reflection namespace like you would the System.IO or System.Data namespaces, for example. However, once you have a solid understanding of just what reflection does, it won’t be long before you’ll see a number of very useful scenarios. In fact, the following are all good candidates for using reflection:

* API help reference based on reflection and custom metadata
* Type browsers and other products that work with compiled code
* Testing tool that looks at an object, builds a form, and allows users to execute the code similar to a .NET Web service’s .asmx file
* Applications that generate code
* Centralized exception handling, logging, and reporting
* Anytime you require late binding
* Workflow-like applications that allow users to connect components (think of a sales pipeline)

From just this short list you can already see that reflection offers a great deal of power to developers. Let’s get started by looking at the basic manner in which you query for reflected information.

Reflection Discovery Basics
When working with reflection your code will typically follow a basic design pattern. You first need to load an assembly’s metadata. You then search through the information to find the type or types in which you are interested. Finally, you either present this information for display or execute directly against a found type. Let’s take a look at how this plays out in code.

You must first create an instance of the Reflection.Assembly class based on a known type or an assembly. We do so not with a constructor but with static (i.e. shared) “Load” methods of the Assembly class. There are a number of versions of these methods. For this example, we will focus on the following two:

1. Load(assemblyString As String) As Assembly
2. LoadFrom(assemblyFile As String) As Assembly

Both of these methods return an instance of the Assembly class. Assembly.Load takes assemblyString as a parameter. This parameter represents the name of a loaded (in the current application’s domain) assembly. For example, if you create a form-based application and then list all loaded assemblies, one of them will be “System.Drawing” which is the name of an assembly (and namespace) used by the .NET forms engine. Therefore to get an instance of this loaded assembly, you would write the following code (in VB):

Dim myAssembly As [Assembly]
myAssembly = [Assembly].Load("System.Drawing")

You can also attach to your own loaded assembly. That is, your code can read reflected information about itself. To do so, you would simply use the same Load method above but replace “System.Drawing” with the name of your assembly. (See Sidebar: Application Domains)

Assembly.LoadFrom works in a similar fashion. However, this method takes a string parameter that points to a .NET assembly via its path and file name. LoadFrom gives you the power to work with any .NET assembly and not just those loaded into the current application domain. The following is an example:

Dim myAssembly As [Assembly]
Dim myPath As String
myPath = "C:\Basics\Bin\Basics.exe"
myAssembly = [Assembly].LoadFrom(myPath)

Visual Basic developers can also use the keyword GetType to load an assembly. This operator takes a type as a parameter and returns a given type, which in turn provides access to its assembly data. The following is an example of GetType:

myAssembly = GetType( _

Of course, the .NET Framework also provides a GetType method that is available in all objects written for .NET. You can use this method as well to return a type’s assembly. For example:

Dim test As New MyTestClass()
myAssembly = test.GetType().Assembly

Now that you’ve seen numerous ways to create an instance of Reflection.Assembly, let’s look at how to dig into an assembly and query the information it contains. To do so, you’ll make extensive use of the System.Type class, which is used to represent all .NET types (classes, enumerations, arrays, etc.). You will use an Assembly instance to return Type instances that represent all the types in a given assembly. For example, suppose you have the following console application:

Imports System.Reflection
Module Basics
Public Enum testEnum
testValue = 1
End Enum

Sub Main()
'do something
End Sub
End Module

There are two types defined here at the assembly level: the module Basics and the enumeration testEnum. To access these types you call Assembly.GetTypes. This will return an array of Type objects that represent the loaded assembly’s types. For example, add the following code to the Sub Main of the previous code snippet:

Dim myAssembly As [Assembly]
myAssembly = [Assembly].Load("Basics")
Dim myType As Type
For Each myType In myAssembly.GetTypes
' do something

You’ve obtained a reference to the types from the assembly with myAssembly.GetTypes and are now able to iterate through those types. You’ll use these type objects to make decisions in your code. For example, you could add the following check inside the For Each…Next loop:

If myType.IsClass Then
End If

You’ve now successfully loaded an assembly, iterated its types, and displayed all of its classes. This represents the basic pattern you follow to work with reflected metadata on an assembly. The rest of the article will dive deeper and show you what more to do with the various Reflection classes.

.NET 3.5 Enhancements Training Kit


Microsoft has recently released an updated training kit with presentations, hands-on labs, demos and event materials describing features found in .NET 3.5 SP1 and Visual Studio 2008 SP1. Here you will find over 100 MB of information with tips on using MVC, Ajax, the Entity Framework, Visual Studio 2008 SP1 and other tools. Materials used world wide in the .NET 3.5 SP1 training tour are also included. You can read more details here. In the download section shown below, I also include a link to the VS 2008 training kit released last Fall.


VS2008 and .Net 3.5 SP1 Download!


Visual Studio Service Pack 1 and .NET 3.5 Service Pack 1 are now available for download. A new feature for C# developers called squiggles provides better error messages while you are typing your code. There are several important LINQ performance enhancements, including one that should yield a 50% speed increase in some common scenarios. There are also thousands of other bug fixes.

A general description of these releases is available here. It describes Visual Studio and .NET 3.5 changes such as:

  • Improvements to the WPF designers, and core WPF performance.
  • SQL 2008 support
  • Entity Framework support.


A download page is available here, or you can just follow these links:

VS 2008 Service Pack 1:

VS 2008 Express Editions with Service Pack 1:

VS Team System 2008 Team Foundation SP 1:

.NET Framework 3.5 Service Pack 1:

Go to Top