Eamon Barker's eb.NET

Not quite vb.NET, but getting close!

Filter by APML
RSS Feed

Search

Profiles/Groups

Google Ads

Top Posts

Tags

Categories

Archive

Calendar

<<  March 2010  >>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Blogroll

Disclaimer

All postings are provided AS IS with no warranties, and confer no rights.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright Eamon Barker 2010

CC License

Visitor Map

Locations of visitors to this page
Welcome to eb.NET... a place that I can keep the things that help me in my day, that might help you in your's!

Development Tools I use Everyday

Development tools I use on a daily basis!

 

The topic at last nights local .NET User Group was "Tools I can’t live without". Joel Meikle (from Meikle Programming) had a massive list of tools... I use most of  them and the ones I don't use I will be looking into. Below is a list of tools I can't do without. This table below doesn't include any of the SharePoint Development Tools or Administration Tools I previously blogged about. NOTE: These tools are in no particular order!

 

Tool Name Description Category
Refactor Code refactoring is the process of changing a computer program's code to make it amenable to change, improve its readability, or simplify its structure, while preserving its existing functionality... Visual Studio Plug-in, Code Analysis & Improvement
Lutz Reflector .NET Reflector enables you to easily view, navigate, and search through the class hierarchies of .NET assemblies even if you don't have the code for them. With it, you can decompile and analyse .NET assemblies in C#, Visual Basic and IL... Code Analysis & Improvement
Key Jedi Key Jedi allows learning and training to use keyboard shortcuts. It can be used in presentations, screencasts and videos, as well as when working with someone else on the same machine to teach new shortcuts. It shows a visual list of shortcuts as you type them, no matter what application you work in. It is free, simple and quick to use... Productivity Improvement
Paint.NET

Paint.NET is an open source, raster graphics editing program for Windows, developed on the .NET Framework. Originally created as a Washington State University student project, Paint.NET has evolved from a simple replacement for the Microsoft Paint program, which is included with Windows, into a powerful editor with support for layers, blending, transparency, and plug-ins. It is often used as a free alternative to Adobe Photoshop. It is available under the MIT License...

Image Manipulation & Design
LINQ Pad

LINQPad is also a great way to learn LINQ: it comes preloaded with 200 examples from my book, C# 3.0 in a Nutshell.  There's no better way to experience the coolness of LINQ and functional programming.

And LINQPad is more than just a LINQ query tool: it's a code snippet IDE. Instantly execute any C# 3 or VB 9 expression or statement block!...

Code Analysis & Improvement
NotePad++

Notepad++ is a free (as in "free speech" and also as in "free beer") source code editor and Notepad replacement that supports several languages. It runs in the MS Windows environment...

Code Analysis & Improvement
Sandcastle Help File Builder Sandcastle, created by Microsoft, is a tool used for creating MSDN-style documentation from .NET assemblies and their associated XML comments files. The current version is the May 2008 release. It is command line based and has no GUI front-end, project management features, or an automated build process like those that you can find in NDoc... Documentation Tools
SharpDevelop

SharpDevelop is a free and open source IDE for the C#, Visual Basic .NET (VB.NET), Boo (programming language) and (starting from version 3.0) F# and IronPython programming languages.

It is typically used as an alternative to Microsoft's Visual Studio .NET. Early in its development there was a fork to Mono/Gtk# called MonoDevelop which includes multi-platform support...

Development Tool
ZoomIT ZoomIt is screen zoom and annotation tool for technical presentations that include application demonstrations. ZoomIt runs unobtrusively in the tray and activates with customisable hot-keys to zoom in on an area of the screen, move around while zoomed, and draw on the zoomed image. I wrote ZoomIt to fit my specific needs and use it in all my presentations Presentation Tool
FireBug & YSlow Firebug integrates with Firefox to put a wealth of web development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page...

YSlow analyzes web pages and tells you why they're slow based on the rules for high performance web sites. YSlow is a Firefox add-on integrated with the popular Firebug web development tool.
Code Analysis & Improvement
Ultramon UltraMon is a utility for multi-monitor systems, designed to increase productivity and unlock the full potential of multiple monitors... Productivity Improvement
Enso Enso is dead simple to use. You just hold down the Caps Lock key and type an Enso command, which is displayed in a translucent overlay. Once the command is typed, you simply release the Caps Lock key to activate it, and the overlay disappears. If you type fast, it all happens in a flash. For instance, to launch the Firefox Web browser, you just hold down the Caps Lock key and type "open firefox." To look up the meaning of the word "proclivity," you just hold down the Caps Lock key and type "define proclivity." Productivity Improvement

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


UNPLUGGED MSDN update & HP-Microsoft SBS Road Show

Road show for the latest and greatest from MSFT & HP in the Small Business and Cloud-Based Services space

This post is for anyone in New Zealand that is interested in the latest and greatest from MSFT & HP in the Small Business and Cloud-Based Services space... If you haven't already done so, check out the following two events:

Both events are being run on the same day in most centres, with the HP-MSFT session in the morning and the unplugged session in the afternoon. For more details on both the events visit the NZ SBSC (Small Business Specialist Community) blog.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


SharePoint Development Tip: Using LINQ to query a SharePoint List

Do you want to use LINQ to query SharePoint information? Read on!

Over the past couple of days I have been playing with an internal project, so I thought I would play and query the lists with LINQ... here is what I came up with!

Because a SPListItemCollection inherits from ICollection, we can attack it with a LINQ query... and that we shall!

A bit of background, I am using a Web Part to render the information to the user. The Web Part has a several properties for the user to supply information to configure it (they will appear in the "Miscellaneous" section of the Web Part Properties Pane). The properties are as follows, the CAML property is optional, but it gives an easy way to filter information from the list without going into the code:

   1: Private _siteurl As String = ""
   2: <WebBrowsable(True), _
   3: WebDisplayName("Web Site URL *"), _
   4: WebDescription("The site the list is located."), _
   5: Personalizable(PersonalizationScope.User)> _
   6: Public Property SiteURL() As String
   7:     Get
   8:         Return Me._siteurl
   9:     End Get
  10:     Set(ByVal value As String)
  11:         Me._siteurl = value
  12:     End Set
  13: End Property
  14:  
  15: Private _listGuid As String = ""
  16: <WebBrowsable(True), _
  17: WebDisplayName("Test Management List GUID *"), _
  18: WebDescription("Enter the GUID for the List."), _
  19: Personalizable(PersonalizationScope.User)> _
  20: Public Property ListGUID() As String
  21:     Get
  22:         Return Me._listGuid
  23:     End Get
  24:     Set(ByVal value As String)
  25:         Me._listGuid = value
  26:     End Set
  27: End Property
  28:  
  29: Private _camlQuery As String = ""
  30: <WebBrowsable(True), _
  31: WebDisplayName("CAML query for the webpart"), _
  32: WebDescription("Enter the CAML for the List."), _
  33: Personalizable(PersonalizationScope.User)> _
  34: Public Property CAML() As String
  35:     Get
  36:         Return Me._camlQuery
  37:     End Get
  38:     Set(ByVal value As String)
  39:         Me._camlQuery = value
  40:     End Set
  41: End Property

The first thing we need to do in the process is attach to the site and then web that we want we want to query. To do this I do the following:

   1: ' Create a Web object to use through the routine
   2: Dim Web as SPWeb
   3: ' Crate a local SPWeb Object
   4: Dim _thisweb As SPWeb
   5: ' Open the site with the URL supplied by the user
   6: Dim SiteColl As SPSite = New SPSite(SiteURL)
   7: _thisweb = SiteColl.OpenWeb
   8: ' Write the SPWeb Object to the property
   9: Web = _thisweb
  10: ' Clean up the SPWeb Object
  11: _thisweb.Dispose()
  12: _thisweb.Close()

This gives us an SPWeb object to play with. In the next snippet of code we attach to the list and create a CAML query object (SPQuery) that we will apply if it was supplied:

   1: ' Attach to the list
   2: Dim thisListGUID As Guid = New Guid(_listGuid)
   3: Dim list As SPList = Web.Lists(thisListGUID)
   4:  
   5: ' Define the Query
   6: Dim query As SPQuery = New SPQuery(list.DefaultView)
   7: If CAML <> "" Then
   8:     query.Query = CAML
   9: End If

Now we have all of that set up, we can make the LINQ query. in this query we are simply retrieving the data, but it is possible to sort, group by etc. as well.

   1: Dim linqQuery = From Z As SPListItem In list.GetItems(query) _
   2:                                   Select Z

Now we have the information, we can loop through it and do what we want with it. Below I have shown we can use a Lambda Query to group SharePoint information. In the example list I have Title, Category and Status fields and I want to group the information by the Category and then do something depending on the status, it may be some conditional formatting depending on other properties or maybe assign a task... anything really.

The reason for the substring is that SharePoint formats fields with an ID and then the Human Readable value, e.g. 1;#Status. We only want what is after the #.

   1: Dim CategoryTitle as String = ""
   2: For Each i In linqQuery.GroupBy(Function(a As SPListItem) a("Category"))
   3:     CategoryTitle = i.Key.ToString.Substring(i.Key.ToString.LastIndexOf("#") + 1)
   4:     For Each item In i
   5:         Select Case item("Status").ToString
   6:             Case "Open"
   7:                 ' Do Something
   8:             Case "Closed"
   9:                 ' Do Something
  10:             Case Else
  11:                 ' Do Something else
  12:         End Select
  13:     Next
  14: Next 

Hopefully that is a fairly quick and easy demo of how to get information from SharePoint as objects and use them!

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Accessing Database Images in ASP.NET and displaying them with .JPG extension

How to use .NET 3.5 SP1 to display images from a database so the user sees them with a .JPG extension.

In this post I am going to explain how to use the latest bits of the .NET framework (.NET 3.5 SP1) to display images from a database so the user sees them with a *.JPG extension. This is something that should be simple, yet, takes a little bit of work! The scenario I am using is as follows:

  • MSFT SQL 2005 Database
  • .NET 3.5 SP1 Entity Framework
  • .NET 2.5 SP1 Routing Engine
  • ASP.NET Web Forms
  • IIS 7 on a Vista PC (But works on Server 2008)

OK so to paint the picture, by the end of this we will be able to access an image from a URL that looks like this: http://yoursite.com/Image/imgID-1-T.jpg. This will return the image with ID of 1 and it will be a thumbnail image, but I will show how to display either the thumbnail or full size version, or what ever you want!

Displaying the images in an ASP.NET Webform

Rendering the image to a web page can be quite a cool thing to do, the code below takes a few different pieces of a pie and puts them nicely together! I will start off by accessing the data, then altering the images (if needed) and finally rendering the images to the page.

Note: As this page will be accessed via a routing rule that passes in the name of the JPG being accessed through, the JPG has to be named with the format of ImageType-ImageID-ImageSize.

For this example, I have created a folder called _Images. Inside that folder I have an aspx page called image.aspx. The actual ASPX page is standard as shown below:

   1: <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Image.aspx.vb" Inherits="Example.Image" EnableTheming="false" StylesheetTheme="" %>
   2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   3: <html xmlns="http://www.w3.org/1999/xhtml" >
   4: <head id="Head1" runat="server">
   5:     <title>Image</title>
   6: </head>
   7: <body>
   8:  
   9: </body>
  10: </html>

Database

The database in this example is a simple one table set-up with two columns in it, the table is called Images.

  • ImageID - Int field to hold the image ID
  • Image - Image field

Here is the SQL for the table:

   1: CREATE TABLE [dbo].[Images](
   2:     [ImageID] [int] IDENTITY(1,1) NOT NULL,
   3:     [Image] [image] NOT NULL,
   4:  CONSTRAINT [PK_Images] PRIMARY KEY CLUSTERED 
   5: (
   6:     [ImageID] ASC
   7: )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
   8: IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
   9: ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  10:  
  11: GO

 

Code Behind

My data-source is the Entity Framework (EF), but you could use any source to populate the code. First off I need to reference the System.Data namespaces that will allow me to work with the EF, also I have added a reference to the System.Drawing namespace that I will use to resize the images.

   1: Imports System.Data.Objects
   2: Imports System.Data.Objects.DataClasses
   3: Imports System.Drawing.Image

The Process of generating the image

In this section I will explain how the code below works and what is happening through out.

Here is how the code below works:

  1. First we need to create the EF context that we will use to get the images from the data classes. This is the ImageData definition.
  2. The first thing we need to do in the Page_Load is to split up the name of the file. I will explain later how the file is routed to this page, but as we are using the routing rules, we need to get this name from the HttpContect. ImageName is set in the routing rules to pass the name of the image through to this page (see the Routing the Images using a *.JPG File section). 
  3. Now that we have access to the data and the image ID, we need to get the image out of the database. This is done using a lambda query on the context we opened above. We pass this to an instance of the Images class (this is an EF generated class).
  4. As we are not rendering an aspx text page, we need to change the Content Type of the output, this is now "image/jpeg"
  5. The next part of the process is to resize the image. As part of the image naming process (e.g. <img scr="/image/ImgID-1-T.jpg">) you need to make sure you have the "-" in the same order as I have and that you are using a size character you have defined in the code below.
    Notice there are three options, T and S being the resize options and the final is just rendering the full size image.
   1: Dim ImageData as new ImageEntities
   2: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   3:     'Split the File Name out to get the information we need
   4:     Dim ImageInfo As String() = Context.Items("imagename").ToString.Split("-")
   5:     
   6:     'In this example I am only accessing one data source, you could access many,
   7:     'add a Select Case with the Image Type and select from other areas
   8:     Dim ImageType As String = ImageInfo(0) 
   9:     
  10:     'We need the ID of the image
  11:     Dim ImageID As Integer = ImageInfo(1)
  12:     
  13:     'We need to know what size to change it to
  14:     Dim ImageSize As String = ImageInfo(2)
  15:     
  16:     Dim Images As ImageModel.Images
  17:     Images = ShoppingData.Images.FirstOrDefault( _
  18:                         Function(i As Images) i.ImageID = ImageID)
  19:     
  20:     'Cast the Image data from the database to a Byte()
  21:     Dim aryContent As Byte() = DirectCast(Images.Image, Byte())
  22:     
  23:     'Create a new Memory Stream to hold the contents of the 
  24:     'image in ready for the Drawing.Image
  25:     Dim imgStream As New IO.MemoryStream
  26:     
  27:     Dim dummyCallBack As GetThumbnailImageAbort
  28:     dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort( _
  29:                                                         AddressOf ThumbnailCallback)
  30:  
  31:     imgStream.Write(aryContent, 0, aryContent.Length)
  32:  
  33:     Dim Img As System.Drawing.Image
  34:     Img = Drawing.Image.FromStream(imgStream)
  35:     
  36:     'Set the MIME tyoe
  37:     Response.ContentType = "image/jpeg"
  38:     'Resize the image if we need to
  39:     If ImageSize = "T" Then
  40:         'This will keep the image 100px high and change the width to keep the 
  41:         'ratio correct
  42:         Dim ratio = Img.Width / Img.Height
  43:         Dim height = 100
  44:         Dim width = 100 * ratio
  45:         Dim thumbnail As Drawing.Image = Img.GetThumbnailImage( _ 
  46:                                             width, height, dummyCallBack, IntPtr.Zero)
  47:         thumbnail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  48:     ElseIf ImageSize = "S" Then
  49:         'This will keep the image 100px wide and change the height to keep the 
  50:         'ratio correct
  51:         Dim ratio = Img.Height / Img.Width
  52:         Dim height = 100 * ratio
  53:         Dim width = 100
  54:         Dim thumbnail As Drawing.Image = Img.GetThumbnailImage(width, height, _
  55:                                                            dummyCallBack, IntPtr.Zero)
  56:         thumbnail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  57:     Else
  58:         Img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  59:     End If
  60:     Response.End()
  61: End Sub

The above code will now render an image, but will have to go via the routing engine. If you want to skip that step change the Context.Items code to a Request.QueryString and pass through some parameters that way.

Routing the Images using a *.JPG File

To route the images with a certain path and extension I used the example Chris Cavanagh gave on his blog. His example was in C#, so I converted it and have the VB example below. First off, I added a new class file to my project and put the routing functions in that. Then I referenced those classes from the global.asax.

PageRouter Class

The PageRouter class will handle all the setting of page routing rules. The first thing we have to do is reference some of the namespaces we are going to use in the class:

   1: Imports System
   2: Imports System.Web
   3: Imports System.Web.UI
   4: Imports System.Web.UI.HtmlControls
   5: Imports System.Web.UI.WebControls
   6: Imports System.Web.UI.WebControls.WebParts
   7: Imports System.Web.Routing
   8: Imports System.Web.Compilation

The first class we want to create is a page handler class that will have our custom IRouteHandler and will do the registering of our rules. For more information about what happens in this class, please read Chris' blog.

   1: Public Class WebFormRouteHandler(Of T As {IHttpHandler, New})
   2:         Implements IRouteHandler
   3:         Private _virtualpath
   4:         Public Property VirtualPath() As String
   5:             Get
   6:                 Return _virtualpath
   7:             End Get
   8:             Set(ByVal value As String)
   9:                 _virtualpath = value
  10:             End Set
  11:         End Property
  12:  
  13:         Public Sub New(ByVal virtualPath As String)
  14:             _virtualpath = virtualPath
  15:         End Sub
  16:  
  17: #Region "IRouteHandler Members"
  18:         Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) _
  19:         As System.Web.IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
  20:             For Each value In requestContext.RouteData.Values
  21:                 requestContext.HttpContext.Items(value.Key) = value.Value
  22:             Next
  23:             If (VirtualPath IsNot Nothing) Then
  24:                 Return (DirectCast(BuildManager.CreateInstanceFromVirtualPath(VirtualPath, GetType(T)), IHttpHandler))
  25:             Else
  26:                 Return New T()
  27:             End If
  28:         End Function
  29: #End Region
  30:  
  31: End Class

Once that is done we have to create our PageRouter class, we will reference this later in the Global.asax. This class will hold all of the routing rules we want to use in our project. This project calls on the WebFormRouteHandler to register the pages.

   1: Public Class PageRouter
   2:     Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
   3:             ' Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable 
   4:             ' automatic support on IIS6 and IIS7 classic mode 
   5:             routes.Add(New Route("Image/{imagename}.jpg", New WebFormRouteHandler(Of Page)("~/_Images/Image.aspx")))
   6:     End Sub
   7: End Class

The code above will catch any requests coming for images at http://yoursite.com/image/imgID-1-T.jpg and send them to our image page. You will notice this is where we set the name of the context item, {imagename}.

Global.asax Code

Finally we need to fire the PageRouter class and register the routes. When the application starts, I want to set all then routing rules, so in the global.asax I add the following code to the Application_Start subroutine. Please note, you will have to reference the System.Web namespace (Imports System.Web.Routing).

   1: Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
   2:     PageRouter.RegisterRoutes(RouteTable.Routes)
   3: End Sub

Finally

Hopefully that explains how to render a database image as a *.JPG using ASP.NET SP1! There are some cool things we can now do straight out of the box, and from what I have seen of SP1 I can't wait for it to be released!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Drop-Down List in a ListView InsertItem Template

Using a drop-down list in an List View control, "insert item template".

The ASP.NET 3.5 listview control is a great tool in the arsenal of a web developer! Coming from classic ASP, I thought the gridview was the best thing since sliced bread, but it has been well trumped by the listview. A really cool thing about the listview is the ability to insert a new item, straight out-of-the-box.

This feature is great until you want to put a drop-down box in the InsertItem template. I was hit by an error and I figure it is a result of there being no data for the drop-down to set as it's selected value when it loads. So, what do we do to fix this? Well here is my solution:

ASPX ListView Code

In the ListView control, navigate to your InsertItemTemplate tag. Place a drop-down list control in the column (<td>) where you want. Below I am using an Entity Data Source (Provided by the ADO.NET Entity Framework), but you could bind it any data source you want or add values in your code behind.

   1: <InsertItemTemplate>
   2:     <tr style="background-color: #05A705;">
   3:         <td>
   4:             <asp:DropDownList ID="ddlExample" runat="server" DataSourceID="EntityDataSource" DataTextField="DataName"
   5:                 DataValueField="DataID" AppendDataBoundItems="true">
   6:                 <asp:ListItem Selected="True" Text="Select a Value" Value="" />
   7:             </asp:DropDownList>
   8:         </td>
   9:     </tr>
  10: </InsertItemTemplate>

 

 

Notice how I don't have a "SelectedItem" tag in the Drop-down list control. Putting this in will cause all sorts of issues and you'll probably get an error that looks similar to this:

System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

On Item Inserting

Because the above drop-down list isn't bound to the ListView, we need to handle the inserting ourselves. The code below shows how to modify the ListView property with a value we want, namely the selected item in the drop-down list. In the code I have to find the drop-down list in the InsertItem Template.

   1: Private Sub lvExample_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles lvExample.ItemInserting
   2:     e.Values("ExampleID") = DirectCast( _
   3:                 lvExample.InsertItem.FindControl("ddlExample"), DropDownList).SelectedValue
   4: End Sub

The above code can be used to add any values to items in your ListView.

Currently rated 4.5 by 12 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5