Datatable to List of objects mapper.

In my previous blog post about mapping data rows to objects you saw how easy it was to use reflection to map a data row to an object. With a little extra effort you can map data tables to a list of those mapped objects. The code below builds on my previous blog and will not work unless you have the code from that blog entry.

Public Shared Function MapDataTableToList(Of T)(ByVal dt As DataTable) As IList(Of T)
        If dt Is Nothing Then
            Return Nothing
        End If
 
        Dim list As IList(Of T) = New List(Of T)
       
        For Each dr As DataRow In dt.Rows
            list.Add(MapDatarowToObject(Of T)(dr))
        Next
 
        Return list
    End Function
To explain how this works we’ll go line by line

Public Shared Function MapDataTableToList(Of T)(ByVal dt As DataTable) As IList(Of T)

I’m creating a method that needs to be called by defining a type for T. The only parameter is a data table. An example of calling this method looks like this:

Dim dt As DataTable = GetDataTableFromDb()
Dim usersList As IList(Of User) = MapDataTableToList(Of User)(dt)
 
This would convert a data table to a generic list (IList) of user objects.

If dt Is Nothing Then
Return Nothing
End If

These lines of code make sure that the data table parameter has a value.

Dim list As IList(Of T) = New List(Of T)
 
Create a generic list of objects based on T. T is the generic type specified when calling the method. In the example usage above the T parameter would be of type User.
 
 
For Each dr As DataRow In dt.Rows
list.Add(MapDatarowToObject(Of T)(dr))
Next

Iterate through each data row in the data table and call MapDatarowToObject (from the previous blog entry) and add the mapped object to the list.


Return list
 
Return the list of objects from the method.

So there you go, An easy to use data table to object mapper.

Until next time.

So I can add trainer to my resume...

It's been about a month since I last posted. I have a good excuse although I could have taken 5 minutes over the last month to at least say hi. In any case over the last month I've been busy with two big projects.

The first is the relaunch of JumpStartTV.com. It's was a site developed by Brian Knight with the intentions of a different kind of video site. Along the way it was forgotten. Andy decided to revive the project and I was the one tasked with the revival. I haven't done too much work to it yet as we just wanted to get it up and running with a few minor changes. I added a tag cloud and added some admin tools for automatically processing videos but that's about it. Look for some very nice updates over the next few weeks with a major push of content. We're currently planning 1 new video per day 4 days a week for the SQL channel. The .NET channel will start adding content in October. We have a lot of different authors contributing content which should make for a very cool technical video site. By the way, all of the content is free.

The second project is the one with which I can add trainer to my resume. I successfully put together a Beginning ASP.NET course and delivered it to 7 students last week. It went well in most places but needs a little work to get it totally right. I would say that the students were pretty happy with the content and enjoyed their time training with me. I even had one student buy a $50 gift card as a thank you.

Andy and I are going to be developing two more courses that will be available next year. Advanced ASP.NET and .NET for DBA's. There will be more information on those courses to come.

I'm really excited about the work that I'm doing and this next year should be a really good for me both professionally and personally.

My next blog should be the one I promised a few months ago about mapping a data table to list objects.

Until next time.

Recap of the Central Florida .NET Users Group

Last tuesday I went to speak at the Central Florida .NET Users group. My session covered some of the new features in C# and VB.NET using Visual Studio 2008. I want to thank Roy and Charlie for inviting me to speak. It was my first users group session.

I had a good time with this group. There were a lot of really good questions and suggestions. I've never had such a huge response. Where were these attendees at my last SQL Saturday session?

After the session was over I got to hang out with a couple of the attendees at Chili's. Had a beer, some food and some really interesting conversations. One in particular about Project Euler. I had never heard about it before. It's a really cool site for nerds!

One of the most surprising things I heard that night was the number of employees that attended the users group from a local company. Out of approximately 1000 developers, 2 from the company showed up. 2 out of 1000.... That's just staggering to me. However it's not that surprising. A good number developers do not want to do anything outside of work. I think the reasoning I've heard behind that is if they aren't being paid for it, then why should they do it? The 2 people that showed up were really smart and I enjoyed talking with them.

All in all it was a good night. I got some great feedback, had a good time and enjoyed everything about it. Ok, maybe the two hour drive home at 10:30 P.M. wasn't all that fun but it was worth it...

Sorry for my lack of posts recently. I've been really busy with a few projects that you'll be hearing about shortly.

Minuteman!

No, that's not what my wife calls me. Ok, maybe it is, but that's not the point of this post.

The SQL Saturday "inbetween" this weekend event went pretty well. Lot of old friends and some new ones. I had lunch with a few new speakers which is a rarity that day of the event. As usual got to meet a few new people. I had to remind Joe Healy of our bet. All in all it was a pretty good day. If you're looking for the session files, visit SQLSaturday.com.

Now back to the subject at hand here. The night before my presentation I went over my session. I used the "Use Rehearsed Timings" in powerpoint. The total time (without questions) was about 35 minutes. The last time I did this session I had about 15 minutes of question and answers. I didn't realize until I got to the event that the sessions were 1 hour 15 minutes long. No big deal I would just let them go early and stick around for any private questions.

I started my session at 3:00... It was over at 3:15...

At around 3:14 I thought "Oh sh**, I'm finished?". I tried to remember if I was missing anything. Nothing came to mind. I was at a complete loss. So my total presentation was 15 minutes long. Hence the term "minuteman".

Luckily at that time, Andy walked in and saved the session. He started questioning the audience about triggers . He brought Kendall Van Dyke on stage and we did an impromptu panel session. That went over pretty well.

After the session was over I knew what was coming. I got the "That was a huge screw up!" from Andy. Which, of course, it was. The problem I have this time was I had no idea what happened except to say I rushed the presentation. I'm not sure where I lost all of that time though. Surely I was talking like the micro machines dude. No one complained about anything else except to say my presentation was too short.

So the lesson from all of this is have enough content to cover your session... or talk really slow and take pauses if you don't. Just another lesson along the way for me.

I WON!

Ok... So it didn't come in a package marked "Fragile" (must be italian) but never the less, I did win a prize from community-credit.com. A pair of usp soft monkey speakers (pictures below).

It's an interesting site if you never been to it before. Basically if you do something for the community (teach at an event, help on forums, write a blog post, etc) you get points for it.

I ended up with the 13th most points in March and won a pretty cool set of monkey speakers. My youngest daughter wanted to chew on them but I ended up taking them to work.

Here is the package:



Here they are at my desk at work.



Close up



Pretty cool eh? Thank you to David Silverlight over at community-credit.com!

Upcoming events

I have some upcoming events I'd like everyone to know about.

1) I'm speaking at the "Tweener" SQL Saturday event in Orlando on June 7th. My session will cover using DDL Triggers in development. Tweener has many different meanings so let me clarify. The event occurs between the two TechEd events in Orlando. Hence the name "Tweener". It doesn't have anything to do with a particular sub-culture...

2) I'm speaking at the June 17th Central Florida .NET Users group in Lakeland. Roy Lawson extended an invitation for me to speak after he saw me at the Orlando Code Camp.

3) I'm teaching a course on ASP.NET July 21-24th. I had to start somewhere. I had a dream of teaching development on some level. Now it's becoming a reality. This is first of many courses I plan on developing. A side benefit from writing a course about ASP.NET is that you learn a few things along the way that you didn't know.

4) Last but not least, I turn 30 on June 6th. I feel pretty good about where I'm at in life at 30. I don't feel 30. Obviously I don't look 30 because I cannot buy any alcohol without being carded... But that's a good thing. I was carded for lottery tickets a few months ago. Seriously... I don't look 18? Maybe the cashier was being nice...

In any case, the next few years is where I will make my mark! The last couple were just a start. I'm gearing up for a whole bunch of good stuff...

So this is the reason last blog posts have been spaced so far apart.

Until next time...

Unskilled and unable to realize it

Ok, so the title of the post is plagiarized. I "stoled" it from http://blogs.msdn.com/architectsrule/archive/2008/05/16/unskilled-and-unable-to-realize-it.aspx

The reason I stole it is because I thought it was a great post. After I read it I started to think about my speaking sessions. After my last session I sought out constructive (or negative) feedback. I had a lot of praise and that's always nice but I don't learn anything from those comments.

I didn't get any constructive feedback and that's one of the points of the post in the link above. "People seldom receive negative feedback about their skills and abilities".

Even though I asked for it, no one was able to tell me I did something "wrong" or didn't learn anything. Surely my presentations are not perfect. I think people are afraid of being critical even though I'm asking for it.

I believe this is also very common in software development as well. Programmer 1 writes a piece of code. Programmer 2 makes a change to the same code and notices it's wrong. Instead of programmer 2 going to programmer 1 and saying "Hey, this is messed up" he complains to himself and others that the code "sucks" but never tells programmer 1. How can programmer 1 ever learn anything if they don't receive the constructive feedback. I've seen (and been guilty) of this behavior.

Now it's not good enough to not get negative feedback. You should understand WHY that negative feedback was given. If you're the one giving the negative feedback, you can't tell your colleague "Hey man, that presentation was awful!" and not explain WHY is was awful. Be constructive. Let them know what is wrong and maybe a few suggestions on how to correct it.

As for you, the reader... Seek out criticism. Be open to good and bad criticism. It will help you become better at whatever you're trying to achieve. I know it has helped me immensely.

Naming my methods

We bought a calendaring tool from TMSSoftware that we use to schedule events for SQL Saturday. The calendar works great but has an issue that clears out the screen when deleting an event. It's a really weird behavior. I decided that I needed help from the support staff at TMS. I started the email with a couple of screenshots of the error. Then I started pasting in the code example that I used to delete the event and then rebind the calendar control. This is where I had to step back for a second and think about what I was sending them.

Here is the code that handles the delete event from the control:

Protected Sub WebPlanner1_EventDeleting(ByVal sender As Object, ByVal e As TMS.WebPlanner.PlannerEventDeletingEventArgs) Handles WebPlanner1.EventDeleting
        Dim pe As PlannerEvent = e.Event
        DeleteEvent(pe)
 
        LoadSessions()
        LoadWaitList()
    End Sub

I looked at this code and said "Wow, I wonder if they will know what those methods (DeleteEvent, LoadSessions and LoadWaitList) do?". My conclusion was no, they probably won't. The last thing I want is another email asking "What does this do?".

After thinking about it for a few minutes this is more like what I should have wrote:

Protected Sub WebPlanner1_EventDeleting(ByVal sender As Object, ByVal e As TMS.WebPlanner.PlannerEventDeletingEventArgs) Handles WebPlanner1.EventDeleting
        Dim pe As PlannerEvent = e.Event
        DeleteEventFromDatabase(pe)
 
        BindSessionsToCalendarControl()
        BindSessionsToWaitlist()
    End Sub

To me, the second code example is going to tell the support staff exactly what I'm doing. But the examples don't stop there. Let's look at the actual "LoadSessions (aka BindSessionsToCalendarControl)" method.

Private Sub LoadSessions()
        Dim db As New DatabaseMethods
        WebPlanner1.Items.Clear()
           WebPlanner1.DataSource = db.GetScheduleSessions(GetEventId)       
        WebPlanner1.DataBind()
End Sub

The problem here is "GetScheduleSessions". What does that return? Is it a collection of objects or a datatable? GetScheduleSessions returns a datatable but you would know by looking at it. Here is what I should have wrote:

Private Sub LoadSessions()
        Dim db As New DatabaseMethods
        WebPlanner1.Items.Clear()
           WebPlanner1.DataSource = db.GetScheduleSessionsDataTable(GetEventId)       
        WebPlanner1.DataBind()
End Sub

It's those small changes that make your code just a little easier for you and others to understand what the code is doing. How many times have you gone back to your code 6 months later and said to yourself "What the hell does this do?"

I thought that I had given my methods good names. From the standpoint of another human reading the code and being able to understand it without much effort, I failed.

So going forward I will put more effort into making my method names more readable. It's a lesson I learned a while ago when I read code complete but somehow I deviated from my "learnings".

Another Successful SQL Saturday (Jacksonville)

The weekend officially started on Friday night at the speakers get together. I got to meet some new, cool people and see some friends again.

One of the more interesting things that happened on Friday was I made a bet with Joe Healy from Microsoft. Joe is a Georgia grad and of course a fan of the UGA football team. I'm a Florida fan. The bet is if Georgia wins the football game this fall I have to go to Tampa on my dime and take Joe out drinking. If Florida wins he has to come to Orlando on his dime and take me out drinking. Should make for an interesting game... Go Gators...

Met Rodney Landrum, author of Pro SQL Server Reporting. He also writes for SQL magazine. This was made apparent by the tattoo of the SQL magazine logo on his left arm. I think Joe will have a pic on his site soon. We also played the SQL Alphabet game. Basically each person says a word starting with each letter of the alphabet that is relevant to SQL Server. (e.g. A = Avg, B = Bit, etc). It's funny how much you forgot when under pressure to come up with something.

Spent some more time with Shawn Weisfeld. He's the (current) president of the orlando .net users group. He's leaving in June to accept a new position with his company. We'll miss you sage!

All in all, Friday night was a blast. It's always fun talking smack with everyone.

Saturday morning was a mad rush to get everything setup. Andy and I arrived around 7:30 AM and got right to work with Brian, Shawn and the other volunteers. People started showing up around 8:00 AM. The bulk of them got there between 8:30 AM and 9:00 AM. When it was all said and done the morning breakfast and rush went really well. The first sessions got started a little late but I don't think anyone really cared.

My session started at 2:45 PM so I spent most of the day helping other speakers get setup and ready to go. Spent some more time with Joe and Andy. Joe likes to give me crap about my iPhone. Says it's an MVP killer. I guess I'm out. I loves my iPhone. Andy spent most of the day taking notes about things that went right and wrong. We're always trying to figure out how to make these events go as smoothly as possible.

I even got "twitted" by Mark Polino (that means he said something about me on twitter). At some point I'll have to give twitter a try.

About 20 minutes before my session started I started my "routine". Ok, It's not really a routine at all. I just watch a video by "the whitest kids u know". This allows me to start my session in a good mood. I made the session interactive which seems to keep the attention of the audience a lot better than a lecture. It seems the attendees picked up on it because I scored relatively high on my evals.

I didn't have the privilege of being the youngest speaker this time around. That award went to Devin Knight, The younger brother of Brian Knight. I'm sure at some point there will be a book by Knight and Knight... He did a pretty good job for his first time speaking. He's a fellow UF fan so he's good in my book.

The day was wrapped up at 7 Bridges. About 30 of the attendees and speakers showed up to hang out. I played a game of pool with Devin and introduced him to my pre-session videos... I got to hang out with Tim Mitchell from Dallas for a while as well. He joined in on another game of SQL Alphabet with Rodney and I while I played pool.

Of course I spent most of my time this weekend with Andy. We had a lot of good talks about various things that we'll be spending some time on in the future. Mentoring, books, etc. You'll hear some good things in the future from us.

All in all it was a great weekend on many levels. I'm looking forward to the next event coming up on June 7th and 8th in Orlando.

Reminder: SQL Saturday #3 (Jacksonville)

The Jacksonville SQL Saturday will be held on May 3rd, 2008 at The University of North Florida Campus, Building 50, 1 UNF Drive, Jacksonville, FL 32224.

I am doing a session on ASP.NET Dynamic Data Websites

If you haven't registered yet go to http://www.sqlsaturday.com

Hope to see you there.


View Larger Map

Technorati Tags: SQL SERVER, SQL SATURDAY
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011