Dinowebs

ASP.NET Tips & Web Development

RSS FeedsFollow me on Twitter!Follow me on Facebook!

  • Home
  • ASP.NET
    • AccessDataSource
    • DataList
    • GridView
      • ButtonField
      • Row
        • Cells
      • TemplateField
        • OnClientClick
    • MasterPage
  • css
    • Mozilla CSS
  • html
    • fieldset
      • legend
  • SQL
    • DeleteCommand
    • NEWID()
    • SelectCommand
    • TOP
  • VB.NET
    • DataRowView
    • DataView
    • DateDiff()
    • DateInterval
    • DateValue()
    • Math.Floor()
    • Now()
    • Page.Header.Title
    • Random()

Generating Random Colors in VB.NET

Jun 16th

Posted by admin in Random()

No comments

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 RandomClass As New Random()
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:

Dim hexR, hexG, hexB, As String
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:

Dim strColor As 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:

Label1.BackColor = Drawing.Color.FromArgb(intR, intG, intB)
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...
add leading zeros, convert int to hex, convert integer to hexadecimal, hex colors, int to hex, leading zero, random color, random hex value, random integer, Random()

Selecting a random database record with SQL

Apr 9th

Posted by admin in ASP.NET

No comments

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:

SELECT TOP 1 * FROM table ORDER BY NEWID()

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:

<asp:HyperLink ID=”lnkRandomProduct” runat=”server” />

And here’s the VB to grab the data for the single record:

Dim dv As Data.DataView
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

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
featured product, random content, random data, random hyperlink, random link, random product link, random record, random row, random sql record, retrieve random record

Calculating Age from Date of Birth in VB.NET

Mar 8th

Posted by admin in DateDiff()

No comments

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:

Dim strDOB As String
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.

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
age from date of birth, calculate age, converting string to date, date of birth, date of birth to age, difference between dates, working with dates

Conditional Formatting in a GridView

Dec 17th

Posted by admin in ASP.NET

4 comments

This code will allow you to change the background colour of a cell or entire row depending on certain conditions.

In this example we test if the row being bound is a data row, then if the second column (indexed as 1) is equal to 0. If it is then the row’s backcolor is set as PeachPuff and the cell itself has backcolor Salmon. Remember that cell indexing starts at 0 so that the first column is e.Row.Cells(0), the second column e.Row.Cells(1), etc.

Protected Sub grdTable_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdTable.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

If e.Row.Cells(1).Text = “0″ Then

e.Row.BackColor = System.Drawing.Color.PeachPuff
e.Row.Cells(1).BackColor = Drawing.Color.Salmon

End If

End If

End Sub

To change the text colour simply substitute BackColor with ForeColor.

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
cell backcolor, cell forecolor, conditional formatting, formatting cells, row backcolor, row forecolor, rowdatabound

Returning a Record Count using VB.net

Dec 15th

Posted by admin in ASP.NET

No comments

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:

<asp:SqlDataSource ID=”srcResults” runat=”server” ConnectionString=”<%$ ConnectionStrings:constrConnectionString %>” SelectCommand=”SELECT * FROM [contacts] WHERE [firstname] LIKE ‘%’ + txtSearch + ‘%’ OR [lastname] LIKE ‘%’ + txtSearch + ‘%’”>

Then, in your VB you get the number of records returned as follows:

Dim intMatches As Integer
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:

<asp:Literal ID=”litRecordCountText” runat=”server” />

Here’s some code which gives different output depending on whether there are 0, 1 or more than 1 record counted:

If intMatches = 0 Then
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
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
record count, recordcount, result count, row count, search matches, total records
12»
  • Web Development

    web development services | dinowebsCommercial web development services at Dinowebs.com
  • Email Updates

    Enter your email address:

    Delivered by FeedBurner

  • Main Menu

    • About
    • Quick Colour Schemes
  • Post Categories

    • ASP.NET (7)
      • AccessDataSource (1)
      • DataList (1)
      • GridView (2)
        • ButtonField (1)
        • Row (1)
          • Cells (1)
        • TemplateField (1)
          • OnClientClick (1)
      • MasterPage (1)
    • css (1)
      • Mozilla CSS (1)
    • html (1)
      • fieldset (1)
        • legend (1)
    • SQL (4)
      • DeleteCommand (1)
      • NEWID() (1)
      • SelectCommand (2)
      • TOP (1)
    • Uncategorized (1)
    • VB.NET (5)
      • DataRowView (2)
      • DataView (3)
      • DateDiff() (1)
      • DateInterval (1)
      • DateValue() (1)
      • Math.Floor() (1)
      • Now() (1)
      • Page.Header.Title (1)
      • Random() (1)
  • Graphics Tools

    • Background Patterns
    • Favicon Generator
    • GetFavicon.org
    • Gradient Image Maker
    • JS Charts
    • Reflection Maker
  • JavaScript

    • jQuery
    • jQuery for Designers
    • jQuery Masonry
    • jQuery Tools
    • jQuery UI
    • Scriptaculous
  • Online Tutorials

    • BrainJar
    • W3 Schools
  • RSS Web Design Articles

    • How to pick the right domain name June 22, 2010
    • Static Website or Content Management System? June 2, 2010
    • Good web design – following conventions September 16, 2009
    • Fighting Spam September 14, 2009
    • Twitter explained simply July 3, 2009
    • Web traffic through quality links June 21, 2009
    • Introducing… Dinowebs.net June 21, 2009
    • My website is 5 today! June 15, 2009
    • Monitoring your Marketing with Unique URLs June 9, 2009
    • Don’t put all your eggs in Google’s basket June 5, 2009
  • Recent Posts

    • Generating Random Colors in VB.NET
    • Selecting a random database record with SQL
    • Calculating Age from Date of Birth in VB.NET
    • Conditional Formatting in a GridView
    • Returning a Record Count using VB.net
    • Using fieldsets for style
    • Confirming delete actions in ASP.NET
    • Setting the page title dynamically in ASP.NET
    • Showing only present and future dates using SelectCommand in ASP.NET
    • Category links with record count in ASP.NET
  • User Login






    • Register
    • Lost your password?
    • Tags

      are you sure bind data to page title cancel category list cell backcolor conditional formatting confirmation confirm delete css databound page title date filtering date value datevalue delete DeleteCommand delete confirm dynamic page title fieldset fieldset tag Firefox future dates gridview html IE7 legend legend tag list record count message box moz-border-radius Mozilla CSS ok page header page title product name in title record count recordcount result count rounded corners row count search matches select count set page title set title tag total records
Mystique theme by digitalnature | Powered by WordPress
RSS Feeds XHTML 1.1 Top