Posts

Use a Web.Config connection string in your Data Access Layer with LINQ To SQL

Image
My applications have the Business and Data Access Layers in a separate DLL from the presentation. This makes it so that everything that wants to do a certain function uses the same library. I also like to pre-compile my web sites which means that I can't get at the app.config if I need to make changes to the connection string (moving around dev, test, prod, etc). I found this from Jason Gaylord that worked quite well. As I worked with it, I began to think that there might be a better way to handle it. My answer was a simple factory pattern class to create my datacontext. I'd like to claim the idea but I really got it from the Enterprise Application Block's DatabaseFactory. Instead of adding an additional constructor I simply create a class that has the responsibility for creating the datacontext. The CreateDataContext method determines the correct connection string and then uses the predefined constructors. I place this class in the code file for the LINQ To SQL since ...

Silverlight Master Page

Image
Since I'm old school and so are most of my users, I wanted to create a simple Silverlight app that had a title bar across the top, nav menu on the left side and a content area on the right. You can modify this for mutiple zones and do some pretty slick stuff. Note: This is Silverlight 2 Beta 2 and VS 2008. The key is to create a Canvas that gets changed when you want to make changes. Here's the code for the above -- notice the Canvas named ContentHolder. < UserControl x:Class ="SilverLightMasterPageTest.Page" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:SilverLightMasterPageTest" > < Grid x:Name ="LayoutRoot" Background ="#FF5C7590" > < Grid.RowDefinitions > < RowDefinition Height ="30" /> < RowDefinition Heig...

Det. Eddie Ibarra -- Hero of the Week

Det. Eddie Ibarra is my hero of the week. Because he solved a homicide in the First 48? No. Because he's a Steelers fan living in Dallas. He was featured on the show the other night and his cube was covered in Steelers stuff. There was even a picture of his kid in a Big Ben jersey. Way to go Eddie!!!! How long is it until the first game.

Easy Way To Maintain Div Scroll Position On Postback

There are numerous ways to maintain scroll position on a postback that all involve javascript coding. Unfortuantely, it always seems like they don't work in the situation I need. Well, the other day, I found a solution that had been right in front of my nose. I was working on a page where I wanted a list of people on the left and the details for the selected person on the right. The list was inside a scrolling div so they'd be able to scroll the list to find the individual.  Everything was wrapped in an UpdatePanel  and I had everything set up right. However, when a postback occurred -- boom -- back to the top of the div. My solution was two UpdatePanels. One inside my scrolling div and the other holding the DetailsView. The UpdateMode is set to Always for both so when the update fires in one, it also fires in the other. Since UpdatePanel is inside the scolling div, the div (and its current scroll position don't update). Viola, a scrolling div that maintains position...

Custom Classes and LINQ To SQL Group By

Image
I spent a good part of my morning trying to get LTS to return my custom class as a result of a group by instead of an anonymous type. Most of the samples I found on the web for Group By returned anonymous types and the ones that didn't were from early versions and I couldn't get them to work. The problem I was having was getting the column I was grouping on mapped. I created a class called DescriptionCounts that had two properties Description (string) and Count (integer). The following code will correctly map a group by query to this class. Dim i = From b In dc.Barriers _ Group By b.BarrierType.Description Into g = Group _ Select New DescriptionCounts _ With {.Description = Description, .Count = g.Count} The main thing to notice is that when the Description property gets mapped, it is mapped to a variable named Description (.Description = Description -- notice that there is no prefix) and not to a property of b. All of the aggregate functions require...

LINQ To SQL Attach

The Attach method allows updating with a disconnected object that has been updated. The easiest version to use is: Public Sub UpdateCustomer(updatedCustomer As Customer)    Dim dc As New MyDataContext()    dc.Customers.Attach(updatedCustomer, True)    dc.SubmitChanges End Sub The only problem is that this generates a SQL statement that updates all columns rather than just the on that has changed. In other words, we used a little extra bandwidth. The reason this happens is because when we disconnected the object, we lost all the built in change tracking provided by LINQ (for now). Option 2 is to get a new copy of the object and update it. Public Sub UpdateCustomer(updatedCustomer as customer)   Dim dc as New MyDataContext   Dim originalCustomer as Customer = from c in     dc.Customers Where c.PrimaryKey =     updatedCustomer.PrimaryKey   originalCustomer.CustomerName = ...

Visual Studio 2008 & SPAN tags

Visual Studio doesn't seem to like SPAN tags. I've already reported a bug where it adds duplicates SPAN tags to templated fields in a GridView or DetailsView that is within a span tags. Now, it's moving my span tags around. I had two span tags to define left and right columns with inline-block. They were contained within the same div. Somehow, VS decided that the left span belonged to the div above and moved it all on it's own. If it keeps happening, I'll see if I can narrow the circumstances down and get a bug report filed. Technorati Tags: Visual Studio 2008 , SPAN , Designer