Tuesday, May 7, 2013

Clear All Text in All Textbox

Here's a sample code for clearing all text in all of the textbox that you have in your windows form or web form.

You can used it for example in a registration form after saving all the data it will clear all of the text in the textbox for another entry.

ASP.NET web form application
VB.NET windows form application


Here's my sample Add/Edit/Delete in ASP.NET Web Application,
I used VB.NET for my code behind and MS SQL Server for my back-end Database

to run this program,
open your Visual Studio (any version like 2005/2010/2012)
File>Open>Web Site and locate folder where my sample is... then press f5

feel free to used this for your references...

thanks for using this!! ^_^

Here the link of the file

Saturday, May 4, 2013

Statements in VB.NET


If...Then...Else - Do { While | Until } - For Each...Next - For...Next


"Conditionally executes a group of statements, depending on the value of an expression."

Now let us say that you have more than one variable (Which 99.99 % of the time, you are going to), and you want to see what that variable is equal to or maybe compare it to another variable. This would be accomplished by using an If statement. First let's declare our variables...

Dim str As String = "123"
Dim anotherStr As String = "1234"

Next let's have a brief overview of the If statement.

This is how it will look (without all the brackets):

If condition [ Then ]
   [ statements  ]
[ ElseIf elseifcondition [ Then ]
   [ elseifstatements ] ]
[ Else
   [ elsestatements ] ]
End If

As you can tell the If statement contains 2 other things within it. These are Else and ElseIf, then of course you have the End If which ends the If statement.

This is how you would compare the 2 previous variables that we declared:

If str = anotherStr Then
MsgBox("Holy shit! They're the same!")
End If

In this case since str = 123 and anoterStr = 1234 it would not pop up the MsgBox, it would close the application because the strings are not the same.

Now, in this case, we really only need Else, but say that you wanted to see if str = "abc" and if it did it would execute something instead of Application.Exit()...that is when ElseIf comes into play. This is how we could to that:

If str = anotherStr Then
MsgBox("Holy shit! They're the same!")
ElseIf str = "abc" Then
MsgBox("OMFG str = abc.")
End If

here is the thing when using If statements though. If the initial check is true it will not check the ElseIf or execute the Else.

Do { While | Until }...

"Repeats a block of statements while a Boolean condition is True or until the condition becomes True."

This is how one of these statements would look:

Do { While | Until } condition
[ statements ]
[ Exit Do ]
[ statements ]


[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition

The break down

  • While
    • This is required unless you use Until. While will repeat the Loop until the condition that you supplied becomes False.
  • Until
    • This is required unless you use While. Until will repeat the Loop until the condition that you supplied becomes True.
  • condition
    • This is an optional Boolean variable that will be used to evaluate the expression for True or False.
  • statements
    • This is also optional and would be one or more statements that would repeat while or until the condition becomes True or False.

Optional Remarks for the Do { Until | While } statement
Courtesy or MSDN.
The Exit Do statement transfers control immediately to the statement following the Loop statement. Any number of Exit Do statements can be placed anywhere in the Do loop. Exit Do is often used after evaluating some condition, for example with If...Then...Else.

For Each...Next

"Repeats a group of statements for each element in a collection."

This is how one of these statements would look:

For Each element [ As datatype ] In group
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]

The break down

  • element
    • Any type of variable that is used to iterate, or, loop through the elements of the collection that you supplied. Please note: the datatype of element must be an element that the data type of the elements in the group that you supplied can be converted too. Basically you cannot use a String for something that returns an Integer.
  • datatype
    • Required if element is not declared. If element is declared you cannot re-declare it using the As clause.
  • group
    • This is required and should be an Object variable that must refer to an object collection or an array.
  • statements
    • You can optionally add in various statements that will be executed while the loop is in progress. This will be executed for each element in the group.

Optional remarks for For Each...Next
Courtesy of MSDN
If element has not been declared outside this loop, you can declare it within the For Each statement. In this case, the scope of element is the body of the loop. However, you cannot declare element both outside and inside the loop.

The For Each...Next loop is entered if there is at least one element in group. Once the loop has been entered, the statements are executed for the first element in group; if there are more element(s) in group, the statements in the loop continue to execute for each element (That's why this os called a For Each loop.). When there are no more element(s), the loop is terminated and execution continues with the statement following the Next statement.

Any number of Exit For statements may be placed anywhere in the loop as an alternative way to exit. Exit For is often used after evaluating some condition, for example with an If...Then...Else statement, and transfers control to the statement immediately following Next.

You can nest For Each...Next loops by placing one loop within another. Each loop must have a unique element variable.

Error Handling using Try... Catch... in VB.NET

Error Handling in VB.NET

You're eventually going to get an error by using Try...Catch.... VB.NET has a statement called the Try...Catch...Finally statement that is made specifically for error handling. I've devoted this post specifically for Error Handling instead of adding this to the Statements section because this is one of the most important statements that you should learn to use.

Let's go over it


"Provides a way to handle some or all possible errors that may occur in a given block of code, while still running code."
This is how one of these statements would look:

[ tryStatements ]
[ Catch [ exception [ As type ] ] [ When expression ] 
[ catchStatements ] ]
[ Exit Try ]
[ Finally
[ finallyStatements ] ]
End Try

The break down
(Courtesy of MSDN)

  • tryStatements
    • This is optional but it's pointless if you do not include these. This is the code where you think an error will occur. There can be multiple statements within for checking.
  • Catch
    • Optional. Multiple Catch blocks permitted. If an exception occurs while processing the Try block, each Catch statement is examined in textual order to determine if it handles the exception. Exception represents the exception that has been thrown.
  • exception
    • Optional. Any variable name. The initial value of exception is the value of the thrown error. Used with Catch to specify the error caught.
  • type
    • Optional. Specifies the type of class filter. If the value of exception is of the type specified by type or of a derived type, the identifier becomes bound to the exception object.
  • When
    • Optional. A Catch statement with a When clause will only catch exceptions when expression evaluates to True. A When clause is only applied after checking the type of the exception, and expression may refer to the identifier representing the exception.
  • expression
    • Optional. Must be implicitly convertible to Boolean. Any expression that describes a generic filter. Typically used to filter by error number. Used with When keyword to specify circumstances under which the error is caught.
  • catchStatements
    • Optional. Statement(s) to handle errors occurring in the associated Try block. Can be a compound statement.
  • ExitTry
    • Optional. Keyword that breaks out of the Try...Catch...Finally structure. Execution resumes with the Finally block if present, otherwise with the code immediately following the End Try statement. Not allowed in Finally blocks.
  • Finally
    • Optional. A Finally block is always executed when execution leaves any part of the Try statement.
  • finallyStatements
    • Optional. Statement(s) that are executed after all other error processing has occurred.
  • End Try
    • Terminates the Try...Catch...Finally structure.