Wednesday, April 18, 2012

How to Import Spreadsheets in Dot Net in Memory

Steps:

  1. Create an Import Page
  2. Capture the Upload File
  3. Process the Import

1. Create an Import Page

<%@ Page Language="vb" AutoEventWireup="false" Inherits="Admin.ProjectImport" CodeFile="ProjectImport.aspx.vb"
MasterPageFile="~/Shared/template/AppMasterPage.master" Title="Import Projects" %>

<asp:Content ContentPlaceHolderID="ActionBarContentPlaceHolder" runat="server">
<dc:ActionButton ID="ImportButton" Text="Import" runat="server" HasLeadingBullet="false" />
</asp:Content>
<asp:Content ContentPlaceHolderID="WorkSpaceContentPlaceHolder" runat="server">
<asp:Label ID="Label1" runat="server">Enter location</asp:Label>
<asp:FileUpload id="FileUpload1" runat="server" />
<asp:ObjectDataSource ID="ImportProjectLogic" runat="server"
InsertMethod="Add"
TypeName="DC.Business.ImportProject" >
<InsertParameters>
<asp:Parameter Name="attachment" Type="Object" />
</InsertParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ProgrammeLogic" runat="server"
InsertMethod="Insert"
TypeName="DC.Business.Programme">
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ProjectTypeLogic" runat="server"
InsertMethod="Insert"
TypeName="DC.Business.ProjectType">
</asp:ObjectDataSource>
</asp:Content>
<asp:Content ContentPlaceHolderID="StatusBarContentPlaceHolder" runat="server">
<asp:Label ID="ErrorMessageLabel" runat="server" ForeColor="Red"></asp:Label><br />
</asp:Content>




2. Capture the Upload File



Imports DC.Model

Namespace Admin

Partial Class ProjectImport
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Select Case Request.QueryString("Err")
Case 1
Dim sheetName As String = ConfigurationManager.AppSettings("SheetName")
ErrorMessageLabel.Text = "Could not find sheet named: " + sheetName
Exit Sub
Case 2
Dim columnProjectIDName As String = ConfigurationManager.AppSettings("ProjectID")
ErrorMessageLabel.Text = "Could not find column named: " + columnProjectIDName
Exit Sub
End Select

Session("Requery") = "No"

End Sub

Protected Sub ImportButton_Click() Handles ImportButton.Click
If FileUpload1.FileName = "" Then
ErrorMessageLabel.Text = "Error: you must enter a file name"
Return
End If

If UCase(Right(FileUpload1.FileName, 3)) <> "XLS" Then
ErrorMessageLabel.Text = "Must be an Excel spreadsheet."
Return
End If

If Not (FileUpload1.PostedFile Is Nothing) Then
ErrorMessageLabel.Text = ""
Try
ImportProjectLogic.Insert()
ProgrammeLogic.Insert()
ProjectTypeLogic.Insert()
Catch ex As Exception
While ex IsNot Nothing
ErrorMessageLabel.Text &= ex.Message & " "
ex = ex.InnerException
End While
Exit Sub
End Try
Response.Redirect("ProjectProcess.aspx")
End If
End Sub

Protected Sub ImportProjectLogic_Inserting(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) _
Handles ImportProjectLogic.Inserting
Dim iod As IOrderedDictionary = e.InputParameters
iod("attachment") = FileUpload1.PostedFile.InputStream
End Sub
End Class


End Namespace


 


3. Process the Import



Imports SubSonic
Imports DAL = DC.DataAccess
Imports Columns = DC.DataAccess.ImportProject.Columns
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Text
Imports Excel

<System.ComponentModel.DataObject()>
Public Class ImportProject

Private _qry As New SqlQuery
Private _ret As New DAL.ImportProjectCollection

<System.ComponentModel.DataObjectMethod(
ComponentModel.DataObjectMethodType.Select, True)>
Public Function GetData(ByVal projectID As Integer) As DAL.ImportProjectCollection
_qry.From(DAL.ImportProject.Schema)
If projectID > 0 Then _qry.Where(Columns.ProjectID).IsEqualTo(projectID)
_ret.LoadAndCloseReader(_qry.ExecuteReader)
Return _ret
End Function

<System.ComponentModel.DataObjectMethod(
ComponentModel.DataObjectMethodType.Insert, True)>
Public Function Add(
ByVal projectID As String,
ByVal projectName As String,
ByVal projectManagerName As String,
ByVal status As String,
ByVal programme As String,
ByVal programmeManager As String,
ByVal currentPhase As String,
ByVal projectType As String,
ByVal businessOwner As String) As Boolean

Dim dr As DAL.ImportProject = DAL.ImportProject.FetchByID(projectID)
If dr Is Nothing Then dr = New DAL.ImportProject

Try
dr.ProjectID = projectID
dr.ProjectManagerName = projectManagerName
dr.ProjectName = projectName
dr.Status = status
dr.Programme = programme
dr.ProgrammeManager = programmeManager
dr.CurrentPhase = currentPhase
dr.ProjectType = projectType
dr.BusinessOwner = businessOwner
dr.Save()
Return True
Catch ex As Exception
Throw New ApplicationException("Unable to add new record.", ex)
Return False
End Try
End Function

<System.ComponentModel.DataObjectMethodAttribute(
System.ComponentModel.DataObjectMethodType.Insert, False)>
Public Function Add(ByVal attachment As Stream) As Boolean

Try
'0. Delete all projects
DeleteProjects(0)
'2. Reading from a binary Excel file ('97-2003 format; *.xls)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(attachment)
'3. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = True
Dim result As DataSet = excelReader.AsDataSet()
Dim dt As DataTable = result.Tables(0)

Dim projectID As String
Dim projectName As String
Dim projectManager As String
Dim projectStatus As String
Dim programme As String
Dim programmeManager As String
Dim currentPhase As String
Dim projectType As String
Dim businessOwner As String

For Each dr As DataRow In dt.Rows
projectID = GetValueOrNull(dr("Project ID").ToString)
projectName = StripIdFromName(dr("Project Name").ToString)
projectManager = GetValueOrNull(dr("Project Manager").ToString)
projectStatus = GetValueOrNull(dr("Telecom Status").ToString)
programme = GetValueOrNull(dr("Programme").ToString)
programmeManager = GetValueOrNull(dr("Programme Manager").ToString)
currentPhase = GetValueOrNull(dr("Current Phase").ToString)
projectType = GetValueOrNull(dr("Project Type").ToString)
businessOwner = GetValueOrNull(dr("Business Owner").ToString)

Add(projectID,
projectName,
projectManager,
projectStatus,
programme,
programmeManager,
currentPhase,
projectType,
businessOwner)

Next
Return True
Catch ex As Exception
Throw New ApplicationException("Unable to import Projects.", ex)
End Try
End Function

Private Function GetValueOrNull(ByVal value As String) As String
If value = "" Then
Return vbNullString
Else
Return value
End If
End Function

Private Function StripIdFromName(ByVal value As String) As String
Dim retVal As String
If IsDBNull(value) Then
retVal = vbNullString
Else
Dim projectNameList = value.Split("-")
If projectNameList.Length = 1 Then
retVal = projectNameList(0)
Else
retVal = ""
For i As Integer = 1 To projectNameList.Length - 1
retVal += projectNameList(i) & "-"
Next
retVal = Left(retVal, Len(retVal) - 1)
End If
End If
Return retVal

End Function

<System.ComponentModel.DataObjectMethod(
ComponentModel.DataObjectMethodType.Delete, True)>
Public Function DeleteProjects(ByVal projectID As Integer) As Boolean
Dim delQuery As New DeleteQuery(DAL.ImportProject.Schema)
If projectID > 0 Then delQuery.AddWhere(Columns.ProjectID, projectID)
Try
delQuery.Execute()
Return True
Catch ex As Exception
Throw New ApplicationException(ex.Message)
Return False
End Try
End Function

End Class

Thursday, March 15, 2012

Adding a dynamic “Contact Us” page to your website

I just added a dynamic Contact Us page to my charity websites:

This is where a signed on administrator can add a new contact person to the page, without having to edit the HTML page and save/overwrite it back.

The secret is to store everything in a database, then output the results of a database query.

Here’s how it’s done (an overview, followed by detailed instructions below):

  1. Create database table “Contact”
  2. Import Contact class using an Object Relational Mapper (such as SubSonic).
  3. Wrap some ComponentModel tags around the ORM class in the business layer.
  4. Output the results of the SELECT in a GridView.  (Code some input fields which are only visible to users with Admin role. )
  5. Render the image using a Handler.
  6. Capture the input fields and store them back into the database.

1. Create database table “Contact”

CREATE TABLE [dbo].[Contact](
[ContactID] [int] IDENTITY(1,1) NOT NULL,
[FullName] [nvarchar](100) NOT NULL,
[ImageType] [nvarchar](50) NOT NULL,
[JobTitle] [nvarchar](50) NOT NULL,
[PhoneNumber] [nvarchar](20) NULL,
[UpdatedBy] [nvarchar](20) NOT NULL,
[UpdatedOn] [smalldatetime] NOT NULL,
[Attachment] [image] NULL,
[EmailAddress] [nvarchar](100) NULL,
[FileName] [nvarchar](100) NULL,
CONSTRAINT [PK_Contact] PRIMARY KEY CLUSTERED
(
[ContactID] ASC
)



2. Import Contact class using an ORM



image



3. Wrap some ComponentModel tags around the ORM class



Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Text
Imports DAL = Align.DataAccess
Imports columns = Align.DataAccess.Contact.Columns
Imports SubSonic

<System.ComponentModel.DataObject()>
Public Class Contact

Dim _qry As SqlQuery = New SqlQuery()
Dim _ret As DAL.ContactCollection = New DAL.ContactCollection()


<System.ComponentModel.DataObjectMethodAttribute(
System.ComponentModel.DataObjectMethodType.Select, True)>
Public Function GetData(ByVal ContactID As Integer) As DAL.ContactCollection
_qry.From(DAL.Contact.Schema)
If (ContactID > 0) Then _qry.Where(columns.ContactID).IsEqualTo(ContactID)
_qry.OrderAsc(columns.ContactID)
_ret.LoadAndCloseReader(_qry.ExecuteReader())
Return _ret
End Function

<System.ComponentModel.DataObjectMethodAttribute(
System.ComponentModel.DataObjectMethodType.Insert, True)>
Public Function Insert(
ByVal fullName As String,
ByVal imageType As String,
ByVal jobTitle As String,
ByVal phoneNumber As String,
ByVal emailAddress As String,
ByVal attachment As Stream,
ByVal userName As String,
ByVal fileName As String) As Boolean

Dim dr As DAL.Contact = New DAL.Contact()

dr.FullName = fullName
dr.ImageType = imageType
dr.JobTitle = jobTitle
dr.PhoneNumber = phoneNumber
dr.EmailAddress = emailAddress
dr.UpdatedBy = userName
dr.UpdatedOn = DateTime.Now
If (Not attachment Is Nothing) Then
Dim docLength As Int32 = attachment.Length
Dim docBuffer(docLength) As Byte
attachment.Read(docBuffer, 0, docLength)
dr.Attachment = docBuffer
dr.FileName = fileName
End If

Try
dr.Save(userName)
Return True
Catch ex As Exception
Throw New ApplicationException("Unable to save new Contact", ex)
End Try
End Function

<System.ComponentModel.DataObjectMethod(
System.ComponentModel.DataObjectMethodType.Delete, True)>
Public Function Delete(ByVal contactID As Integer)
Dim delQuery As DeleteQuery = New DeleteQuery(DAL.Contact.Schema)
delQuery.WHERE(columns.ContactID, contactID)

Try
delQuery.Execute()
Return True
Catch ex As Exception
Throw New ApplicationException("Unable to delete Contact", ex)
End Try
End Function

End Class



4. Output the results of the SELECT in a GridView



<%@ Page Title="Contact Us" MasterPageFile="~/site.master" CodeFile="~/Community/contact_us.aspx.vb"
Inherits="Community.contact_us" %>


<asp:Content ContentPlaceHolderID="HeaderContentPlaceHolder" runat="server">
Contact Us
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="WorkSpaceContentPlaceHolder" runat="server">
<p>
For further information regarding Align Church, please contact us at the following
numbers.</p>
<asp:HiddenField ID="UserName_HiddenField" runat="server" />
<asp:Label ID="Err_Label" runat="server" ForeColor="Red"></asp:Label>
<asp:GridView ID="GridView1" BorderWidth="0px" CellPadding="0" Style="border-collapse: collapse;"
Width="100%" runat="server" AutoGenerateColumns="False" DataSourceID="Contact_Logic"
DataKeyNames="ContactID" ShowHeader="False" EnableModelValidation="True">
<HeaderStyle CssClass="style1" />
<RowStyle CssClass="style2" />
<AlternatingRowStyle CssClass="style3" />
<Columns>
<asp:ImageField DataImageUrlField="ContactID" DataImageUrlFormatString="GetDbImage.ashx?Id={0}" ControlStyle-Width="100px" />
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td class="style1">
Name
</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("FullName") %>' />
</td>
</tr>
<tr>
<td class="style1">
Title
</td>
<td>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("JobTitle") %>' />
</td>
</tr>
<tr>
<td class="style1">
Phone
</td>
<td>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("PhoneNumber") %>' />
</td>
</tr>
<tr>
<td class="style1">
Email
</td>
<td>
<asp:HyperLink ID="Label9" runat="server" Text='<%# Eval("EmailAddress") %>' NavigateUrl='<%#Eval("EmailAddress","mailto:{0}") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="Contact_Logic" runat="server" TypeName="Align.Business.Contact"
SelectMethod="GetData" DeleteMethod="Delete" InsertMethod="Insert">
<DeleteParameters>
<asp:Parameter Name="contactID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:ControlParameter Name="fullName" Type="String" ControlID="FullName_TextBox"
PropertyName="Text" />
<asp:Parameter Name="attachment" Type="Object" />
<asp:Parameter Name="imageType" Type="String" />
<asp:Parameter Name="fileName" Type="String" />
<asp:ControlParameter Name="jobTitle" Type="String" ControlID="JobTitle_TextBox"
PropertyName="Text" />
<asp:ControlParameter Name="phoneNumber" Type="String" ControlID="PhoneNumber_TextBox"
PropertyName="Text" />
<asp:ControlParameter Name="emailAddress" Type="String" ControlID="EmailAddress_TextBox"
PropertyName="Text" />
<asp:ControlParameter Name="userName" Type="String" ControlID="UserName_HiddenField"
PropertyName="Value" />
</InsertParameters>
<SelectParameters>
<asp:Parameter Name="ContactID" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:ObjectDataSource>
<br />
<asp:Table ID="insert_table" runat="server" Width="100%">
<asp:TableHeaderRow CssClass="style1">
<asp:TableHeaderCell>Photo</asp:TableHeaderCell>
<asp:TableHeaderCell>Full Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Job Title</asp:TableHeaderCell>
<asp:TableHeaderCell>Phone Number</asp:TableHeaderCell>
<asp:TableHeaderCell>Email Address</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow CssClass="style2">
<asp:TableCell>
<asp:FileUpload ID="FileUpload1" runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="FullName_TextBox" runat="server" /></asp:TableCell><asp:TableCell>
<asp:TextBox ID="JobTitle_TextBox" runat="server" /></asp:TableCell><asp:TableCell>
<asp:TextBox ID="PhoneNumber_TextBox" runat="server" /></asp:TableCell><asp:TableCell>
<asp:TextBox ID="EmailAddress_TextBox" runat="server" /></asp:TableCell></asp:TableRow></asp:Table><asp:LinkButton ID="Insert_Button" Text="Insert" CommandName="Insert" runat="server">
</asp:LinkButton><br />
<table>
<tr>
<td valign="top">
<b>Address</b> </td><td>
<p>
Taita Community Hall<br />
22 Taine Street<br />
Taita<br />
Lower Hutt</p><hr />
</td>
</tr>
<tr>
<td valign="top">
<b>Map</b> </td><td>
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0"
marginwidth="0" src="http://maps.google.co.nz/maps?q=Taita+Community+Hall&amp;hl=en&amp;cd=1&amp;ei=yZG2S6_iCp2uiwPXyMGYBw&amp;sig2=ii97zdnzh-Ye38TgUgVLiw&amp;ie=UTF8&amp;view=map&amp;cid=10505385825416012616&amp;ved=0CBoQpQY&amp;hq=Taita+Community+Hall&amp;hnear=&amp;ll=-41.179429,174.958979&amp;spn=0.006295,0.006295&amp;iwloc=A&amp;layer=c&amp;cbll=-41.179591,174.958736&amp;panoid=cBJq7GpBqDsMcOoNOA7sfw&amp;cbp=12,31.86,,0,5.53&amp;source=embed&amp;output=svembed">
</iframe>
<br />
<small><a href="http://maps.google.co.nz/maps?q=Taita+Community+Hall&amp;hl=en&amp;cd=1&amp;ei=yZG2S6_iCp2uiwPXyMGYBw&amp;sig2=ii97zdnzh-Ye38TgUgVLiw&amp;ie=UTF8&amp;view=map&amp;cid=10505385825416012616&amp;ved=0CBoQpQY&amp;hq=Taita+Community+Hall&amp;hnear=&amp;ll=-41.179429,174.958979&amp;spn=0.006295,0.006295&amp;iwloc=A&amp;layer=c&amp;cbll=-41.179591,174.958736&amp;panoid=cBJq7GpBqDsMcOoNOA7sfw&amp;cbp=12,31.86,,0,5.53&amp;source=embed"
style="color: #0000FF; text-align: left">View Larger Map</a></small> <hr />
</td>
</tr>
<tr>
<td valign="top">
<b>Bank Account</b> </td><td>
<p>
If you have prayefully considered and decide to make Align Church your spiritual
home, or if you earnestly desire to bless the ministry of Align Church to the Taita
community, then you might want to contribute an offering to this ministry.</p><p>
Align Church is a registered charity with the New Zealand Charities Commission.
Your donations are tax-deductible. </p><p>
Our bank account number (for Internet Banking and/or Direct Debits) is: </p><p>
ALIGN CHURCH TRUST 38-9010-0431534-00 </p><blockquote>
<strong>But just as you excel in everything - in faith, in speech, in knowledge, in
complete earnestness and in your love for us - see that you also excel in the grace
of giving.</strong></blockquote><blockquote>
<em>2 Corinthians 8:7 </em></blockquote><p>
Please clearly identify yourself, so we can send a tax receipt at year end.</p><hr />
</td>
</tr>
</table>
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="head">
</asp:Content>


5. Render the image using a Handler



<%@ WebHandler Language="VB" Class="GetDbImage" %>

Imports System
Imports System.Web
Imports DAL = Align.DataAccess

Public Class GetDbImage : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.Clear()

If Not context.Request.QueryString("ID") Is Nothing Then
Dim theContact As DAL.Contact = DAL.Contact.FetchByID(context.Request.QueryString("ID"))

context.Response.ContentType = theContact.ImageType
context.Response.BinaryWrite(theContact.Attachment)
Else
context.Response.ContentType = "text/plain"
context.Response.Write("No image")
End If
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class


6. Capture the input fields and store them back into the database



Imports Align.Business
Imports System.Security.Principal
Imports System.IO
Imports System.Data.SqlClient
Imports System.Xml.Linq

Namespace Community

Public Class contact_us
Inherits Page

Private p As IPrincipal = HttpContext.Current.User

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.IsPostBack Then Exit Sub

If p.Identity.IsAuthenticated Then UserName_HiddenField.Value = p.Identity.Name
If p.IsInRole("Administrator") Then
insert_table.Visible = True
Insert_Button.Visible = True
Else
insert_table.Visible = False
Insert_Button.Visible = False
End If
End Sub

Protected Sub Contact_Logic_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles Contact_Logic.Inserted
If Not e.Exception Is Nothing Then
Err_Label.Text = e.Exception.Message
End If
End Sub

Protected Sub Insert_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Insert_Button.Click
Contact_Logic.Insert()
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim theRow As GridViewRow = e.Row
If theRow.RowType = DataControlRowType.Header Then Exit Sub

Dim theCell As DataControlFieldCell = theRow.Cells(2)
For Each ctl As Control In theCell.Controls
If TypeOf ctl Is LinkButton Then
If p.IsInRole("Administrator") Then
ctl.Visible = True
Else
ctl.Visible = False
End If
End If
Next
End Sub

Protected Sub Contact_Logic_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles Contact_Logic.Inserting
Dim iod As IOrderedDictionary = e.InputParameters
iod("attachment") = FileUpload1.PostedFile.InputStream
iod("imageType") = FileUpload1.PostedFile.ContentType
iod("fileName") = FileUpload1.FileName
End Sub
End Class
End Namespace