Archive for June, 2010

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