Friday, November 23, 2007

Visual Studio 2008 - Automatic Properties in .NET 2.0

In the next couple of blogs we will present shortly some cool and interesting stuff we found about Visual Studio 2008 and .NET Framework 3.5. For all cool stuff see Visual Studio 2008 Official page.
One of the first items we noticed and played around are Automatic Properties. What does it mean? Untill now it was common to define a class as
public class Test
{
public Test() { }

private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
where for each property you usually had a private member variable.
But now, Visual Studio 2008 and .NET framework 3.5 introduces Automatic Proeprties where your class can be defined as

public class Test
{
public Test() { }
public string FirstName { get; set; }
}
and you will notice that a private member variable is missing and a whole body of the property implementation itself (it has just a stub definition get; set; )
Imagine how much you reduce a time for class scripting. Readability is also much better.
But you will probably ask, where does .NET keep your data now? If you take a look at IL you will find that actually a compiler generates a field (variable) for you:
'<>k__AutomaticallyGeneratedPropertyField0'. Whole construction is done just by using CompilerGeneratedAttribute. But, what we had in mind, this attribute is part of .NET Framework 2.0, what lead us to... Hey, Automatic Properties are possible in .NET 2.0 than?! And this is really true. Open your Visual Studio 2008 (Express), create such a construction, as a Target Framework for a project choose .NET Framework 2.0 and you will see it runs. WOW!!! We like this very much and will use it in DBTyP.NET as soon as possible.

Monday, November 12, 2007

Formatting bounded DateTime fields in ASP.NET

If you ever tried to work with BoundField object bound to a DateTime field with the DataFormatString, it must happen, that first time you didn't get your DateTime value proeprly formatted.
<asp:BoundField DataField="MyNotFormattedDate" DataFormatString="{0:MM/dd/yyyy}" />

Looks like that you have done everything correct but still getting your value formatted using its ToString() method like "11/13/2007 10:05:12 PM". So, the problem is not in your definition but in ASP.NET which tries to prevent cross site scripting attacks, the field value is HTMLEncoded. And HTMLEncoding occurs before applying any formatting, making your formatting string without effects. To Get your field formatted as you define, you should tell object not to use HTMLEncoding like

<asp:BoundField DataField="MyFormattedDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>

Compare databases with the best tool - DBTYP.NET Studio.

Tuesday, October 30, 2007

Changing default column values - SQL Server

A very common mistake in T-SQL is trying to alter column definition trying to alter DEFAULT value (constraint) for that column. Probably, developers expect to have this because it is possible to assign default value for a column during table definition:
CREATE TABLE Table1 (
col1 INT NOT NULL DEFAULT (0),
col2 INT NOT NULL
)
Due to possibility to change column type from int to varchar(20) or null-ability with
ALTER TABLE Table1 ALTER COLUMN col2 VARCHAR(20) NULL
a lot of developers expect to be able to change a column default value with
ALTER TABLE Table1 ALTER COLUMN col1 DEFAULT (1)
Error message is displayed trying to execute this: "Incorrect syntax near the keyword 'DEFAULT'".
Mistake.
With a create table syntax mentioned earlier,
SQL Server creates default constraint with auto generated name like "DF__Table1__col1__57DD0BE4" and stores default value in [text] column of syscomments table.
So, to change a default value for a column you should change a constraint definition and the only way to do that is to drop current constraint and create the new one:
ALTER TABLE table1 DROP CONSTRAINT
DF__Table1__col1__57DD0BE4
ALTER TABLE table1 WITH NOCHECK ADD CONSTRAINT [Df_test_col1] DEFAULT (1) FOR col1


Compare databases with the fastest tool - DBTYP.NET

Wednesday, October 24, 2007

CheckBox header column for DatagridView

It is very common to have a list of items in DataGridView with a check box in the first column where your later action will depend on user selection. This can be very easy done having a first column defined as DataGridViewCheckBoxCell object. But, how can your customer select all items in the list (let's say you are working on an email client app and user wants to delete all of his 100 spams)?
Unfortunately, .NET framework does not have a header class similar to DataGridViewCheckBoxColumn. Such a problem lead us to the idea to generate a class which will have a check box item in header where developer can have a full control after user check/uncheck item in the header. Common action is to check/uncheck all items in the DataGridView depending if header is checked/unchecked.
Whole article and class definition is available at http://www.codeproject.com/useritems/CheckBoxHeaderCell.asp

Introduction

BYPsoft decided to be a part of programming community and brings a valuable expirence to others. You can find our posts in different forums all around the world. But now, it is time to collect all of this, and brings new and interesting topics, mainly from .NET area to publicity. Besides writing on .NET stuff we will also promote our database comparison tool DBTyP.NET here.