Introduction
In this article, I want to document a bug in Visual Studio. NET concerning to setting a null value to entity property's through a Textbox control. There is a common scenario to have an entity type in your data model where some attributes are optional. When you develop a rich client application using the Windows forms, data binding techniques and a Textbox to enter the simple data, and you want to set null the optional attributes of the underlying data source by leaving in blank the Text property of the Textbox control; then there is a missing feature for the Formatting and Advanced Binding dialog box for null values. They should have a checkbox there to allow people to use blank as null. In this article, I will show one simple solution to this bug.
Getting started with the solution
Let's supposed that we have the table schema for the dept table representing the department business entity (see Listing 1).
CREATE TABLE [dbo].[dept](
[deptno] [int] NOT NULL,
[dname] [varchar](60) NULL,
[loc] [varchar](60) NULL,
[rowversion] [timestamp] NOT NULL,
PRIMARY KEY CLUSTERED([deptno] ASC)
)
Listing 1
Let's add one DataSet NullValueDS and one Windows Form NullValueForm artifact. Add a table and a table adapter item onto the DataSet NullValueDS (see Figure 1 and Figure 2).
Figure 1
Figure 2
Now open the NullValueForm Windows Form, change data presentation style of the dept node (representing the dept entity) from DataGridView to Details in the Data Sources (see Figure 3).
Figure 3
Then, drag and drop the dept node in the Data Sources onto the form (see Figure 4).
Figure 4
If you run this application and try to leave the dname and loc optional fields in blank, and then query the table on the data source, you can see that the value is an empty string not a null value. In order, set a null value on the underlying attributes of the data source, you must check on the dname and loc Textbox controls and click on the (Data Bindings) pseudo-property on the Property window. Set a tag identifier for the Null value on Formatting and Advanced Binding dialog box (see Figure 5).
Figure 5
Then go to the NullValueForm.Designer.cs and search for the Tag_Null string. Once you have found this string, you need to replace the Tag_Null string for an empty string (see Listing 2).
this.dnameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.deptBindingSource, "dname", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, ""));
Listing 2
Now, run the application and leave in blank the dname field (see Figure 6).
Figure 6
When you run a query against the underlying data source, you see the null value associated to the column of the underlying row (see Listing 3).
deptno dname loc ----------- ------------------------------------------------------------ ------------------------------------------------------------ 10 NULL New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston
(4 row(s) affected) |
Listing 3
Conclusion
In this article, I've illustrated how to set a null value to entity property's through a blank string in a Textbox control. Now you can apply this solution to your own situation.