Google
 

Saturday, April 26, 2008

EDC: VS2008 New Enhancements

In this post I'll try to shed some light on what Chad Hower covered in his edc2008 session :"VS2008 New Enhancements".

The new keyword "var":
It's both used in LINQ expressions and when using anonymous types (more about them later). It also can be used in any regular variable declaration and initialization, for example:

StringBuilder b = new StringBuilder();
b.Append("abc");
var varB = new StringBuilder();
varB.Append("abc");


In the above example, var keyword was used instead of the StringBuilder class name in the second declaration. And the StringBuilder is used regularly. What worth mentioning is:


  • Some ex-vb developers may think that var is the same as the vb6 Variant datatype, which will cause some performace degradation when calling methods due to late binding. This is not true. The compiler replaces the var with the type initialized in the RHS. So the compiler output of these 2 lines is the same:

    StringBuilder b = new StringBuilder();

    var b = new StringBuilder();
    We just saved some keystrokes :).

  • VS2008 has full intellisense support for var. as you can see in the screenshot:


LINQ:

Using Language Integrated Query with collections can make many of programming tasks easier, like searching for an object or objects in a collection that meet a certain criteria.
For example, if we have a List< string > and we want to find all strings that start with the letter 'S', this is the old way to do this :

List< string > found=new List< string >();
foreach (string s in items)
{
if (s.StartsWith("S"))
found.Add(s);
}

Using LINQ:

var found = from s in items
where s.StartsWith("S")
select s;

Ok, more value can be gained if you need to get them sorted by length:

var found = from s in items
where s.StartsWith("S")
orderby s.Length
select s;


Some people may think that the 'var' keyword is what indicates a LINQ expression, but no, it's actually the 'from'.

A good place to think about using LINQ is where you usually use for and foreach.

LINQ is a new feature of C# v3.0 and VB.NET v9.0

Anonymous types:
I'll talk about Anonymous types usage in LINQ, although there are other cases when are useful.
In the above LINQ example, what if we don't need to get the result as a collection of complete objects of the original collection, and we just need one or two attributes (just as we can select just a few columns from a SQL database, not all columns from the table).
Anonymous types can be used in this case:

var found = from s in items
where s.StartsWith("S")
orderby s.Length
select new { value = s, length = s.Length };


Here, we created a new type on the fly, it has 2 properties: value which is a string and length which is an integer.
But how to iterate on found when we don't know the type of the objects it holds (remember, it's anonymous). This is where var comes to be useful again:

foreach (var s in found)
{
Console.WriteLine(s);
}



Others:
Chad talked about other thinks like OBA (Office Business Applications), WPF. The most important thing is that WPF works fine with Winforms. Winforms can host WPF components.

A very interesting presentation that Chad has shown for us was the History of Microsoft Office Download it and enjoy !!
Besides being funny, it shows how MS Office navigation had to evolve.


Chad just scratched the surface, but left us hungry for more knowledge about the existing new features.

Friday, April 11, 2008

Another place when NULL gets tricky

Dealing with NULLs in SQL causes confusion in many cases. Two good articles cover a lot about NULLs in SQL server: 4 Simple Rules for Handling SQL NULLs and Gotcha! SQL Aggregate Functions and NULL.

I this the latest should have covered another situation, when aggregate functions return NULL in case of not satisfying the where condition.
Let's take this Books table as an example:

ID Title Price
--------------------------------
1 C# 10
2 Database 15
3 VB6 30
3 VB.net 35

When you want to get the sum of all books that start with 'vb', you simply use SUM function like this:

Select sum(Price) from Books where title like 'vb%'

And the result would be 65

One may expect that if he asks for the sum of c++ books prices, the result will be zero (as we have none). for example, this query returns zero:

Select count(Price) from Books where title ='c++'

But with sum:

Select sum(Price) from Books where title ='c++'

The result is NULL. Why? Because, using the same logic described in the above articles, SQL will consider this as an absence of data (It couldn't determine the total price since it did not find any prices) if that makes sense.

This can cause issues when trying to read the value returned by this query in the application which expects a float value. A simple remedy for this would be using COALESCE or ISNULL: (I prefer to use COALESCE since it is standard.)

Select COALESCE(sum(Price),0) from Books where title ='c++'
Select ISNULL(sum(Price),0) from Books where title ='c++'


The result in this case is zero as desired.

Wednesday, March 12, 2008

Promoted to Lead Software Engineer

On Sunday, March, 2, 2008, I was promoted, to be the first Lead Software Engineer in ITWorx. the new position is an extension to the technical career path inside the company.

As the case with any promotion or move, it's a new challenge!! I can conclude my new duties as: making sure that software team I work with will do things right.

Right coding standards, practices, design, testing, etc.

I had the chance to be promoted to the position of Technical Architect, but seems that I still keep a lot of love for code, for the ambition of young fellow developers, for their need to explore and get guidance. I still want to be close to the day to day software building activities.I pray to God to help me to fulfill my new responsibilities. Wish me luck.

Thursday, February 21, 2008

ITWorx' products ExamExpert and Catalyst at The Microsoft® Ingenuity Point Contest

Vote is open at the The Microsoft® Ingenuity Point Contest to select your favorite product out of many innovative software products in Health care, Education , and Clean Technology.
ITWorx (where I work as a Senior Software Engineer), participates with 2 products:
Have a look, and vote !!

Restart your machine after installing Fulltext service for MS SQL Server Express 2005

I had MS SQL server express 2005 installed without Fulltext index support. When I needed it, I installed it, created a catalog, an index, and started change tracking, then I tried to make simple CONTAINS query, nothing returned.
I worked with FTS many times and for years (mainly with SQL Server 2000). It was hard to retry many times to get any result with not good.
I tried many times to drop the index, start full population, incremental population. Nothing worked.
I looked into the event viewer, I found this error:

Errors were encountered during full-text index population for table or indexed view '[dbname].[dbo].[TableName]', database ''[dbname].[' (table or indexed view ID '1550628567', database ID '7'). Please see full-text crawl logs for details.

I looked in the fulltext log ( located at : installationpath/MSSQL.1\MSSQL\LOG\SQLFTxxxxxxxxxx.LOG.x) , this info existed in error logs:

Error '0x80040e09' occurred during full-text index population for table or indexed view '[dbname].[dbo].[TableName]' (table or indexed view ID '1550628567', database ID '7'), full-text key value 0x00000001. Attempt will be made to reindex it.
The component 'sqlfth90.dll' reported error while indexing. Component path 'C:\Program Files\Microsoft SQL Server\90\COM\sqlfth90.dll'.

After searching for such error, I found that after installing fulltext service on MS SQL Express 2005, the machine must be restarted, and that this is mentioned in the ReadMe!!

If only we read the manual !!!!

Sunday, February 17, 2008

Another reason for “Cannot Generate SSPI Context” error

Today, I had trouble with SharePoint server. Each time I open the main page I get the error page (500-internal server error). This error occurred in an environment that worked fine for months. But suffered an accidental electricity cut off.

When I looked in the event log, I realized that SharePoint cannot connect to SQL Server configuration database (which existed on another server) the error details contained the famous error:
“Cannot Generate SSPI Context”
I tried to ping the server, flush DNS, connected to SQL Server using SQL authentication, restarted some services and machines, just to get the same error.

Searching Google led me to a good article: Troubleshooting Cannot Generate SSPI Context Errors , which is really good, but I still did not find the solution.

Finally, after consulting an infrastructure engineer, he suggested to check that the net logon service is running on the domain controller. I checked in and I found that it was paused. After running it, everything went well.

So, the next time you get “Cannot Generate SSPI Context” when connecting to SQL Server, add this check to the list of checks you do, and good luck.

Friday, February 8, 2008

Visual Studio 2008 Product Comparison

This is a great guide when you decide to buy visual studio:
Visual Studio 2008 Product Comparison

Note that Standard edition differs from Professional edition mainly in office tools, crystal reports, test tools, server explorer. Whether these features worth the price difference (about $440) depends on your needs.