How to specify Triggers’ execution order in SQL Server 2005/2008

By Mohammad Mahdi Ramezanpour at February 27, 2010 03:19
Filed Under: SQL Server

A trigger is a database object that is attached to a table. In many aspects it is similar to a stored procedure. As a matter of fact, triggers are often referred to as a "special kind of stored procedure." The main difference between a trigger and a stored procedure is that the former is attached to a table and is only fired when an INSERT, UPDATE or DELETE occurs. You specify the modification action(s) that fire the trigger when it is created.

The following shows how to create a trigger that displays the current system time when a row is inserted into the table to which it is attached:

SET NOCOUNT ON

CREATE TABLE Source (Sou_ID int IDENTITY, Sou_Desc varchar(10))
go
CREATE TRIGGER tr_Source_INSERT
ON Source
FOR INSERT
AS
PRINT GETDATE()
go
INSERT Source (Sou_Desc) VALUES ('Test 1')

-- Results --

Apr 28 2001 9:56AM

In some situations, you may want create more than one trigger on table. By default, all triggers execute at the same time and if there was a dependency between your triggers, you’ll get an exception. In this situation you have to execute triggers one by one and in an order.

In SQL Server 2005, Microsoft introduced a system stored procedure name “SP_SETTRIGGERORDER” that do whatever you want.

Note: In “SP_SETTRIGGERODER”, you can set the first and last trigger to execute; so if you have more than tree triggers in a table, you won’t be able to set all triggers’ orders.

The structure of “SP_SETTRIGGERODER” system stored procedure is like this:

USE AdventureWorks;
GO
sp_settriggerorder @triggername= 'Sales.uSalesOrderHeader',
@order='First', @stmttype = 'UPDATE';

Note: You CANNOT use SP_SETTRIGGERODER for INSTEAD OF triggers.

Hope it helps.


My portal is almost done!

By Mohammad Mahdi Ramezanpour at February 24, 2010 12:21
Filed Under: System.Web.Portal

A year ago, I posted about a portal that I was going to develop and its name was System.Web.Portal. In that post I said the portal is based on ASP.NET Web Forms, LINQ (especially LINQ to SQL) and some other technologies. Unfortunately, because of the situation I had, I couldn’t continue the project’s development until November 2009.

Since that time, I started to develop this application and realized that some features that I was planned to implement is not popular these days and if I develop in that way, the portal won’t have any differences with other portals in the market; so I decided to develop this application in another way. In this post I want to describe the new System.Web.Portal.

When I started to develop this portal, I checked lots of portals out and found out the following:

  • Nearly all portals are based on databases such as SQL Server, SQLite, VistaDB and so on.
  • Most of portals’ designs are old and they aren’t using new designing technologies yet (In a sentence, they’re not Web 2.0 yet)
  • When you look at the source code of those portals, you can’t understand because the code is NOT clean.
  • Module creation on those portals is like pain in the neck and it’s not so easy.
  • And a lot more leakages…

I decided to cover all of leakages above and something which is interesting so here’s my main goal on developing System.Web.Portal:

  • System.Web.Portal (SWP) has written based on .NET Framework 3.5 or above. Also, it’s working fine on Mono 2.6.1 or above. This feature enables you to install this portal on almost any server including Windows, LINUX or ever MAC OS servers.
  • SWP is completely based on standard XML and there’s no database at all! So the user can install it on every server that supports .NET Framework 3.5 or Mono 2.6.1 or above without any database need. By the way, I’m going to release providers for SQL Server too. So if you want to have SQL Server backend, you can download this patch and install it on your own application.
  • One of the technologies that I really like is Web 2.0. It’ll improve website’s performance because it uses less HTML elements and there’s no “Table”, “Tr” and “Td” tags which I hate. SWP is completely based on Web 2.0 technology and I think it’s one of the advantages of this product. Here’s some other features of Web 2.0 represented by Andrew McAfee:

    Search
    Finding information through keyword search.

    Links
    Connects information together into a meaningful information ecosystem using the model of the Web, and provides low-barrier social tools.

    Authoring
    The ability to create and update content leads to the collaborative work of many rather than just a few web authors. In wikis, users may extend, undo and redo each other's work. In blogs, posts and the comments of individuals build up over time.

    Tags
    Categorization of content by users adding "tags" - short, usually one-word descriptions = to facilitate searching, without dependence on pre-made categories. Collections of tags created by many users within a single system may be referred to as "folksonomies" (i.e., folk taxonomies).

    Extensions
    Software that makes the Web an application platform as well as a document server.

    Signals
    The use of syndication technology such as RSS to notify users of content changes.

  • You can ever begin to figure out how easy is to create module for this portal. I really happy about it because whenever a customer needs a specific module, I can develop that module very fast and easy. I’m going to write a new post about creating module on SWP but for now I can tell you that it’s easy like a piece of cake ;-)
  • One other feature that I really like to talk about in this post is Theme creation. Creating theme in SWP is very easy! You don’t need to write any code at all. All you need to do is to go to the administration panel and create a new theme. With a little knowledge of HTML and CSS, you can create your own theme for your website (I’ll describe more at a later date).

There are more dedicated features that I really want to inform you about but I think I should explain those in series of posts.

Version:

Obviously, the first version will be beta 1 (0.1). I’ll do my best to release the first major version (1.0) in 3 months.

So, when you’re going to experience it:

I’m going to upload this project to the CodePlex as an Open-Source project in a month so you can download it for free!

Thanks for reading.

How to change Windows Desktop Wallpaper using C# and Windows APIs

By Mohammad Mahdi Ramezanpour at February 23, 2010 04:05
Filed Under: .NET General

It’s about a week that I’m working on an advertising application which manages customer’s advertising programs. One of the cool features that my customer requested was to change user’s desktop wallpaper into a specific photo or add some advertising notes to the current wallpaper.

So I started developing this application and faced something: “How to programmatically change Windows Desktop Wallpaper?

As you know, to change anything in Windows, you have to make use of Windows APIs. This one is the same. In order to change anything in Control Panel, There is an API function, named “SystemParametersInfo” :

The SystemParametersInfo function retrieves or sets the value of one of the system-wide parameters. This function can also update the user profile while setting a parameter.

How it works:

First of all you need to import System.Runtime.InteropServices namespace. This namespace enables you to import your favorite Windows API function library.

Note that SystemParametersInfo function is in User32.dll library.

To import a Windows API library in C#, it’s necessary to use “DLLImport” attribute:

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction,
int uParam,
string lpvParam,
int fuWinIni);
Now you have the function, so you have to use it somewhere:
 
string path = @"C:\Users\Public\Pictures\Sample Pictures\Desert.bmp";
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;

SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
path,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

Note that, the file you specify to be the wallpaper must be in Bitmap format. If you have JPEG, PNG, etc. files, you have to convert it to Bitmap, save it in a temp folder and then set it as Windows wallpaper.


Currently Reading

Quote of the day

Send Persian SMS