VB.NET
Generating Random Colors in VB.NET
Jun 16th
Here’s how to generate a random color in the hex RGB format “#FFFFFF”.
Firstly, we generate random integer values from 0 to 255 for each color channel, red, green and blue:
Dim intR, intG, intB As Integer
intR = RandomClass.Next(0, 256)
intG = RandomClass.Next(0, 256)
intB = RandomClass.Next(0, 256)
With random numbers you need to give two values. The range returned is greater than or equal to the first value and less than the second value so (0, 256) gives a range from 0 to 255 inclusive.
Next, we convert these integer values to hexadecimal:
hexR = intR.ToString(“X”).PadLeft(2, “0″c)
hexG = intG.ToString(“X”).PadLeft(2, “0″c)
hexB = intB.ToString(“X”).PadLeft(2, “0″c)
The ToString(“X”) puts the integer into hexadecimal format. The PadLeft(2, “0″c) adds a leading zero before single character hex values, numbers 0-15 so that value 8 becomes 08. Expressing a color hex RGB value shorter than 6 digits may cause problems.
Finally, we put the red, green and blue values together with a hash symbol in a string:
strColor = “#” & hexR & hexG & hexB
You can then use this string value to generate random colors within your application.
To set the random color for web form controls you can generally use the randomly generated integer values rather than the hex format, like this:
Selecting a random database record with SQL
Apr 9th
Here’s a “quick and dirty” way of selecting a database record at random. This can be used for things like showing a featured product or a random advert.
Here’s the SQL to use in your SelectCommand:
You can either bind a control such as a Repeater to the data source or use VB to pull data from the record and use it. Here’s an example of producing a random link in a page. ASP.NET form:
And here’s the VB to grab the data for the single record:
dv = CType(srcRandomCourse.Select(DataSourceSelectArguments.Empty), Data.DataView)
Dim dr As Data.DataRowView = dv(0)
Dim strProductName As String = CStr(dr(“product_name”))
Dim strProductId As String = CStr(dr(“product_id”))
lnkRandomProduct.Text = strProductName
lnkRandomProduct.NavigateUrl = “http://www.yoursite.com/products.aspx?id=” & strProductId
Calculating Age from Date of Birth in VB.NET
Mar 8th
It’s quite common to need to calculate a person’s age from the date of birth they have entered. Here’s the quick and easy way of doing it:
strDOB = txtDateOfBirth.Text
Dim intAge As Integer
intAge = Math.Floor(DateDiff(DateInterval.Month, DateValue(strDOB), Now()) / 12)
lblAge.Text = intAge
In this example the date of birth data is captured in a TextBox control called txtDateOfBirth. The DateDiff function calculates the difference between two dates. The arguments are the interval to focus on (here months), the first date (here the date of birth) and the second date (here the current date). The date of birth is expressed as DateValue(strDOB) to convert the string to a date format. This is then divided by 12 to give the age in years as a decimal. Finally, the introductory Math.Floor is used to round the number down to the nearest whole number.
Returning a Record Count using VB.net
Dec 15th
When websites feature a text search and a table or list of results you will sometimes see something like:
73 records matched your search or 128 matches found.
This is actually very easy to do but incredibly hard to find an easy way of doing it. Here’s how it’s done.
This example is based on searching a company contact list.
Firstly, in your web form you need your DataSource and SelectCommand, for example:
Then, in your VB you get the number of records returned as follows:
Dim dv As Data.DataView
dv = CType(srcResults.Select(DataSourceSelectArguments.Empty), Data.DataView)
intMatches = dv.Count.ToString()
You will need to change srcResults in the third line to the ID of your DataSource control, the rest of the code can stay as it is.
Finally, you need to output the number of records to the page. This can be done by putting a Literal or Label control in the web form:
Here’s some code which gives different output depending on whether there are 0, 1 or more than 1 record counted:
litRecordCountText.Text = “<span style=’color:’cc0000′>Sorry, no contacts matched your search text.<br />Try entering just the first few letters of a name.</span>”
ElseIf intMatches = 1 Then
litRecordCountText.Text = “1 match found. The following contact matches your search.”
Else
litRecordCountText.Text = intMatches & ” matches found. The following contacts match your search.”
End If
Setting the page title dynamically in ASP.NET
Mar 12th
With a database driven site it’s very useful if pages giving details of a specific product have that product name in the page title.
This sounds like it should be quite easy but it isn’t. You can insert data from a field into a page quite easily by binding the data to a control. However, the ASP.NET controls have to sit with the <form> tags in the <body> of the HTML so binding data to the <head> section is more difficult.
It has to be done programmatically. Here how’s it’s done in VB.
dv = CType(srcDataSourceControl.Select(DataSourceSelectArguments.Empty), Data.DataView)
Dim dr As Data.DataRowView = dv(0)
Dim strProductName As String = CStr(dr(“ProductName”))
Dim strProductNumber As String = CStr(dr(“ProductNumber”))
Dim strProductCategory As String = CStr(dr(“ProductCategory”))
Page.Header.Title = strProductName & ” – ” & strProductNumber & ” – ” & strProductCategory
This example uses srcDataSourceControl as the data source control and assumes that the database table has fields called ProductName, ProductNumber and ProductCategory. You can bind as many fields as you like in this way.
The title is set by giving a string value to Page.Header.Title. This can be strings bound with DataView (strProductName) or bits of text in quote marks (” – “) joined by the & sign.
One final important thing. The <head> section in your page HTML must include the runat=”server” to enable the header content to be manipulated on the server side.


