Passing variable from code behind to HTML tag in ASP.NET

Some how in some case, we want to directly passing variable that we already declare in ASP.NET with code behind to the HTML tag, like <input type = “TEXT”> tag in HTML, we can do that just with the simple code, here is the way:

Declare test as a property (at the class level) instead of a local variable, then refer to it as you currently do in your markup (aspx).

VB.NET 10 (automatic properties):

Protected Property test As String = "Test" 


Pre-VB.NET 10 (no support for automatic properties)

Private _test As String
Protected Property Test As String
     Return _test
End Get
Set(value As String)
     _test = value
End Set
End Property


place that variable in HTML tag that you want to place, such as :

<input type=”TEXT” name=”SomeThing” size=”50″ maxlength=”250″ value = “<%= test %>”>


that’s all, hope this helpful

The DATEPART function on SQL Server

The DATEPART function.

This function will take a date/time expression and return a single part of the date, such as hour, minute, month, day, etc. The syntax for using DATEPART is:


DATEPART(datepart, date)

Where the datepart is one of the following:

  • Year: yy, yyyy
  • Quarter: qq, q
  • Month: mm, m
  • dayofyear: dy, y
  • Day: dd, d
  • Week: wk, ww
  • Weekday: dw
  • Hour: hh
  • Minute: mi, n
  • Second: ss, s
  • Millisecond: ms.

let say the date right now is 23-03-2012

if we want to display the day, use :

SET @date = '2012-03-23 10:31 PM'
--- the query will return = 23

if we want to display the Month, use :

SET @date = '2012-03-23 10:31 PM'
--- the query will return = 03

if we want to display the Year, use :

SET @date = '2012-03-23 10:31 PM'
SELECT DATPART(yyyy,@date)
--- the query will return = 2012

Enable content expiration under IIS 7

In IIS6, it was with the website properties –> HTTP Headers. Here in IIS7, you can do this on the “HTTP Response Headers” feature on the website.

  • Select the website in the Connections pane.
  • Double click on the “HTTP Response Headers” feature in the features pane
  • In the action pane, click on the “Set Common Headers…”


  • You will see the following popup


Now, you know what to select. You should enable the HTTP Keep-Alive on this popup only.


Source :

XML Web Services

In this section we will build a more interesting Web service that returns a ADO .NET DataSet, containing the full set of records from a table. We will create our own database table and access the data from the table with this Web service. To Start, open Microsoft Access and create a new Database named Currency. Create a new table Table1 and add three columns named, Country Code, Country Name and Currency. Enter some values in the table and close it. Open Visual Studio .NET and select ASP .NET Web service from the projects type template. Drag a OleDb connection from the Data tab in the toolbox and using the properties window build a connection string that connects to the Currency database which we created. Switch to the code view and start writing the following code.

Imports System
Imports System.Web.Services
Imports System.Data.OleDb
‘import this namespace as we are working with an OleDb source<WebService(Namespace := “;)>

_Public Class Service1 Inherits System.Web.Services.WebService

#Region ” Web Services Designer Generated Code ”

#End Region

<WebMethod()> Public Function GetData() As DataSet
‘WebMethod name is GetData,generate data set
Dim da as OleDbDataAdapter=new OleDbDataAdapter(“Select * From Table1”,_
Dim ds As DataSet=new DataSet()
‘declaring a new DataSet
da.Fill(ds, “Table1”)
‘filling dataadapter
Return ds
‘returning dataset
End Function
End Class

Consuming the Service

Once you finish with coding the Web service we need to consume this service. To do that, open a new Windows Application and from the toolbox drag a DataGrid and a Button. Our intention here is to load the data from Table1 in the Currency database into the DataGrid when we click the Button. Now, add a Web reference to the Web service by selecting Reference->Add WebReference in the Solution Explorer Window. Enter the URL of the service in the address bar and click “Add Reference“. That adds a reference to the Web Service. Now double-click on the Button and write the following code.

Public Class Form1Inherits System.Windows.Forms.Form

#Region ” Windows Form Designer generated code ”

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs)Handles Button1.Click
Dim myService As New localhost.Service1()
‘an instance of the Web service
Dim ds1 As DataSet = myService.GetData
DataGrid1.DataSource = ds1.Tables(“Table1”)
‘filling the datagrid with table
End Sub
End Class

Once you finish with the code, run the Windows Application and click on the Button. The data you entered in Table1 of the Currency database will be displayed in the datagrid. The difference, we are accessing the data with a Web service. The image below displays that.

Creating a Web service with VB.NET

Web services technology is based on HTTP, Simple Object Access Protocol (SOAP), and XML. Since Web services use open standards, calling Web services is fairly simple.

VB.NET allows you to use Web services as if they were entirely local objects since most of the marshaling between the client and the server is taking place in the background. This tip shows you how to create a simple Web service.


I will create a Web service and add a Web function that will return the current machine’s IP address. Here are the steps for creating a Web service:

  1. Open Visual Studio.Net and select Create New Website under VB.NET.
  2. Select Web Service from the options listed.
  3. Once you get the code window open, add the following code:
<WebMethod()> _
        Public Function GetMachineIPAddress() As String         

         Dim strHostName As String = ""
         Dim strIPAddress As String = ""
         Dim host As System.Net.IPHostEntry         

         strHostName = System.Net.Dns.GetHostName()
         strIPAddress = System.Net.Dns.GetHostEntry(strHostName).HostName.ToString()         

         host = System.Net.Dns.GetHostEntry(strHostName)
         Dim ip As System.Net.IPAddress
         For Each ip In host.AddressList
             Return ip.ToString()

         Return ""         

     End Function

Once you debug the example, you will see a screen that looks like Figure A.

Figure A

Figure A

It will list two available Web methods: the one that was generated (HelloWorld) and the one I created (GetMachineIPAddress). Click the link for GetMachineIPAddress, and you will see a screen allowing you to get more information about this method and invoking it (Figure B).

Figure B

Figure B

Click the Invoke button, and the result will look like Figure C.
Figure C

Figure C

Using Windows Services to monitor folders and unzip files – VB.NET

Sample Image - screenshot3.jpg


A friend, developing an application in VB 6.0 needed a solution with the following requirements:

  • Have to run all the time.
  • There is no need to any user to be logged on.
  • Must run in Win 2K, XP and may be in 2003 server.

The answer was: Windows Services.

What does the application do?

The application that generated this solution uploads mdb access files zipped into a file to decrease the size of the data base file, ’cause some of his clients use 56k modem connections.

When developing his application in ASP.NET, he needed some way to monitor directory for this special zipped files. As the files arrive, the solution must unzip the files and overwrite the file content in the same directories.

The easiest way to do this, we thought, was with VB 6. We started to look for some place to find information of how doing this in VB 6, but no success. Every article, tutorial and code I found used VB.NET, C# or any other .NET language.

We were convinced that VB.NET had to be the choice for developing the Windows service. Were fascinated with how easy is to build a Windows service with VB.NET. With VB 6, we tried to build a normal executable and run it against a series of tools (as many articles describe) but none of the tools did the job.

Read more of this post

Killing Processes from a Visual Basic Application

This article provides a simple example of how to use the System.Diagnostics.Process library to display a list of running processes, and to select and kill processes by their process name and ID. It would not be a major trick to display additional information about the process using the same library or to kill processes by their ID or name alone rather than their name and ID but in this example, process names and IDs are used for display purposes and the purpose of killing a running process.


Figure 1: Listing and Killing Processes by Process Name and ID

Getting Started

There is a single solution included with this download, the solution contains a Win Forms project called “ProcessKillerVB”; this project contains one form (Form1.vb) and all of the code required to display and kill processes is contained within that single form class. If you open the attached project into Visual Studio 2008; you should see the following in the solution explorer:


Figure 2: Solution Explorer

Code: The Main Form

The main form contains all of the user interface elements necessary to display and update the list of currently running processes found on the user’s machine. It also contains a button handler which will kill a process by the process name. You can also kill a process by its ID but in terms of readability, if the user is selecting the process, the process name is going to make a lot more sense to them than will the process ID.

Read more of this post