March 2007 - Posts
The calendar control is useful for displaying event dates that users can then click on for more information. Here are the steps for setting up the calendar control to display event information from database.
1. Place a calendar control on the required page (for example, default.aspx)
2. Create a second page called calendar.aspx for displaying the event information.
3. In your codebehind file for the page with the calendar control (default.aspx.vb in my example), create the SelectionChanged Sub for your calendar control (calendar1 in my example) and enter
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
' Display calendar details when date selected
Dim eventDateSelected As String
eventDateSelected = Format(Calendar1.SelectedDate.Date, "MM/dd/yyyy").ToString
Response.Redirect("calendar.aspx?eventDate=" & eventDateSelected)
End Sub
The code above retrieves the date clicked on by the user (Calendar1.SelectedDate.Date), formats the date, and redirects the user to calendar.aspx with the date selected as a querystring. You will need to format the date to match the format of the date in your database events table.
4. In calendar.aspx, use a data control such as a detailsview or formview to display the information.
5. When setting up your formview or detailsview data source (assuming that you are using Visual Studio or Visual Web Developer), click the Where button to create the querystring criteria. The column should be your date column, the Source should be QueryString, and the QueryString Field should be eventDate (if you are using the code above).
One thing I should stress is that the date format from the database needs to match the date format of the query string. I use a SQL Server 2005 database to store event data. In the view I created to display event information, I use the following conversion to extract the date from the eventDate column and match it with the query string:
CONVERT (nvarchar(30), eventDate, 101)
101 is one of the predefined date formats.
If you want to show images of products, members, or anything else, one method is to store the image filename in a database field and then add an imagefield column type to an ASP.NET gridview. Here are the steps:
1. Let's assume you have a products table in an Access database (the step are almost identical for MSSQL) with a column called product_image that stores image filenames such as product1.jpg.
2. Let's also assume that you have saved all of your product images to a folder called images in your web site.
3. In Visual Web Developer or Visual Studio, create a GridView based on the products table in the normal way.
4. From GridView Tasks, Select Add New Column
5. For Field Type, select ImageField.
6. Type in a Header if you want.
7. For the data field, type in product_image (or whatever you called your images field in the database).
8. For the URL format string, type in images/{0}
In step 8, images refers to the folder in your web site where you saved all of your images.
When you view your web page, the {0} is replaced by the relevant image filename and then converted into an img HTML tag so that it displays as an image.
I was recently asked to create a random question number generator in Microsoft Excel. The requirements were to create random question numbers from 1 to 65 that did not repeat. Here is the solution I came up with but I would interested to hear of any better methods:
Sub GenerateQuestions()
' Use Column B to display the random question numbers
Range("B1").Select
'Number of questions
questionCount = 65
For counter = 1 To questionCount
' Generate random number
ActiveCell.FormulaR1C1 = "=RANDBETWEEN(1,65)"
questionsAllocated = counter - 1
' Check if question number already allocated
For checkCounter = 1 To questionsAllocated
If ActiveCell.Value = ActiveCell.Offset(-checkCounter, 0).Value Then
ActiveCell.Value = ActiveCell.Value + 1
If ActiveCell.Value = questionCount + 1 Then ActiveCell.Value = 1
checkCounter = 0
End If
Next checkCounter
' Copy and Paste Values so that allocated question numbers don't regenerate
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' Go to next question
ActiveCell.Offset(1, 0).Select
Next counter 'Loop
End Sub