-1

I got the error in unit testing for the method of "AddEffectiveQuarterTest". How to set the property to true that will allow me to insert null values?

AvailabilityBLTest.cs file : "AddEffectiveQuarterTest" method

    public void AddEffectiveQuarterTest()
    {
        AvailabilityBL target = new AvailabilityBL();
        AvailabilityDS ds = new AvailabilityDS();
        TestBusinessLogic.BusinessLogic_AvailabilityBLAccessor accessor = new TestBusinessLogic.BusinessLogic_AvailabilityBLAccessor(target);

        // SETUP STANDARD TEST DATA
        this.SetupTestData(ds, null);
        int newModelID = -1;    
        .........
    }

AvailabilityDS.Designer.cs file

public string QuarterDisplay {
    get {
            try {
                return ((string)(this[this.tableTime.QuarterDisplayColumn]));
            }
            catch (global::System.InvalidCastException e) {
                throw new global::System.Data.StrongTypingException("The value for column \'QuarterDisplay\' in table \'Time\' is DBNull.", e);
        }
    }
    set {
            this[this.tableTime.QuarterDisplayColumn] = value;
    }
}

How do I edit with the following code to set the property to true that will allow me to insert null values?

public bool AllowDBNull { get; set; }

How do I check the dbnull? I got the error with the following code.

 public bool IsColumnNameNull() {
                    return this.IsNull(this.tableColumn.ColumnNameColumn);
                }

 public string SetColumnKeyNull() {
            {
                get
                {
                    try
                    {
                        return ((string)(this[this.tableColumn.ColumnKeyColumn]));
                    }
                    catch (global::System.InvalidCastException e)
                    {
                        throw new global::System.Data.StrongTypingException("The value for column \'SetColumnKeyNull\' in table \'Column\' is DBNull.", e);
                    }
                }
                set
                {
                    this[this.tableColumn.ColumnKeyColumn] = value;
                }
            }
iop
  • 39
  • 1
  • 7

1 Answers1

0

You appear to be using a strongly-typed DataSet.

In this case, the generated code will contain methods to set fields to null.

For example, if your field QuarterDisplay allows DBNull, the generated code in AvailabilityDS.Designer.cs will include a method SetQuarterDisplayNull(). It will also include a method IsQuarterDisplayNull() that you can use to check for a DBNull value.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • I do not have a method to SetQuarterDisplayNull(), should I create another method for these case? I am just new in unit testing... – iop Dec 11 '14 at 07:47
  • I do not have the generated code that include the method SetQuarterDisplayNull() and IsQuarterDisplayNull(), is that any other ways to solve? How to fix the "null"? make it non-null, or perform a null-test? – iop Dec 11 '14 at 07:52
  • If you don't have those methods, it means the corresponding column is not nullable. You could consider opening the AvailabilityDS.xsd file in the Visual Studio DataSet designer, and changing the AllowDBNull property of the column to True in column Properties. This will result in the methods being added to the generated code. – Joe Dec 11 '14 at 08:17
  • Very clear explanation, can I ask how to check for a DBNull value? I have modified some at here,http://forums.asp.net/t/1383849.aspx?How+to+check+dbnull+in+C+, but could not work. – iop Dec 11 '14 at 08:57
  • "can I ask how to check for a DBNull value" - in a strongly-typed dataset, you can use the generated method `IsColumnNameNull()` as described in the answer. – Joe Dec 11 '14 at 10:51
  • hi @Joe, i have edited as above,may u explain more precisely how to check for a DBNull value? I am first time doing these =D thx – iop Dec 11 '14 at 14:48
  • @iop - Have you opened your AvailabilityDS.xsd file in the Visual Studio designer, and set the "AllowDBNull" property to `True` for the `QuarterDisplay` column? If you have done this, the generated code in `AvailabilityDS.Designer.cs` will contain methods `SetQuarterDisplayNull()` and `IsQuarterDisplayNull()` - you don't need to write them yourself. – Joe Dec 11 '14 at 15:02
  • yes...i have done all of these, but my test case still fail...which part i have left out? – iop Dec 11 '14 at 15:05
  • I can see the "SetQuarterDisplayNull() and IsQuarterDisplayNull()" now, but my test case still fail.. – iop Dec 11 '14 at 15:06