Friday, June 28, 2013

Header Alignment Issue with ASP.NET GridView Control

Without any styling a GridView control's header will display the text in the center of the column.  If you want the header text to be aligned to the left of the column, it can be tricky and not very intuitive to accomplish this.

There are several places (markup not code-behind) in the grid where you can specify a CSS class to apply to the header.  The attribute is called HeaderStyle-CssClass.  This attribute is applicable to the asp:GridView node, or the individual Column nodes (i.e. asp:TemplateField, asp:BoundField, etc.).  There is also a HeaderStyle node which as a CssClass attribute that would mean the same thing.  However, any alignment styling will not work unless the HeaderStyle-CssClass attribute is set on the individual Column nodes.  So, you have to specify the header's CSS Class for each column.  To me this is counter-intuitive, but appears to be the only way to get a header to align a certain way.

Friday, June 7, 2013

SharePoint 2010 will not open PDF files by default

By default, if you click on a PDF file in a document library, the file will not open in a browser.  Instead, a dialog box will show up prompting you to save the document to your hard drive.  This is supposed to "protect" the user from opening up malicious files.  Obviously, if you deal with PDF a lot, you can't expect people to download the file every time they want to edit or just look at it.  It pretty much defeats the entire purpose of the document library.  So, we have to "loosen up" this restriction.  Unfortunately, Microsoft does not provide a way to only "loosen up" this restriction for specific file types.  It's all or nothing.  So, follow these steps:


  1. Central Admin
  2. Manage Web Applications
  3. Select your web application, and click General Settings / General Settings in the Web Applications ribbon.
  4. Select the Permissive setting in the Browser File Handling section.

Tuesday, June 4, 2013

View ALL user profiles in SharePoint 2010

From Central Admin, if you navigate to Manage Service Applications, User Profile Service Application, and then Manage User Profiles, it appears that you can only search for specific profiles using the provided "Find Profiles" box.  However, sometimes you want to see a list of all profiles when looking for something.  Just type the name of the domain in the box, and all profiles tied to an account in that domain will pop up.

Wednesday, May 29, 2013

System.IO.File.WriteAllBytes doesn't work well with large files over a network.

While moving files from one server to another, I would get an "Insufficient System Resources" message when the file was big (around 30 MB).  Turns out, WriteAllBytes works pretty well if you are moving the file around locally, but if you are writing the file to a network share, it can't handle it.  Fortunately, the following code will work:
Using MyStream As New FileStream( _
        FilePath, FileMode.Create, FileAccess.ReadWrite)
    MyStream.Write(Data, 0, Data.Length)
End Using
In this example, I am writing a byte array (represented by "Data") to a file specified by the "FilePath".

Tuesday, May 28, 2013

Internet Explorer 10 breaks debugging with Visual Studio 2010

"Welcome to your new browser!"  Today, automatic updates installed the latest version of IE and I noticed that I got the following error when debugging from Visual Studio 2010:


After some research on the web, it looks like everyone gets this after installing IE 10.  The solution is to register the new IE10 debugger dll.  Apparently, this would happen if you installed Visual Studio after you installed your browser (which is normally what happens).  So use this command as an admin:
regsvr32 "%ProgramFiles%\Internet Explorer\msdbg2.dll"
If you are using a 64 bit box, then use the command prompt here:
C:\Windows\SysWOW64\cmd.exe
Otherwise, just use the standard command prompt.  Remember to run it as administrator.

Add days, months, or years to a date in SQL

Use the following:
DATEADD(month, -3, GETDATE())
This example gets a date 3 months prior to the current date.  Use "day", or "year" in place of "month" to do similar calculations with those units.

Thursday, May 23, 2013

Be mindful of transactions when logging exceptions to a database

The idea behind transactions is this:  When an exception occurs, any database updates defined within the scope of a transaction are not committed to the database.  Either everything works or everything doesn't.  If you are logging exceptions to a database, you have to make sure your logging code is not within the scope of the transaction where the exception is occurring.

This can be tricky when using web services.  I would advise not using the System.EnterpriseServices.TransactionOption web method attribute to implement transactions.  This essentially includes the entire web method within the scope of a transaction.  So, your logging code would also be included in this transaction and would not be committed if an exception occurs.

Replace the attribute with the following code:

Try

    Dim MyOptions As New System.Transactions.TransactionOptions
    Dim MyScopeOption As New System.Transactions.TransactionScopeOption
    MyOptions.IsolationLevel = Transactions.IsolationLevel.ReadCommitted

    Using scope As System.Transactions.TransactionScope = _
      New System.Transactions.TransactionScope(Transactions.TransactionScopeOption.Required, _
      MyOptions)

        ...
        Code goes here
        ...

        scope.Complete()

    End Using

Catch ex As Exception

    ...
    Database logging code goes here
    ...

End Try

Note that when an exception occurs, you leave the scope of the transaction, and are free to commit your logging entries in the database.