Tuesday, April 05, 2005

Formatting ASP.NET DataGrid's Data

Question:

The data in database is as follows:
Id Name Color HireDate
1 Tom Pink 9/15/2001 3:30:00 AM
2 Jerry Blue 9/15/1991 3:30:00 AM

Question:

* How change DataGrid row color according to color in database .
* Format of the Date should be mm/dd/yyyy

Solution:

Step 1:

In webform1.aspx drag drop a Datagrid.




Simple binding of theData to datagrid as given below

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim ds As New DataSet()
Dim sqlStmt As String = "SELECT * FROM ColorTable"
Dim conString As String = "server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds, "Table")
Dim dv As New DataView(ds.Tables(0))
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub

Step 2:

To display the data in the Datagrid based on Database values

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
'Create a DataView Based on the DataSource of DataGrid
Dim dv As DataView = DataGrid1.DataSource
Dim dc As DataColumnCollection = dv.Table.Columns

'Check for ItemType
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

'Declare string variable
'Assign the relevant data to a variable
Dim fieldcolor As String
fieldcolor = DataBinder.Eval(e.Item.DataItem, "color")

'To convert the value of Type String to System.Drawing.Color
e.Item.BackColor = System.Drawing.Color.FromName(fieldcolor)


'Declare DateTime variable
'Assign the relevant data to a variable
'To display DateTime field in the format "MM/dd/yyyy"
Dim fieldDate As DateTime
fieldDate = Convert.ToDateTime _
(DataBinder.Eval(e.Item.DataItem, "HireDate", "{0:MM/dd/yyyy}"))
'To display the value in the proper cell of DataGrid
e.Item.Cells(dc.IndexOf(dc("HireDate"))).Text = fieldDate
End If

End Sub



The other ways to format Date in MM/dd/yyyy format are:

a)

e.Item.Cells(dc.IndexOf(dc("HireDate"))).Text = fieldDate.ToString("d")

b)

e.Item.Cells(dc.IndexOf(dc("HireDate"))).Text = fieldDate.ToShortDateString()

Pivot Tables with ADO.NET and Display in Horizontal Paged DataGrid

This article describes a simple method to pivot a DataTable producing rows for each column and a column for each row. The pivoted DataTable is then displayed in a DataGrid using horizontal paging to view a limited number of Columns in each page. This is a common display technique used to compare, for instance, product features, where the product names appear across the top of the DataGrid with the Features listed down the side.

http://aspalliance.com/538

ASP.NET Sample application with source code

Duwamish 7.0 is a multitier, distributed business-to-consumer commerce application built specifically for the Microsoft® .NET platform.

You can download the installation file with source code at

VB App : http://download.microsoft.com/download/visualstudio.netenta/smpDCS/7.0/NT5XP/EN-US/Duwamish7-CS.cab

C# App: http://download.microsoft.com/download/visualstudio.netenta/smpDVB/7.0/NT5XP/EN-US/Duwamish7-VB.cab

With Best Regards,
Mitesh V. Mehta

Resolving quick time-out issue with Forms Authentication

When using forms authentication with slidingExpiration set to true (default), the cookie is updated only when more than half the timeout value has elapsed. As a result of this, you might be logged off sooner than you think.

Consider this: You have set the timeout to 30 minutes. You logon on at 3:00 pm; a FormsAuthenticationTicket is set to expire at 3:30 pm. The expiration of this ticket will not be extended for another 30 minutes until you make a request after 3:15 pm. So, if you made your last request at 3:15 pm, the ticket will still expire at 3:30 pm as more than half the timeout value has not elapsed (giving you a 15 minute window before you get logged out).

On the other had, if you had made a request at 3:16 pm, the expiration of the ticket is extended to 3:46 p.m.

From MSDN:

timeout : Specifies the amount of time, in integer minutes, after which the cookie expires. The default value is 30. If the SlidingExpiration attribute is true, the timeout attribute is a sliding value, expiring at the specified number of minutes after the time the last request was received. To prevent compromised performance, and to avoid multiple browser warnings for users that have cookie warnings turned on, the cookie is updated when more than half the specified time has elapsed. This might result in a loss of precision. Persistent cookies do not time out.

For reference visit : http://msdn.microsoft.com/library/defaultasp?url=/library/en-us/cpgenref/html/gngrfforms.asp


With Best Regards,
Mitesh Mehta

Trick with DataGrid

You can trick the DataGrid's ButtonColumn to use an image for a button without having to use a TemplateColumn. It's really easy. In the DataGrid Property Builder, change the column's Button Type to “LinkButton”, then use regular HTML for the “Text” property, like




Click “Apply“, you now have an image button without the hassle of having a TemplateColumn. Switch over to the ASPX code view, and you should see the following:

ASP.NET SQL Server Session state - Links and Overview

Basic steps to set up SQL Server Session state
1. Run either of these scripts (InstallSqlState uses temp tables, InstallPersistSqlState uses permanent tables so it survives reboots)
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallSqlState.sql
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallPersistSqlState.sql

2. Change your session state mode in web.config to SQLServer:
mode="SQLServer"
sqlConnectionString="data source=server;user id=uid;password=pwd"
cookieless="false" timeout="20" />

References
Good overview http://idunno.org/dotNet/sessionState.aspx

MSDN overview of SQL Server Session State
http://support.microsoft.com/default.aspx?kbid=311209

Common issues
325056 PRB: Session State Is Lost in Web Farm If You Use SqlServer or
http://support.microsoft.com/?id=325056
323744 FIX: "The View State Is Invalid for This Page and Might Be Corrupted"
http://support.microsoft.com/?id=323744


With Best Regards,
Mitesh V. Mehta

Overriding Pop up Blocker Settings in IE

The new Popup Blocker feature of Internet Explorer 6.0 provided in Windows XP SP2 is really good one. Even though it helps in blocking useless windows while browing on internet, sometimes it block useful windows initiated by trusted webpages also. I used to get really frustrated when emptying Hotmail or Yahoo Junk Mail folders as it used to block the confirmation window.
You can set Temporarily Allow Pop ups but its really irritating at times. Also you can set the Always Allows Pop up From This Site, but what if u dont want to switch that on and decide at run time?
Yes there is a way to overcome this by holding Cntrl Key, If you want the popup window to appear then press Control Key--> Hold it and then --> Click the URL you wish to... thats it:), it overrides the popup blocker settings and allows you to see the intended window initiated by the URL.

Regards,
Mitesh V. Mehta