Posts tagged VB.NET

VB.NET – ASP.NET communication between user control and web page using event handler

In this post, we want to show how button in user control communicate with web page in asp.net

First create user control called button.ascx.

Add 3 button, name it btnSave, btnEdit and btnCancel.

in button.ascx back code, add this code:

Public Partial Class button
    Inherits System.Web.UI.UserControl
    Public Event btnSaveHandler As System.EventHandler
    Public Event btnEditHandler As System.EventHandler
    Public Event btnCancelHandler As System.EventHandler

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

    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        RaiseEvent btnSaveHandler(Me, New EventArgs())
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEdit.Click
        RaiseEvent btnEditHandler(Me, New EventArgs())
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        RaiseEvent btnCancelHandler(Me, New EventArgs())
    End Sub
End Class

In Default.aspx, add 1 label called label1, then drag the button.ascx to the default.aspx.
Add code in default.aspx as below:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = Request.QueryString("label")
        AddHandler button1.btnCancelHandler, AddressOf cancelClick
        AddHandler button1.btnEditHandler, AddressOf editClick
        AddHandler button1.btnSaveHandler, AddressOf saveClick
    End Sub

    Private Sub cancelClick(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("default.aspx?label=Cancel")
    End Sub

    Private Sub editClick(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("default.aspx?label=Edit")
    End Sub

    Private Sub saveClick(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("default.aspx?label=Save")
    End Sub
End Class

Run it, you will see that button that we create in user control can be manipulate and used in default.aspx
See other example for communication between user control and page in:
http://www.codeproject.com/KB/user-controls/Page_UserControl.aspx

  • 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

VB.NET Date Time Format Patterns

Plus500

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

Connecting asp.net to Sql server 2005 stored procedures into dataset

This article demonstrates how to use ASP.NET and ADO.NET with Visual Basic .NET to create and to call a Microsoft SQL Server stored procedure with an input parameter and an output parameter.

1 step create store procedure


Create Procedure GetAuthorsByLastName1 (@au_lname varchar(40), @RowCount int output)
as

select * from authors where au_lname like @au_lname;

/* @@ROWCOUNT returns the number of rows that are affected by the last statement. */
select @RowCount=@@ROWCOUNT

Call the stored procedure in asp.net code
Read the rest of this entry »

  • Share/Bookmark

Read Excel Cell Value from ASP.NET 2008 – VB.NET

In this article I will show how to read value from excel per cell,

First Im using FileUpload component from asp.net

	<td>
                <asp:FileUpload ID="uplFile" runat="server"  Width="367px"/>
	</td>

Read the rest of this entry »

  • Share/Bookmark

SQL Server Management Objects with VB.net

SQL Serverâ„¢ Management Objects (SMO) offer developers a robust toolset for operations such as backing up and restoring databases, and issuing Data Definition Language (DDL) commands. Using SQL SMO you can also connect to SQL Server, iterate through a collection of database objects and perform a variety of tasks against them.

I have been created simple program using vb.net 2008 and sql server 2005 using SQL SMO.
Read the rest of this entry »

  • Share/Bookmark