Search code examples
.netstrongly-typed-dataset

Typed DataSet in .NET Framework


I've the need to automatically get a DataSet from a DB table, I used internal Visual Studio tool, and between the millions of lines created, this is one of the accessor method:

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public string Referente {
            get {
                try {
                    return ((string)(this[this.tableCATALOGO_Cliente.ReferenteColumn]));
                }
                catch (global::System.InvalidCastException e) {
                    throw new global::System.Data.StrongTypingException("The value for column \'Referente\' in table \'CATALOGO_Cliente\' is DBNull.", e);
                }
            }
            set {
                this[this.tableCATALOGO_Cliente.ReferenteColumn] = value;
            }
        }

As you can see, this code refer to the Referente columns. When I need to get a generic Referente if is NULL the return can't happen cause exception is throwned. I resolved the issue replacing the return statement with:

return this[this.tableCATALOGO_Cliente.ReferenteColumn] as string;

Since the table contains hundred of columns I would a way to automatically this process (i.e. I would generate a DataSet with accessor method that doesn't thrown exception if IS NULL happens).


Solution

  • In the dataset designer, select (Empty) instead of (Throw Exception) in the NullValue field. enter image description here

    If you are accessing the dataset in code, you need to use the IsxxxNull method on the datarow: In this code, r is the datarow, _DateOpened is a Nullable (of Date)

      If r.IsDateOpenedNull Then
            _DateOpened = Nothing
        Else
            _DateOpened = r.DateOpened
        End If