VB.NET

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

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

VB.NET: How to implement Group By in DataSet using LINQ

In this post I will show you the power of LINQ. At first I never try LINQ even when I developing project using Visual Studio 2008. But since I have no way to find a solution to one of my project, I start to explore LINQ which is give me a thought of how powerful and simple it become when doing a coding using LINQ.
Read the rest of this entry »

  • Share/Bookmark

ASP.NET: How to read XML Data String to Dataset


The output of XML String:

<?xml version="1.0" encoding="utf-8" ?>
- <NewDataSet>
- <Table>
 <CONDOM>Durex</CONDOM>
 <SIZE>XL</SIZE>
 <QTY>123</QTY>
 <COLOR>616</COLOR>
 </Table>
 </NewDataSet>

In order to convert from XML to dataset, this is the code:
Read the rest of this entry »

  • Share/Bookmark

ASP.NET: Convert String XML to XML Node

For example I have string value like this:

    <?xml version="1.0"?>
           <Product type="Condom">
                <CondomName>durex</CondomName>
                <ExpDate>False</ExpDate>
                <Type>False</Type>
                <Color>False</Color>
                <Qty>False</Qty>
            </Product>

Since this this is a string value, so we need to convert to xml node to be able to process.
So this is the code:
Read the rest of this entry »

  • Share/Bookmark

ASP.NET: How to use Javascript alert inside AJAX UpdatePanel

Normally when we add Javascript Alert to our asp.net it will be like this:
Page. ClientScript.RegisterStartupScript(this.GetType(), "winPop", "alert('Update is successful')
but this code is not working if you using the code inside update panel.
So in order to add javascript in update panel you need to register you client script to your script manager as shown below:
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Update is successfu.');",True)


  • Share/Bookmark

SSIS: Update data from different table if data is Null

SSIS: SSIS stands for SQL Server Integration Services. It is the new data transformation standard for SQL Server 2005 and has replaced the old SQL Server Data Transformation Services.

In this post, I want to show how to update data from different table with condition that the data is Null.
Read the rest of this entry »

  • Share/Bookmark