Monday, February 27, 2006

SQL Union Statements Implicitly Perform Distinct Selects

Ok, so I'm not sure if the title is worded correctly, but I needed something so here's what I'm getting at:

I was working on a SQL stored procedure at work today and adding some more joins and columns to a Select statement that was part of a Union with another Select statement.  Everything was going good until I added a Text column to the columns to return.  At this point I started getting an error stating that Text columns can not be used with Distinct.  The odd thing was that the Distinct keyword didn't exists in the Select statement (or anywhere in that stored procedure for that fact).  I thought about it for a little bit, after messing around with the statment for a while, and realized something.  A Union will not insert duplicate rows into the resultant data, it basically does a Distinct Select.  Since I knew this particular Union statement would never return duplicate data, I modified it from being a standard 'Union' to a 'Union All'.  I was then able to add the Text column with no problem.

Tags: , ,

Submit this story to DotNetKicks

Friday, February 24, 2006

DataGridView Column Header Style

I was building a .NET 2.0 Windows application which contained a DataGridView object.  I was attempting to modify the column header styles to fit better in the application but none of the settings I was applying seemed to be taking effect.  So finally I posted a message on Channel9 and got a response with the answer to my question.

In order to use styles in your DataGridView control for your .NET 2.0 Windows application, you must specify dgvMyDataGridView.EnableHeadersVisualStyles = false.

Example of the code:
//Instantiate new DataGridViewCellStyle
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 =
    new System.Windows.Forms.DataGridViewCellStyle();

//Define Header Style
dataGridViewCellStyle2.Alignment =
    System.Windows.Forms.DataGridViewContentAlignment.BottomCenter;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif",
    8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White;
dataGridViewCellStyle2.SelectionBackColor =
    System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.WrapMode =
    System.Windows.Forms.DataGridViewTriState.True;

//Apply Header Style
this.dgvTransactions.ColumnHeadersDefaultCellStyle =
    dataGridViewCellStyle2;

//Disable Headers Visual Styles - apparently there is an "automatic"
// visual style that needs to be disabled
this.dgvTransactions.EnableHeadersVisualStyles = false;

Tags: , , ,

Submit this story to DotNetKicks

Sunday, February 19, 2006

Conditional Compilation Symbols for ASP.NET 2.0 Projects

Conditional compilation symbols are those constants that you can define in order to include or exclude certain code in compilation of your solution.  The most common constant is DEBUG.  With DEBUG defined in your build configuration, you can define code that only gets compiled into the final binaries if compiled in the Debug build configuration.  Below is an example of using the DEBUG constant to include code only when in Debug configuration.

#if DEBUG
            Response.Write("The value of 'a' is " + a);
#endif

Since ASP.NET 2.0 projects do not have project files, there is no place in the Microsoft Visual Studio 2005 IDE to define the conditional compilation symbols like there is in DLL and Windows Executable projects.

It turns out that you can define these constants in your web.config.  The downside to this is that they are not bound to a specific build configuration and so you will either have to manually add/edit these constants in your web.config or add something to your build process to handle these constants.  Just add the xml below to your configuration of your web.config to define the DEBUG constant and a custom SANDBOX constant.  (I like defining SANDBOX in order to hit against a developement/test database rather than the real/production database.)

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
      Culture=neutral, PublicKeyToken=b77a5c561934e089"
      compilerOptions="/d:DEBUG,SANDBOX"/>
  </compilers>
</system.codedom>

Submit this story to DotNetKicks