String.IsNullOrWhiteSpace in .NET Framework 4

By Mohammad Mahdi Ramezanpour at March 03, 2010 03:13
Filed Under: .NET General

Microsoft .NET New Logo As you know, String class in .NET Framework contains lots of methods and properties that related to strings. One of the popular methods that we had in previous versions of .NET (3.5, 2.0, etc.) was String.IsNullOrEmpty that indicates whether a String variable contains a string or it’s Null (Nothing in VB). I’m using this method a lot of time in my project it becomes one of my favorite methods in .NET.

But what if you want to check if a String variable is Null or is contains White spaces! It’s something that I wanted to explain in this post.

In Visual Studio 2010, Microsoft introduced a new method in the String class. The method is String.IsNullOrWhiteSpace that Indicates whether a specified string is null reference (Nothing in Visual Basic), empty, or consists only of white-space characters.

How it works:

The following code shows you how to use String.IsNullOrWhiteSpace method:

using System;

public class Example
{
public static void Main()
{
string[] values = { null, String.Empty, "ABCDE",
new String(' ', 20), " \t ",
new String('\u2000', 10) };
foreach (string value in values)
Console.WriteLine(String.IsNullOrWhiteSpace(value));
}
}
// The example displays the following output:
// True
// True
// False
// True
// True
// True

For more information please visit: http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace%28VS.100%29.aspx

Hope it helps.

Most of programmers CANNOT sleep on time

By Mohammad Mahdi Ramezanpour at March 02, 2010 11:37
Filed Under: Other

FUNNY! A computer addict. If you’re a programmer, I think you should agree with it. Most of programmers are used to sleep late and wake up soon because they’re busy with a machine named computer. Programmers are part of society who isn’t good sleepers and I think it’s totally because they’re in touch with computers. So the main question is why the computer is so addictive.

An anonymous user just asked a question:

Q: Why Is The Computer Or Internet So Addictive?

A: That is a very good question. Computer and Internet are the inventions of the modern age. They hold every kind of information and data you need to know. Mostly it helps you to do school projects and homeworks. People prefer it because it is fast and efficient. Nowadays no one would like to visit the library with heavy books and a lot of searching when you can just do all that with a click of a button while relaxing in your arm chair. You can play different games on computer and talk to anyone in the world at any time of the day. Personnel stuff or official, you can do all that in the computer with fast access to everything. While you use the computer the whole world is at your fingertips every kind of stuff. In one sentence people of every time and age preferred quick job with no or little hard work and especially the young generation, so why would they go to the library (even it is important for you as books are your best-friends) while you can do it all easily at your cosy homes. So dear friend these are the basic reasons for the addiction of Internet and computer at the present time!

Another answer which was so interesting is this:

Video Game Addiction A: Because growing has no space in it any more. No time when nothing is placed into the mind, so kids can just kind of dream or imagine. Even reading books, you got to fill in the spaces. Now the "writers" from everywhere--Disney, the Simpson's, etc., All offering fascinating, funny and often very original stuff, take up all the empty space. Most school programs offer very little in the arts of music for free, having decided these things are not essential--despite an astounding amount of evidence demonstrating that studying music enhances one's desire and understanding of math. And writing one's own poetry, or hikes, or anything creative, would create a greater desire to care about what you're "supposed to" read. In a conference on "Men and the Life of Desire," 1991, available on audiotape, Michael Meade, Robert Bly and James Hillman discussed the repression of desire in our society. Men are not encouraged. It makes them less controllable, and let's face it. SOMEONE wants us more controlled. We need to rehabilitate imagination. Then the computer would be less addictive, and so would TV and other media. I think it requires a connection to nature at a young age. Take 'em to the hills; let the kids make something, even badly, and feel good about it. Praise them for what they do with their minds and hearts and hands. Help addict them to their own creativity. It's not hopeless.

computer-cartoon-freak-copyright8 Like other kinds of people, programmers can’t live without computer. Most of them spend about 12 hours a day working with it and prefer to spend their time with their machines and nothing more. They’re addicted like no one else and can’t quit because they’re doing businesses with it too.

Nearly all programmers prefer to work with computer in silence. They don’t like noise!

Warning: If you have a programmer wife/husband, please DONOT become involved with them when they’re working! Because they’ll get all bent out of the shape and you can’t control the situation :D

So the best way to achieve silence is to work at night (especially with everyone in the house slept).

But, is this the way to live?

Never ever!!! After some time of working with computer at night (12 AM – 6 AM) you can’t manage your sleep time anymore and most of your friends and especially family can’t stand you. After sometime, you’ll lose your mood and sociality! The best way to be a good programmer is to go to sleep at last at 1AM and wake up at 7AM and that’s it.

Conclusion

If you want to be good man of your life, it’s better to manage your sleep time. It’s never good when you sleep when others are awake or being impatient, sleepy and more. Quit this addiction a start a new life as soon as possible. If you don’t, you’ll lose your life after some time. I know people who’re got divorced just because they were addicted to the computer. Trust me if you’re a man: “No woman can stand men who’re working at home. They want you to spent time with them instead of computer! Some of them are jealous of your relationship with you and your machine.” I’m sure about it.

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.


Quote of the day