Calculating Age

Try
Dim dbdate As String = "25 April 1976"
Dim newdate As DateTime = Convert.ToDateTime(dbdate)
Response.Write(newdate.ToString("d") & "<br>")
Response.Write("Age : " & DateDiff("yyyy", newdate, Now()))
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try

Validating MaxLength of the TextArea using CustomValidator

In Webform1.aspx
<SCRIPT language="c#" runat=server >
void validateLength(object source, ServerValidateEventArgs args){
int num  = int.Parse(args.Value.Length.ToString());
if (num >= 4000)
{
    args.IsValid=false;
}
else
{
    args.IsValid=true;
}
}
</SCRIPT>
<asp:TextBox
	id="TextBox1"
	style="Z-INDEX: 102; LEFT: 192px;
	POSITION: absolute; TOP: 311px"
	runat="server"
	TextMode="MultiLine">
</asp:TextBox>
<asp:CustomValidator
	id="CustomValidator1"
	runat="server"
	ControlToValidate="TextBox1"
	OnServerValidate="validateLength">
	</asp:CustomValidator>

And in webform1.aspx.cs
private void Button1 Click(object sender, System.EventArgs e)
{

if (Page.IsValid)

{
       Response.Write ( "Length Proper =4000");
}
}

Force TextBox Values to Capital Letters whenever user types

Add Controls
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 62px; POSITION: absolute; TOP: 90px" runat="server"></asp:TextBox>

<asp:linkbutton id="clk_Btn"
	onclick="TxtChange"
	Visible="False"
	runat="server"
	Text="Invisible LinkButton acts as delegate to TxtChangeMethod">
	</asp:linkbutton>
In the Code behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
	If Not Page.IsPostBack Then
		TextBox1.Attributes.Add("autocomplete", "off")
		TextBox1.Attributes.Add("onkeyup", "javascript:__doPostBack('clk_Btn','')")
	End If
End Sub
Sub TxtChange(ByVal sender As System.Object, ByVal e As System.EventArgs)
	TextBox1.Text = UCase(TextBox1.Text)
End Sub

Using Hex Codes for Colors in System.Drawing

All the methods of System.Drawing.Color, at first, seem to bypass the use of Hex codes, like normally used in HTML. However, there is a method using one argument that you can use:

Color.FromARGB

If you use only one Int32 as an argument, you can preface your Hex code with ‘&H78′ and it will work perfectly – - like this:

myVar=Color.FromARGB(&H78CEEFFF)

Data from multiple columns in a dropdownlist

Code shows how to display multiple fields in a dropdownlist

Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
Dim strConn As String
strConn = "Server=localhost;uid=sa;password=;database=northwind;"
If Not Page.IsPostBack Then
    myconnection = New SqlConnection(strConn)
    myda = New SqlDataAdapter("Select * from Categories", myconnection)
    ds = New DataSet()
    myda.Fill(ds, "Table")
    Dim dr As DataRow
    For Each dr In ds.Tables(0).Rows
        DropDownList1.Items.Add(dr("CategoryID") & dr("CategoryName"))
    Next
End If

Check a DataSet for Empty

Let’s say you fill a dataset:

Dim ds as DataSet=New DataSet()
Dim Cmd as New OleDBDataAdapter(sql,MyConn)
Cmd.Fill(ds,”Employees”)
dg.Datasource=ds.Tables(“Employees”).DefaultView

Before you Bind it to a server control, you’d like to see if it actually has any data in it……so – you can do something like this:

If ds.Tables(0).Rows.Count >0 then
   ‘do your databinding here
Else
   ‘ enter your message here, like, ‘Sorry but no data exists with this criteria’
End if

Displaying Saved Line Feeds/Carriage Returns

When displaying the information contained in and/or saved in a database from a Multiline textbox, remember, it saves line feeds/carriage returns as just that – line feeds/carriage returns.

A web browser does not recognize those characters – - therefore, to display them as such, you must replace the characters with tags a web browser can understand, like <br> or <p>.

In VB.Net, ‘vbcrlf’ is the equivalent of a LineFeed/CarriageReturn, so to display those correctly in a browser, before displaying them (lets call the string, ‘sItem’), you need to do something like this:

sItem=sItem.Replace(vbcrlf, “<br>”)

.Then, when sItem (a variable containing the saved data) is displayed, it will display correctly.

Disabling a Button Until Processing is Complete

Here’s the scenario – let’s say you have an Insert subroutine, called ‘doInsert’. You want to immediately disable the Submit button, so that the end-user won’t click it multiple times, therefore, submitting the same data multiple times.

For this, use a regular HTML button, including a Runat=”server” and an ‘OnServerClick’ event designation – like this:
<INPUT id=”Button1″ onclick=”document.form1.Button1.disabled=true;” type=”button” value=”Submit – Insert Data” name=”Button1″ runat=”server” onserverclick=”doInsert”>

Then, in the very last line of the ‘doInsert’ subroutine, add this line:
Button1.enabled=”True”

That’s all there is to it!