ASP.NET DBNull Value

The DBNull represents an uninitialized variant or nonexistent database column. It is not the number zero or it is not an empty string value. DBNull is a singleton class, which means only this instance of this class can exist. The DBNull.Value member represents the sole DBNull object.

In many situations while reading data from DataSource, we have seen the error message like the following :

Conversion from type 'DBNull' to type '' is not valid

This message is getting because the ASP.NET program unable to handle DBNull value. In these cases you can determine whether a value retrieved from a database field is a DBNull value by passing the value of that field to the DBNull.Value.Equals method.

VB.Net
If IsDBNull(ds.Tables(0).Rows(i).Item(0)) Then Label1.Text = "DBNULL exist in the field " End If
C#
if (ds.Tables[0].Rows[0].ItemArray[0] == System.DBNull.Value) { Label1.Text = "DBNULL exist in the field "; }

The following ASP.NET program is checking wether the retrieved values is DBNull.

Default.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; using System.Data ; using System.Data.SqlClient ; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString(); SqlConnection connection = new SqlConnection(connectionString); DataSet ds = new DataSet(); string sql = "select count(*) from tablename"; try { connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sql, connection); adapter.Fill(ds); connection.Close(); if (ds.Tables[0].Rows[0].ItemArray[0] == System.DBNull.Value) { Label1.Text = "DBNULL exist in the field "; } else { Label1.Text = ds.Tables[0].Rows[0][0].ToString(); } } catch (Exception ex) { Label1.Text = "Error in execution " + ex.ToString(); } } }
Full Source | VB.NET
Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionString As String Dim connection As SqlConnection Dim ds As New DataSet Dim i As Integer connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString connection = New SqlConnection(connectionString) Dim sql As String = "select count(*) from publishers" Try connection.Open() Dim adapter As New SqlDataAdapter(sql, connection) adapter.Fill(ds) connection.Close() If IsDBNull(ds.Tables(0).Rows(i).Item(0)) Then Label1.Text = "DBNULL exist in the field " Else Label1.Text = CInt(ds.Tables(0).Rows(i).Item(0)) End If Catch ex As Exception Label1.Text = "Error in execution " & ex.ToString End Try End Sub End Class



Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb