Mar 4 2006

Difference between NULL and DBNull

I have always misunderstood the difference between NULL and DBNULL until recently while I was working on ASP.NET web application, and was talking to a Webservice and I found an exception coming from one of the web methods inside the Webservice which is using the SqlCommand.ExecuteScalar() method.

What happens when the ExecuteScalar() method is used? Usually, the ExecuteScalar() method returns the First Column of the First Row returned by the database stored procedure or SQL query.

Suppose for example you are searching for the CustomerPhone of the customer named "Bilal Haidar", and that customer didn't have any record in the database, so a query such as:

public string Get_Customer_ID(string UserName)
{
   // Prepare database connection
   SqlConnection conn = new SqlConnection("...");
   // Prepare command to execute
   SqlCommand cmd = new SqlCommand("SELECT CustomerPhone FROM Custoers WHERE CustomerName = 'Bilal Haidar'", conn);

   // Execute the command and store result in an object
   object objResult = cmd.ExecuteScalar();






if (objResult == null) return "Customer not found in our database";
if (objResult == System.DBNull.Value) return "Customer found but his/her phone number is null"; return (string) objResult; }

So now here how it goes. The query will search for the Customer Phone. We are returning the result and storing it in the objResult object.

If the result object is null, then there is no record in the database with the username specified.

If the result value is not null, we need to check if the Cutomer Phone field has a value or not, how do you do so? Using the DBNull.Value would check for you if the field returned is null or not.

So, comparing the ExecuteScalar() to null means, we are checking if there is a record returned, and checking if value of the first column of the first row is null or not, means we need to check against DBNul..Value

Hope you have now a better understanding of the difference between NULL and DBNull

Regards

Tags: , , , ,

Mar 4 2006

ASP.NET and DNN Website deploy

When you developer an ASP.NET web application or a DotNetNuke application, you will be having all types of file extensions:

  • .csproj
  • .cs
  • .vb
  • .su
  • .sln
  • etc ...

So, imagine you are developing a huge web application or even a DotNetNuke website and now it is time to deploy your application. Are you going to go through each single folder in your web application, remove all un needed files and leave the aspx, ascx, css, etc .. files for deployment? Maybe that can be done for small applications, but not for DoetNetNuke applications nor for huge ASP.NET web applications.

The solution found here is called DNNDeploy which you can read more about it here:

ASP.NET Deployment App

This tool goes through all your files in the application, allows you to choose what file extensions to keep and generates a web site ready for deployment.

I am sure this helps us a lot.

Regards

Tags: , ,

Feb 27 2006

ASP.NET 2.0 How Do I Video Series

Category: ASP.NET 2.0 - GeneralBil@l @ 17:17

ASP.NET HOW DO I Video Series: Caching (Part 1)
This video is part one of a two-part series introducing the new caching features of ASP.NET 2.0. Start with a demonstration of Page Output Caching followed by an overview of the new ASP.NET 2.0 Database Caching support for SQL Server 2000 and SQL Server 2005. Topics include using the aspnet_regsql.exe utility to add SQL caching support to a SQL Server database, creating a SQL cache dependency to eliminate unnecessary connections to the database, enabling table-level caching, and enabling enhanced caching support using SQL Server 2005 Change Notifications.