Welcome to The Coding College, where we deliver actionable tutorials to accelerate your web development journey. In this guide, we provide side-by-side examples of common tasks in C# and VB.NET for ASP.NET Web Pages, making it easier to choose the language that best suits your needs.
Why Learn Both C# and VB.NET?
ASP.NET Web Pages supports both C# and VB.NET, offering flexibility based on your preferences or project requirements. While C# is more widely adopted, VB.NET remains popular in specific industries. Understanding both languages can broaden your opportunities and enhance collaboration with diverse teams.
1. Writing “Hello, World!”
C#:
@{
var message = "Hello, World!";
Response.Write(message);
}
VB.NET:
@Code
Dim message As String = "Hello, World!"
Response.Write(message)
End Code
2. Displaying the Current Date and Time
C#:
@{
var currentTime = DateTime.Now;
}
<p>The current date and time is: @currentTime</p>
VB.NET:
@Code
Dim currentTime As DateTime = DateTime.Now
End Code
<p>The current date and time is: @currentTime</p>
3. Conditional Statements (If-Else)
C#:
@{
var age = 25;
if (age >= 18)
{
<p>You are an adult.</p>
}
else
{
<p>You are a minor.</p>
}
}
VB.NET:
@Code
Dim age As Integer = 25
If age >= 18 Then
Response.Write("<p>You are an adult.</p>")
Else
Response.Write("<p>You are a minor.</p>")
End If
End Code
4. Looping Through an Array
C#:
@{
var fruits = new[] { "Apple", "Banana", "Cherry" };
foreach (var fruit in fruits)
{
<p>@fruit</p>
}
}
VB.NET:
@Code
Dim fruits() As String = {"Apple", "Banana", "Cherry"}
For Each fruit As String In fruits
Response.Write("<p>" & fruit & "</p>")
Next
End Code
5. Creating a Simple Form
C#:
@{
if (IsPost)
{
var name = Request["name"];
<p>Hello, @name!</p>
}
}
<form method="post">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</form>
VB.NET:
@Code
If IsPost Then
Dim name As String = Request("name")
Response.Write("<p>Hello, " & name & "!</p>")
End If
End Code
<form method="post">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" />
<button type="submit">Submit</button>
</form>
6. Reading Data from a Database
Assume a database table named Products
with columns Id
and Name
.
C#:
@{
var db = Database.Open("MyDatabase");
var products = db.Query("SELECT * FROM Products");
}
<ul>
@foreach (var product in products)
{
<li>@product.Name</li>
}
</ul>
VB.NET:
@Code
Dim db = Database.Open("MyDatabase")
Dim products = db.Query("SELECT * FROM Products")
End Code
<ul>
@For Each product In products
Response.Write("<li>" & product.Name & "</li>")
Next
</ul>
7. Handling Errors
C#:
@{
try
{
var x = 10;
var y = 0;
var result = x / y; // Division by zero
}
catch (Exception ex)
{
<p>Error: @ex.Message</p>
}
}
VB.NET:
@Code
Try
Dim x As Integer = 10
Dim y As Integer = 0
Dim result As Integer = x / y ' Division by zero
Catch ex As Exception
Response.Write("<p>Error: " & ex.Message & "</p>")
End Try
End Code
8. Using Helper Methods
C#:
@{
var gravatarUrl = Gravatar.GetUrl("[email protected]");
}
<img src="@gravatarUrl" alt="Gravatar" />
VB.NET:
@Code
Dim gravatarUrl As String = Gravatar.GetUrl("[email protected]")
End Code
<img src="@gravatarUrl" alt="Gravatar" />
Choosing Between C# and VB.NET
Feature | C# | VB.NET |
---|---|---|
Syntax | Curly braces {} | Indentation and End |
Popularity | High | Moderate |
Readability | Concise | Verbose |
Learning Curve | Steeper | Easier for beginners |
Best Practices for ASP.NET Development
- Consistent Language Use
Choose either C# or VB.NET for the entire project to maintain consistency. - Write Clean Code
Use meaningful variable names and comments to improve readability. - Test Thoroughly
Debug and test all code examples to ensure accuracy and reliability.
Why Learn ASP.NET with The Coding College?
At The Coding College, we focus on practical, beginner-friendly tutorials that empower developers. Whether you prefer C# or VB.NET, our examples are designed to help you build efficient web applications.
Explore more tutorials at The Coding College and enhance your ASP.NET expertise today!