How to: Bind Windows Forms Controls to DBNull Database Values

When you bind Windows Forms controls to a data source and the data source returns a DBNull value, you can substitute an appropriate value without handling, formatting, or parsing events. The NullValue property will convert DBNull to a specified object when formatting or parsing the data source values.

The following example demonstrates how to bind a DBNull value in two different situations. The first demonstrates how to set the NullValue for a string property; the second demonstrates how to set the NullValue for an image property.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace DBNullCS
	public class Form1 : Form
		public Form1()
			this.Load += new EventHandler(Form1_Load);

        // The controls and components we need for the form.
        private Button button1;
        private PictureBox pictureBox1;
        private BindingSource bindingSource1;
        private TextBox textBox1;
        private TextBox textBox2;

        // Data table to hold the database data.
        DataTable employeeTable = new DataTable();

		void Form1_Load(object sender, EventArgs e)
            // Basic form setup.
            this.pictureBox1 = new PictureBox();
            this.bindingSource1 = new BindingSource();
            this.textBox1 = new TextBox();
            this.textBox2 = new TextBox();
            this.button1 = new Button();
            this.pictureBox1.Location = new System.Drawing.Point(20, 20);
            this.pictureBox1.Size = new System.Drawing.Size(174, 179);
            this.textBox1.Location = new System.Drawing.Point(25, 215);
            this.textBox1.ReadOnly = true;
            this.textBox2.Location = new System.Drawing.Point(25, 241);
            this.textBox2.ReadOnly = true;
            this.button1.Location = new System.Drawing.Point(200, 103);
            this.button1.Text = "Move Next";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.ClientSize = new System.Drawing.Size(292, 273);

            // Create the connection string and populate the data table
            // with data.
            string connectionString = "Integrated Security=SSPI;" +
				"Persist Security Info = False;Initial Catalog=Northwind;" +
				"Data Source = localhost";
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = connectionString;
            SqlDataAdapter employeeAdapter = 
                new SqlDataAdapter(new SqlCommand("Select * from Employees", connection));

            // Set the DataSource property of the BindingSource to the employee table.
            bindingSource1.DataSource = employeeTable;

           // Set up the binding to the ReportsTo column.
            Binding reportsToBinding = textBox2.DataBindings.Add("Text", bindingSource1, 
                "ReportsTo", true);

            // Set the NullValue property for this binding.
            reportsToBinding.NullValue = "No Manager";

            // Set up the binding for the PictureBox using the Add method, setting
            // the null value in method call.
            pictureBox1.DataBindings.Add("Image", bindingSource1, "Photo", true, 
                DataSourceUpdateMode.Never, new Bitmap(typeof(Button), "Button.bmp"));

            // Set up the remaining binding.
            textBox1.DataBindings.Add("Text", bindingSource1, "LastName", true);

        // Move through the data when the button is clicked.
        private void button1_Click(object sender, EventArgs e)

		static void Main()
			Application.Run(new Form1());


The types of the bound property and the NullValue property must be the same or an error will result, and no further NullValue values will be processed. In this situation, an exception will not be thrown.

This example requires:

  • References to the System, System.Data, System.Drawing and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. For more information, see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio and How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

Community Additions