Inline If Statement

Standard IF statements are great. But I found that when I am using very simple conditions within my IF Statement I waste a lot of space in my code.

Example 1:

public int IfExample(bool bolFlag)
{
int result = 0;
if (bolFlag)
{
result = 1;
}
else
{
result = 2;
}
return result;
} 


There is no problem using Example 1. But there is a much quicker and less tedius way of using a simple conditional IF statement.

Example 2:

public int IfExample(bool bolFlag)
{
return (bolFlag) ? 1 : 2;
}

This example is basically saying: "return if (bolFlag) 1 else 2". So you are in theory assigning the IF condition to the variable.

I know it takes a little while to understand the syntax. But it is really good for those less complex IF conditions.

C# NULL Coalescing Operator

Here is a really neat trick that a mate showed me at work. It is a way to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with perhaps another value.

For example, lets assume we have an integer variable "intCounter". We could check if the integer contents was null, and if not return another value: 

int intCounter = 5;
int intResult = intCounter ?? 0;
// Output: intResult == 5 

We got the following output because intCounter did not contain a null.

The C# NULL coalescing operator also works with string variables as well:

string strMessage = "Hello World";
string strResult = strMessage ?? "No message";
// Output: strResult == "Hello World" 
So we can see from the two examples above that this NULL Coalescing operator is quite useful. Here is an example of what would have happened if a variable was null:
string strMessage = null;
string strResult = strMessage ?? "No message";
// Output: strResult == "No message" 

Scott Guthrie talks more about this in his own blog and how to use this feature in LINQ: http://weblogs.asp.net/scottgu/archive/2007/09/20/the-new-c-null-coalescing-operator-and-using-it-with-linq.aspx

Implement SCOPE_IDENTITY() in Data Access Layer

The SCOPE_IDENTITY() function are used in Insert queries to return the last identity value within your table. However, I never knew how to retrieve the ID value when using this function in my code.

Create an Insert Query in your Data Access Layer called "InsertUser". For example: 

INSERT INTO Users (FirstName, LastName, DateOfBirth, Email, City) VALUES (@FirstName, @LastName, @DateOfBirth, @Email, @City);
SELECT SCOPE_IDENTITY() 

When you return to the DataSet Designer you'll see that the "InsertUser" method has been created. If this new method doesn't have a parameter for each column in the table, chances are you forgot to terminate the INSERT statement with a semi-colon. Configure the "InsertUser" method and ensure you have a semi-colon delimiting the INSERT and SELECT statements.

By default, insert methods issue non-query methods, meaning that they return the number of affected rows. However, we want the "InsertUser" method to return the value returned by the query, not the number of rows affected. To accomplish this, adjust the "InsertUser" method's ExecuteMode property to Scalar (this can be found in the Properties panel on the right).

The following code will put your new Insert Query into action: 

int intUserID = Convert.ToInt32(BLL.InsertUser("John", "Doe", "16/06/1985", "[email protected]", "Oxford"));
//Output new User ID
Response.Write("New User ID Inserted: " + intUserID); 

For more info regarding the use of Data Access in ASP.NET 2.0 go to: http://msdn2.microsoft.com/en-us/library/Aa581778.aspx