Threaded Comments Database Schema

Interesting post on how to use a different schema strategy for nested comments that doesn’t involve a recursive hierarchy:

http://stackoverflow.com/questions/3763273/asp-net-threaded-comments

———————–

One approach I remember seeing somewhere was rather than using a Comment ID and a Parent ID , comments had a Comment ID and a “sort key” which was a concatenation of all the Comment IDs of their ancestors.

E.g. If comment 1 had two replies, comments 2 and 3, the sort keys would be:

1 : 0001
2 : 0001.0002
3 : 0001.0003

Then if someone replied to comment 2, it would be..

4 : 0001.0002.0004

So if you select all comments and sort by this sort key, they’ll fall out in the right order.

Then, to do the indenting, you simply look at the length of the sort key to see how many levels deep the comment is, and indent an appropriate amount.

Adding comments is easy: the sort key of the new comment is simply it’s parent’s sort key, with its own ID added on to the end.

———————–

Obviously you’d have to pad the ID values sufficiently.  In addition, if you wanted to just show comment 12345 and all children, you could just display the thread where code like ‘%00012345%’.

Aligning TABLE inside TDs

I inherited a web app that has a structure like this:
table
  tr
     td
     td
     td
        table

The last table in that list was supposed to be right align, but no modifications to it’s TD parent will change that.  You can’t use align=”right” or text-align:right.

You need to use align=”right” on the TABLE that you want to be right aligned, not on its TD parent.

I know this is a bad idea as CSS is much better for aligning elements instead of nesting tables, but in this case I’m not going to rewrite the whole app layout for 1 thing.

SQL Server Activity Monitoring

If you are using SQL Server and you want to get an overview of whats going on, you can make use of the “Activity Monitor”, which is available in Management Studio for SQL Server 2005 and later.

In Management Studio, just right click on the server (not the DB catalog) and pick Activity Monitor.

If you are using SQL Server 2000, you are in a bit of a tight spot.  Even thought the BOL say that you can use Activity Monitor from Mangement Studio, you can’t.  It’s a lie.  So you have to have Enterprise Manager installed.

From Enterprise Manager, under the server you want to view, expand the “Management” folder, and the “Crrent Activity” node.  Under there you will find Process Info, Locks/Process Id and Locks/Object.

Too bad MS didn’t make Management Studio backwards compatable like BOL claims it is.

Shrinking your SQL Server log files

Shrinking your LDFs is as easy as:

BACKUP LOG DBName WITH TRUNCATE_ONLY
ALTER DATABASE DBName SET RECOVERY SIMPLE
GO
USE DBName
GO
DBCC SHRINKFILE('DBName_log', 2)
GO

Just replace “DBName” with the actual name of the database.

In some cases you might have to inspect the properties of the database using management studio to look at the file names, if the log file happens to have a different name from dbname_log.

 

 

Response.TransmitFile failed 0x8007052e

I was attempting to use Response.TransmitFile to stream a pdf directly to the user but it kept erroring with the following message in the event viewer:

TransmitFile failed. File Name: \somenaspath.pdf,
Impersonation Enabled: 0, Token Valid: 1, HRESULT: 0x8007052e

Turns out, you can’t use TransmitFile on any files that do not exist under the virtual application.  So trying to serve up a PDF from the NAS would never work this way :(.

I fell back to using .WriteFile and didn’t have the same problems.

 

 

ASP.NET Markup Go To Definition and Snippets

I don’t know if this is something new in VS or if it’s because I have the power tools installed, but I just realized that I can pick “Go to Definition” on an asp.net object in my code behind, like a label, or an asp.net checkbox, and instead of taking me to the vb definition of the control, it takes me to the place where the object is defined in the ASPX markup.   

And, in vs2010 if you auto complete with pressing tab 2 times when adding aspx markup items it will auto complete the entire element, not just the little bit you are typing.

 

So for example, if you type “<asp:But” and press tab you get:

 

<asp:Button

 

But if you tab again you get:

 

<asp:Button Text=”text” runat=”server” />

 

There are snippets included that are named things like “textbox” and “button”, so if you type “<bu” or “<hyp” and hit tab 2 times you get:

<asp:Button Text="text" runat="server" />

Or

<asp:HyperLink NavigateUrl="navigateurl" runat="server" />

 

Repoint Access linked tables to different connection string

I’ve been looking for a way to do this for a project I’m working on.

The access app has linked tables that are on a SQL server, but it doesn’t use a DSN.  Instead the connection string is somewhere unknown to the user and so repointing it (like user server1 instead of server2, or db1 instead of db2) was a major pain point.

Finally I found some code that I can use to change it.  I changed some names, but you get the idea. 

Private Sub Detail_DblClick(ByVal Cancel As Integer)
     Dim db As Database, source As String, path As String
     Dim dbsource As String, i As Integer, j As Integer

     db = DBEngine.Workspaces(0).Databases(0)

     For i = 0 To db.TableDefs.count - 1
         If db.TableDefs(i).Connect <> " " Then
             If Right(db.TableDefs(i).Connect, 15) = "DbNameHere12345" Then
                 db.TableDefs(i).Connect = db.TableDefs(i).Connect & "Updated"
                 db.TableDefs(i).RefreshLink()
             End If

         End If
     Next
 End Sub

 

Convert Spaces To Tabs For A Visual Studio Solution or Project

I recently installed some VS.Net power tools, and 1 of the tools is something that tells you when you have a file with mixed tabs and spaces, giving you the chance to convert from one to another.

I decided to stick with Tabs (I know that spaces v. tabs is a religious war for many, but I won’t go into it).  The problem is that this only happens on a file by file basis.  Furthermore, when you change the whitespace, your source controls sees that lots and lots of lines in your file have changed, making it hard to see the 1 or 2 lines that ACTUALLY changed.

I didn’t find a single utility out there for converting spaces to tabs, so after some poking around I’ve come up with a workaround that does the trick, even if it is a bit hackish.

I created a Find/Replace regular expression that will find leading groups of 4 spaces and replace them with tabs.  The problem is: you have to run it recursively.  So if you have a spot in your code with 10 tabs of indentation, you’ll need to run the find/replace 10 times in a row, which each iteration converting 1 of the groups of 4 spaces to a tab.

Each time you run the Find/Replace the count will get smaller and smaller, until finally: 0.

The basic pattern you search for is:

^{(t)*}([ ]^4)

and you replace with:

1t

In this example, the ^4 means that I’m treating 4 spaces as a tab.  Change the number if you want to have 2 spaces = a tab or whatever.

Another thing you can do to make it run faster on a large project is to first try to replace large sections of spaces before replaces 4 spaces at a time.  For example, the following stuff will search for 20 spaces and replace with 5 tabs.

You search for:

^{(t)*}([ ]^20)

and you replace with:

1ttttt

This way, if you have 1 spot in your code with 60 spaces, you won’t have to run the replace 15 times.