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.

Wednesday, May 22, 2013

GacUtil Commands

Use:
gacutil -i "path and name of dll"
to install an assembly in the Global Assembly Cache

Use:
gacutil /u "name of dll without the extension"
to uninstall an assembly in the Global Assembly Cache

GacUtil is a command line utility that comes with Visual Studio.  Just open up the Visual Studio Command Prompt found in the Start Menu.  Make sure you run it as an administrator.

.evt versus .evtx log files

"Classic" .evt log files do not necessarily convert to the new .evtx format.  This becomes an issue when you get sent a log from a Windows Server 2003 server, and need to examine it using the Event Viewer on your Windows 7 pc.  Sometimes you can use Event Viewer's "Save all events as" feature to convert the file to a .evtx file, but this does not always work.  Occasionally you get a "The data is invalid" error.  When this happens, the only way I know of to efficiently examine the log file is to copy it over to a Windows Server 2003 server, and look at it using the old Event Viewer.

Tuesday, May 21, 2013

SharePoint pages are extremely slow to load when debugging

I was debugging a SharePoint project today, and noticed on one of the pages that it took forever to load the page.  It was so slow, you could see the individual controls being painted on the screen.  It only did this while debugging from Visual Studio (2010 in this case).  After some research, I found that turning off IntelliTrace fixed this problem.  This can be done by clicking Debug, Options and Settings, IntelliTrace, and then uncheck the Enable IntelliTrace option.

Friday, May 17, 2013

Delete a sub-site using command prompt

On the SharePoint web server, open a command prompt as administrator.

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\stsadm" -o deleteweb -url "url of subsite goes here"

Tuesday, May 7, 2013

SharePoint .net framework version

When writing code that interacts with SharePoint 2010, you have to make sure your project compiles using version 3.5 since this is the version that SharePoint 2010 uses.  By default Visual Studio 2010 will use version 4.0.

"14 Hive" location

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14

Property Bag Stuff and SharePoint 2010 Web Services

If you need to access a site property that is stored in the "property bag", you're not going to be able to use any of the SharePoint web services to get at it.  Unfortunately, the only way this is possible is through the object model (specifically SPWeb.Properties).  And of course, this code has to run on the web server.  The advantage of the web services is you can run them anywhere, but as you can see, there are some limitations.