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
Get
     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

SQL Server Date Formats

One of the most frequently asked questions in SQL Server forums is how to format a datetime value or column into a specific date format.  Here’s a summary of the different date formats that come standard in SQL Server as part of the CONVERT function.  Following the standard date formats are some extended date formats that are often asked by SQL Server developers.

It is worth to note that the output of these date formats are of VARCHAR data types already and not of DATETIME data type.  With this in mind, any date comparisons performed after the datetime value has been formatted are using the VARCHAR value of the date and time and not its original DATETIME value.

Standard Date Formats

SQL Statement

Sample Output

SELECT CONVERT(VARCHAR(20), GETDATE(), 100)

Jan 1 2005 1:29PM 1

SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY]

11/23/98

SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]

11/23/1998

SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD]

72.01.01

SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]

1972.01.01

SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY]

19/02/72

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]

19/02/1972

SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY]

25.12.05

SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]

25.12.2005

SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY]

24-01-98

SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY]

24-01-1998

SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY]

04 Jul 06

SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY]

04 Jul 2006

SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY]

Jan 24, 98

SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY]

Jan 24, 1998

SELECT CONVERT(VARCHAR(8), GETDATE(), 108)

03:24:53

SELECT CONVERT(VARCHAR(26), GETDATE(), 109)

Apr 28 2006 12:32:29:253PM

SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY]

01-01-06

SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]

01-01-2006

SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD]

98/11/23

SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]

1998/11/23

SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD]

980124

SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]

19980124

SELECT CONVERT(VARCHAR(24), GETDATE(), 113)

28 Apr 2006 00:34:55:190

SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)]

11:34:23:013

SELECT CONVERT(VARCHAR(19), GETDATE(), 120)

1972-01-01 13:42:24

SELECT CONVERT(VARCHAR(23), GETDATE(), 121)

1972-02-19 06:35:24.489

SELECT CONVERT(VARCHAR(23), GETDATE(), 126)

1998-11-23T11:25:43:250

SELECT CONVERT(VARCHAR(26), GETDATE(), 130)

28 Apr 2006 12:39:32:429AM

SELECT CONVERT(VARCHAR(25), GETDATE(), 131)

28/04/2006 12:39:32:429AM

Excel 2003 Add-in: XML Tools Add-in

Instructions

To install the add-in, complete the following steps:

    1. Start Excel.
    2. On the Tools menu, click Add-Ins.
    3. Click the Browse button and navigate to the .xla file.
    4. Select it and then click OK.
    5. Click Yes to any file copy or overwrite prompts.

 

Convert a Excel workbook to an xml file

This post is a bit off topic for this blog but I’ll post it anyway as a reminder for myself and hopefully to help others who want to do the same. At a client someone had to convert a very simple, plain, flat text excel 2007 workbook to an xml file format.

I have made an example to let you see how to do this. Below you see the workbook that has to be converted to xml.

exc_workbook1

I thought that just saving the file with “save as” to xml format would do it but I thought wrong. I got an error that there were no XML mappings found in the workbook. For Excel 2007 you need a add-in that is called OfficeExcel2003XMLToolsAddin.exe and which can be downloaded here. On the download page you can also read how to install and open the add-in in Excel 2007.

Read more: http://www.information-worker.nl/2010/04/19/howto-convert-a-excel-workbook-to-an-xml-file

Changing the Apache Port in XAMPP

How to Change the Port

Edit the httpd.conf file to change the port Apache listens on.

(1) Locate the file httpd.conf. This file should be found in C:\xampp\apache\conf (or whichever directory you installed XAMPP in – mine is on my D-drive because that is where I installed XAMPP).

(2) Open the file using a text editor or programmer’s editor (I use notepad++). DO NOT use a word processor (like Word) to open the file, it will badly mess it up.

(3a) Locate the line that says Listen 80. It is line #47 in the XAMPP 1.7.3 install.

(3b) Change the port number (80) to a different number (for this tutorial, I changed it to 1234).

(3c) Save the file.

(4) Restart the Apache Server to have the change take effect. Do this by clicking on the Stop button on the XAMPP control panel and then clicking on the Start button. (You could also just reboot the computer, but that is overkill).

Using the New Port

When you connect to the Apache Server, you will have to append the port number to the URL. Previously where you typed localhost, you will now have to type localhost:1234 (or whatever your new port number is):

Note: in Microsoft Internet Explorer 8 (IE8), it is necessary to prepend the prefix http:// beforelocalhost: localhost:1234 will not work, it must be http://localhost:1234.

I did not observe this with Firefox 4.0 – entering localhost:1234 worked just fine (the browser was able to resolve the address as http://localhost:1234).

Second note: this is not a bug in IE8 – as a convenience, browsers automatically try to fixup URLs for you. Technically, you ARE required to type http:// before every URL you type in.

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 := “http://tempuri.org/&#8221;)>

_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”,_
OleDbConnection1)
‘dataadapter
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.

Example

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()
         Next         

         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