Word Count in .NET

Ever needed a handy ability to count words in a string using .NET? You are in the right place. There are many ways to count words but I’ll be giving you the most efficient method to do it.

Continue reading

Reading and writing files in VB.NET

Some years ago, I developed a word processor application in VB.NET that I myself used for some time as a replacement to the popular notepad. The most important part of the application was developing an ability to read and write files from the hard disk. After a lot of researching, I found out two classes buried in the System.IO namespace that do just that thing. And they are called StreamReader and StreamWriter. So in this post I’m going to give a short tutorial on how to read and write files to the hard disk.

Continue reading

Returning ContentResult in ASP.NET MVC

Previously, I wrote about returning JsonResult in ASP.NET MVC. Today, I write about returning ContentResult. So, what does ContentResult actually returns? It can return any content. It can be plain text, html, xml, etc. It is specified by the MIME type in the return statement.

Continue reading


“I think Microsoft named .Net so it wouldn’t show up in a Unix directory listing.”
– Oktal

Returning JsonResult in ASP.NET MVC

Along with the ActionResult type (which is probably the most used), there is another type of return i.e. the JsonResult.

Continue reading

Selecting in-between rows in SQL

Suppose you are writing a SQL query and you want to retrieve in-between records. That is, you have retrieved 100 records but you want from 40 to 50 (suppose), then writing a query becomes somewhat difficult. I had this problem while I was developing a web application in which I had to paginate the results. Here’s how to do it in MS-SQL.

Below is the query:

SELECT au_fname, au_lname, dob
FROM (SELECT ROW_NUMBER() OVER(ORDER BY au_lname ASC) AS NUM, * FROM authors) AS BetweenRows
WHERE NUM >= 40 AND NUM <= 50;

You can customize the query the way you want since you won’t be having the same column names as I have. Change the numeric values ’40’ and ’50’ to the records’ numbers. The above query will select all records between the 40th and the 50th records.

SQLException when reading NULL values

It happened with me. I had one or two NULL values in one of the records in my database. When I tried to read it in VB.NET (or C#), a SQLException popped up saying couldn’t execute SQLDataReader on NULL values. So here’s the remedy.

Continue reading

MD5 encryption in VB.NET

The text “12345” converted to “827ccb0eea8a706c4c34a16891f84e7b”. That’s nothing but simple encryption using MD5 cryptography. You can use MD5 encryption in your projects for encrypting sensitive data like passwords, credit card numbers, etc. But beware, once encrypted these cannot be decrypted again. For example, once you store the hashed password in a database, you can afterwards check during user login whether the hash of the entered password and the stored hashes are the same.

I have created a simple utility class which can be used to encrypt or compare hashes. It is a shared class and thus can be used directly without creating any instance. To download it, click here. To use it simply write Hasher.HashIt(inputStr). Replace the inputStr with the string you want to hash and it’s done.

You can find the complete history and algorithm of MD5 here.

Epoch Time in .NET

Epoch time is nothing but the number of milliseconds since January 1, 1970. It can be used in many places as a unique identity. You can use epoch time to provide IDs in a database on the basis of current date and time. It is useful to store date and time since it is short and is just a number. I have created a class which has two functions. One for converting a datetime object to simple epoch time and the other for doing the reverse. The functions are shared functions which don’t need any instance to run. You can call them directly like:
Dim EpochNow As String = Epoch.ConvertDateTimeToEpoch(New Date(2014, 6, 13, 12, 0, 0, 0))

To get the actual date and time from epoch, you can write:
Dim MyDate As Date = Epoch.ConvertEpochToDateTime(1402642978506)
(The epoch time above is the time when I wrote this post).

To download the source file, click here.

Declaring a global connection string in ASP.NET

Using and interacting with database in every ASP.NET projects in now necessary. You cannot make a big project without a database. I have myself made many projects of my own in which I have used Microsoft’s SQL Server for database functionality.

Continue reading