Ask for username and password when calling reporting service

These code allow us to set username and password for reporting services to avoid popup request authentication.

This code tested in Visual Studio 2008 and Reporting Services from SQL Server 2005

First create a class called ReportServerCredentials.vb

Imports Microsoft.VisualBasic
Imports Microsoft.Reporting.WebForms
Imports System.Security.Principal

Public NotInheritable Class ReportServerCredentials
    Implements IReportServerCredentials

    Public ReadOnly Property ImpersonationUser() As WindowsIdentity _
            Implements IReportServerCredentials.ImpersonationUser
        Get

            'Use the default windows user.  Credentials will be
            'provided by the NetworkCredentials property.
            Return Nothing

        End Get
    End Property

    Public ReadOnly Property NetworkCredentials() As Net.ICredentials _
            Implements IReportServerCredentials.NetworkCredentials
        Get

            'Read the user information from the web.config file.
            'By reading the information on demand instead of storing
            'it, the credentials will not be stored in session,
            'reducing the vulnerable surface area to the web.config
            'file, which can be secured with an ACL.

            'User name
            Dim userName As String = _
                ConfigurationManager.AppSettings("UserName")

            If (String.IsNullOrEmpty(userName)) Then
                Throw New Exception("Missing user name from web.config file")
            End If

            'Password
            Dim password As String = _
                ConfigurationManager.AppSettings("Password")

            If (String.IsNullOrEmpty(password)) Then
                Throw New Exception("Missing password from web.config file")
            End If

            'Domain
            Dim domain As String = _
                ConfigurationManager.AppSettings("SERVERNAME")

            If (String.IsNullOrEmpty(domain)) Then
                Throw New Exception("Missing domain from web.config file")
            End If

            Return New Net.NetworkCredential(userName, password, domain)

        End Get
    End Property

    Public Function GetFormsCredentials(ByRef authCookie As System.Net.Cookie, _
                                        ByRef userName As String, _
                                        ByRef password As String, _
                                        ByRef authority As String) _
                                        As Boolean _
            Implements IReportServerCredentials.GetFormsCredentials

        authCookie = Nothing
        userName = Nothing
        password = Nothing
        authority = Nothing

        'Not using form credentials
        Return False

    End Function

End Class 

Second, add parameter key to your web.config

<appSettings>
<add key="UserName" value="UserName"/>
		<add key="Password" value="Password"/>
		<add key="SERVERNAME" value="SERVERNAME"/>
</appSettings>

And then add this line of code before calling reportserverURL in the report view page:

ReportViewer1.ServerReport.ReportServerCredentials = New ReportServerCredentials()
  • Share/Bookmark

Send Email with multiple attachments from VB.NET

This code show how to send email multiple attachments from vb.net by using System.Net.Mail

1. This is how you use it

Dim fileAttch = New ArrayList
fileAttch.Add("C:\test.csv")
fileAttch.Add("C:\test2.csv")
fileAttch.Add("C:\test3.csv")

sendEmail.send(your_emailHost, "no-reply@company.com", your_EmailTo, your_EmailSubject, your_EmailBody, your_EmailccTo, fileAttch)


Read the rest of this entry »

  • Share/Bookmark

Get first day of the month and last day of the month in VB.NET

This code show how to get first date of the month and last day of the month date.

Private Function GetFirstDayOfMonth(ByVal dtDate As DateTime) As DateTime
        Dim dtFrom As DateTime = dtDate
        dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1))
        Return dtFrom
    End Function

    Private Function GetLastDayOfMonth(ByVal dtDate As DateTime) As DateTime
        Dim dtTo As New DateTime(dtDate.Year, dtDate.Month, 1)
        dtTo = dtTo.AddMonths(1)
        dtTo = dtTo.AddDays(-(dtTo.Day))
        Return dtTo
    End Function

'if you put code like:
GetFirstDayOfMonth("2010-05-05") ' It will return = 2010-05-01
GetLastDayOfMonth("2010-05-05") ' It will return = 2010-05-31
  • Share/Bookmark

VB.NET Set Data Table to CSV File

This function show how to Convert DataTable to CSV File

    Sub SetDataTable_To_CSV(ByVal dtable As DataTable, ByVal path_filename As String, ByVal sep_char As String)
        Dim writer As System.IO.StreamWriter
        Try
            writer = New System.IO.StreamWriter(path_filename)

            Dim _sep As String = ""
            Dim builder As New System.Text.StringBuilder
            For Each col As DataColumn In dtable.Columns
                builder.Append(_sep).Append(col.ColumnName)
                _sep = sep_char
            Next
            writer.WriteLine(builder.ToString())

            For Each row As DataRow In dtable.Rows
                _sep = ""
                builder = New System.Text.StringBuilder

                For Each col As DataColumn In dtable.Columns
                    builder.Append(_sep).Append(row(col.ColumnName))
                    _sep = sep_char
                Next
                writer.WriteLine(builder.ToString())
            Next
        Catch ex As Exception

        Finally
            If Not writer Is Nothing Then writer.Close()
        End Try
    End Sub

This is how you use it

Dim dt As New DataTable
SetDataTable_To_CSV(dt, "C:\test.csv", ",")
  • Share/Bookmark

Its not Iron Man but its Iron baby

A VIDEO of a baby girl dressed as Iron Man has become an internet sensation after the child’s filmmaker father posted the clip on YouTube.

Iron Baby, which features Margaret transforming from toddler to iron-clad superhero, has attracted 2.6 million hits so far, the Daily Mail reports.

The parody short film was directed by Margaret’s father Patrick Boivin, who is a self-taught filmmaker and former comic book creator from Montreal.

3-D artist Jocelyn Strob Simard did the special effects for the film, including making the Iron Man costume, according to the Toronto Star.

The clip starts with Margaret as a bib-wearing Tony Stark, who is transformed by her mini-armoured costume into Iron Man.

The tiny superhero flies off to fight gun-toting toy bunnies, who she destroys with laser beams fired from the palm of her hand.

Costing virtually nothing, the short film took two months to make.

Source: http://www.heraldsun.com.au

  • Share/Bookmark

Marina Bay Sands Singapore

sands

Its Saturday morning, I took this picture at 6am. Marina Bay Sands 95% completed.

  • Share/Bookmark

How to set command Timeout for getData() in TableAdapter

One of the disadvantages when we using tableadapter to retrieve the data from database is they do not provide property to set command timeout (SHIT). This giving me a huge headache for my project which most of the entire project is using tableadapter instead of creating my own DAL or class.
After do some research on the internet, there is 1 ways to add command timeout in tableadapter dataset (.xsd file) which is

create new property inside partial class:
Double click on xxxTableAdaper and it will show you behind code in .vb file of xsd file. Add this code inside Partial Public Class xxxDataTableAdapter

Namespace xxxTableAdapters
    Partial Public Class yyyTableAdapter
        Public WriteOnly Property CommandTimeout() As Integer
            Set(ByVal value As Integer)
                Dim i As Integer = 0
                While (i < Me.CommandCollection.Length)
                    If (Me.CommandCollection(i) IsNot Nothing) Then
                        Me.CommandCollection(i).CommandTimeout = value
                    End If
                    i = (i + 1)
                End While
            End Set
        End Property
    End Class
End Namespace

And how you use it, you just add 1 line of code:

            Dim dt As New DataSet1TableAdapters.AddressTableAdapter
            dt.CommandTimeout = 120
            gridview1 = dt.GetData()

Notes:
xxxTableAdapters , xxx you replace with the name of .xsd file
yyyTableAdapter you replace with the name of table adapter

This code is tested in visual studio 2008 and 2005

  • Share/Bookmark

VB.NET Date Time Format Patterns

Standard Format Example List:

Format Code Result
MM/dd/yyyy Date.Now().ToString(”d”) 1/15/2010
dddd, dd MMMM yyyy Date.Now().ToString(”D”) Friday, January 15, 2010
dddd, dd MMMM yyyy hh:mm tt Date.Now().ToString(”f”) Friday, January 15, 2010 11:06 AM
dddd, dd MMMM yyyy HH:mm:ss Date.Now().ToString(”F”) Friday, January 15, 2010 11:06:46 AM
MM/dd/yyyy h:mm tt Date.Now().ToString(”g”) 1/15/2010 11:06 AM
MM/dd/yyyy HH:mm:ss Date.Now().ToString(”G”) 1/15/2010 11:06:46 AM
MMMM dd Date.Now().ToString(”m”) January 15
dd MMM yyyy HH’:'mm’:’ss ‘GMT’ Date.Now().ToString(”r”) Fri, 15 Jan 2010 11:06:46 GMT
yyyy’-'MM’-'dd’T'HH’:'mm’:’ss Date.Now().ToString(”s”) 2010-01-15T11:06:46
yyyy’-'MM’-'dd HH’:'mm’:’ss’Z’ Date.Now().ToString(”u”) 2010-01-15 11:06:46Z
dddd, MMMM dd yyyy HH:mm:ss Date.Now().ToString(”U”) Friday, January 15, 2010 3:06:46 AM
yyyy MMMM Date.Now().ToString(”y”) January, 2010

Read the rest of this entry »

  • Share/Bookmark

How to format date time in SQL? – SQL Server Date Time Format

getdate() = Jan 14 2010 10:00:05:190PM

Format Query Result
USA mm/dd/yy select convert(varchar, getdate(), 1) 01/14/10
ANSI yy.mm.dd select convert(varchar, getdate(), 2) 10.01.14
British/French dd/mm/yy select convert(varchar, getdate(), 3) 14/01/10
German dd.mm.yy select convert(varchar, getdate(), 4) 14.01.10
Italian dd-mm-yy select convert(varchar, getdate(), 5) 14-01-10
dd mon yy select convert(varchar, getdate(), 6) 14 Jan 10
Mon dd, yy select convert(varchar, getdate(), 7) Jan 14, 10
HH:MI:SS select convert(varchar, getdate(), 8 ) 21:54:31
Mon dd yyyy H:MI:SS:msAM/PM select convert(varchar, getdate(), 9) Jan 14 2010 9:54:56:490PM
USA mm-dd-yy select convert(varchar, getdate(), 10) 01-14-10
JAPAN yy/mm/dd select convert(varchar, getdate(), 11) 10/01/14
ISO yymmdd select convert(varchar, getdate(), 12) 100114
dd Mon yyyy HH:MI:MS:SS select convert(varchar, getdate(), 13) 14 Jan 2010 21:57:39:070
HH:MI:MS:SS select convert(varchar, getdate(), 14) 21:58:21:263
mon dd yyyy hh:miAM (or PM) select convert(varchar, getdate(), 100) Jan 14 2010 9:59PM
mm/dd/yyyy select convert(varchar, getdate(), 101) 01/14/2010
yyyy.mm.dd select convert(varchar, getdate(), 102) 2010.01.14
dd/mm/yyyy select convert(varchar, getdate(), 103) 14/01/2010
dd.mm.yyyy select convert(varchar, getdate(), 104) 14.01.2010
dd-mm-yyyy select convert(varchar, getdate(), 105) 14-01-2010
dd mon yyyy select convert(varchar, getdate(), 106) 14 Jan 2010
Mon dd, yyyy select convert(varchar, getdate(), 107) Jan 14, 2010
hh:mm:ss select convert(varchar, getdate(), 108) 21:59:59
Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM) select convert(varchar, getdate(), 109) Jan 14 2010 10:00:05:190PM
mm-dd-yyyy select convert(varchar, getdate(), 110) 01-14-2010
yyyy/mm/dd select convert(varchar, getdate(), 111) 2010/01/14
yyyymmdd select convert(varchar, getdate(), 112) 20100114
Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h) select convert(varchar, getdate(), 113) or select convert(varchar, getdate(), 13) 14 Jan 2010 22:00:35:107
hh:mi:ss:mmm(24h) select convert(varchar, getdate(), 114) 22:00:40:423

References:
http://www.technoreader.com/SQL-Server-Date-Time-Format.aspx
http://www.sql-server-helper.com/tips/date-formats.aspx

  • Share/Bookmark

Chinese Garden Singapore January 01 / 2009

These are a view photo that I took from my Canon 450D. The location of this is Chinese garden in Singapore. I must say, it is very beautiful park they got in there. Is not only Chinese garden, but there is also Japanese garden.

IMG_6117 IMG_6101 IMG_6088 IMG_6087 IMG_6037 IMG_6027 IMG_6008 IMG_6007 IMG_6005 Pagoda
  • Share/Bookmark