Saturday, November 17, 2012

The Principles of Open Space Conference

Today I attended a technical open space conference and I really liked the concept.

In brief these are the principles of open space conference:

1. Whoever shows up are the right people.
2. Whenever it starts, it starts.
3. Whenever it's over, it's over.
4. Wherever it happens is the right place.
5. Whatever happens is the only thing that could


Tuesday, February 21, 2012

Visual Studio Database (VSDB) Project issue with UDF and Views

In Visual Studio 2010, there seems to be a dependency problem with User Defined Functions (UDF) and Views. The deployment script created by the VSDB project always puts UDF before database Views. Consider this scenario, we have a database function which depends on a database view and we have altered both the function and the view. Now, when a deployment script is created for this database project, the altered function is always placed before the alter view statement. Running this script will fail at alter function statement because the dependent view was not altered yet. I could not find any settings in VSDB project to change the sequence order for the build, meaning build my database View first and then the UDF. The only workaround I found was to alter my database View in the pre-deployment script.

Monday, August 8, 2011

Entity Framework Error: Resource not found for segment 'Property'

I was getting "Resource not found for segment 'Property'" error with the below code. I am loading some data from the database using .Net Entity Framework.


 private void LoadItem(int specId)
 {
       var query = DataContext.Items.Where( x => x.SpecId == specID );

       DataServiceQuery dsq = DataServiceQuery)query;

       dsq.BeginExecute(  ar =>
       {
             this.items = dsq.EndExecute( ar ).SingleOrDefault();
       },
       null );
       }
 }

The above code threw an exception when the data query returned no records. I was expecting SingleOrDefault() to return default value (which is NULL) if no matching records were found. But, instead I was getting "Resource not found for segment 'Property'" exception.

After doing some investigation I found out that the exception was due to the fact that I was using primary key field in my data service query.

       var query = DataContext.Items.Where( x => x.SpecId == specID );

Here the field SpecId was a primary key. In Entity Framework if your data service query is performed on a primary key, then SingleOrDefault() and FirstOrDefault() methods will throw an exception if no matching records were found.

To resolve this issue, I set the following property to TRUE on the data service DataContext. This line will suppress the "Resource Not Found" exception.

DataConext.IgnoreResourceNotFoundException = true;

Wednesday, July 13, 2011

SQL: Deleting duplicate records from table

Here is one way to delete duplicate records from a table:

Consider we have some duplicate records in TempTable which has a unique identifier column called "RowId".

delete from TempTable
where RowId not in
(
select min(ROWID)
from TempTable
group by column1, column2,...
)

For each duplicate set of duplicate records, the above query retains one row with minimum RowId and deletes all other duplicate records. If you want to retain row with maximum RowId and delete all other duplicate records, then just replace the min() method with max().

Tuesday, June 7, 2011

SQL Error: None of the result expressions in a CASE specification can be NULL

I got this error when my CASE statement in SQL returned NULL and did not have any ELSE section in the CASE statement: "None of the result expressions in a CASE specification can be NULL"

The CASE statement which resulted in error was like this:

SELECT 
CASE WHEN StudentId = 0 THEN null END

I wanted to return NULL when StudentId = 0; else return the StudentId.

The above query did not work without the ELSE part. After adding the ELSE section like below the query executed without error:

SELECT 
CASE WHEN StudentId = 0 THEN null ELSE StudentId END






Thursday, May 5, 2011

Creating DataGridTemplateColumn from code behind

We can create DataGridTemplateColumn from code behind by defining a DataTemplate in the ResourceDictionary xaml and then use the DataTemplate to create the DataGridTemplateColumn.

In the below sample code, I am creating a data grid column with checkbox.

In the ResourceDictionary xaml, add DataTemplate for the checkbox:


<DataTemplate x:Key="CheckboxTemplate">
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</DataTemplate>

From the code behind create the DataGridTemplateColumn for the checkbox:


DataGridTemplateColumn checkboxColumn = new DataGridTemplateColumn()
{
      CellTemplate = App.Current.Resources["CheckboxTemplate"] as DataTemplate
};

Double click issue with DataGridCheckbox column

DataGridCheckboxColumn in DataGrid seems to have double click issue. One has to click twice to check or uncheck, first click to select the row and the second click for the checkbox. We can use DataGridTemplateColumn instead for the checkbox which does not have double click issue. 

sample code:

<DataGridTemplateColumn Header="Name">
<controlsData:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" />
</DataTemplate>
</controlsData:DataGridTemplateColumn.CellTemplat>
</DataGridTemplateColumn>