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

Advertisements

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:

Quote

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 :

DECLARE @date DATETIME
SET @date = '2012-03-23 10:31 PM'
SELECT DATEPART(dd,@date)
--- the query will return = 23
---------------------------------------

if we want to display the Month, use :

DECLARE @date DATETIME
SET @date = '2012-03-23 10:31 PM'
SELECT DATEPART(mm,@date)
--- the query will return = 03
---------------------------------------

if we want to display the Year, use :

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

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

Display exactly 2 digits after decimal without Rounding on SQL Server

let say, we had a value 7.35642132 on sql query return, then we want to display it only exactly 2 digits after decimal without Rounding, here is the way :

to get only two digits after decimal for a value 7.35642132
Select LTRIM(RTRIM(STR(7.35642132,10,2)))

the results is 7.35

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…”

image

  • You will see the following popup

image

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

 

Source : http://blogs.msdn.com/b/rakkimk/archive/2007/07/10/iis7-how-to-enable-content-expiration.aspx

Backing up System Databases

System databases contain information about user databases as well as meta-data about SQL Server, SQL Server Agent, jobs, alerts, DTS packages and more. Therefore, it is crucial to have valid backups of the following databases:

  1. Master
  2. MSDB
  3. Model
  4. Distribution (if using replication)

Another system database, tempdb, is rebuilt each time SQL Server is started. Therefore tempdb does not need to be backed up. The Model database serves as a blue-print for creating new user databases. This database should not have any activity associated with it, other than the database administrator adding or removing objects required in all user databases. Therefore, you should typically use the SIMPLE recovery model for the Model database and only perform full backups for it.

Read more of this post

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.