Inserting NULL into a row RRS feed

  • Question

  • Hello,

    I use a strongly typed dataset to access my products-table.
    Unfortunately I can't inert NULL values into fields, which could contain NULL values (I use SQL Server Express).
    Here is some vode:

            LSOS.ProductsDataTable dt = new LSOS.ProductsDataTable();
            dt.AddProductsRow("Test1", "Test2", 2, 0, "Test3", 0, true);
            LSOSTableAdapters.ProductsTableAdapter ta = new LSOSTableAdapters.ProductsTableAdapter();

    Have a look at the following line:

    AddProductsRow("Test1", "Test2", 2, 0, "Test3", 0, true)

    The 4th and 6th parameters should be NULL, but that isn't possible, because Int32-variables can't hold NULL.
    And I couldn't find any way to let the Dataset shema designer know, that i want these two fields to be nullable (Int32?). Is there any workaround?

    The two variables belong to foreign key fields in the database, which should be initially set to NULL. And basically this is what i want to be done.

    Every answer would be appreciated! Thanks!
    Saturday, July 14, 2007 2:11 PM

All replies

  • First of all, when you are in design mode of the dataset and you click on a column in a datatable there is a property (in the Properties window) to make the column nullable (AllowDBNull)... so make sure you have set that to "true".  Even if you do that however, the code you displayed still throws an error.  If you right-click on the "AddProductsRow" method (in your code window) and choose "Go to definition" it should take you into the VS-generated code and show you what is going on behind the scenes.  Mine showed that if you pass a value of "null", the generated "AddProductsRow" code thinks the incoming value is a reference to a row in the foreign/related table, and (without checking to see if that row passed as an argument to the method is null) automatically tries to get the value of the first column in that row... which throws an error because the row itself is null so there isn't a column to refer to.  Here is the code that VS generated for the "AddProductsRow" in my sample project:



    public ProductsRow AddProductsRow(string Column1, string Column2, int Column3, DataTable2Row parentDataTable2RowByFK_DataTable2_Products, string Column5, DataTable1Row parentDataTable1RowByFK_DataTable1_Products, bool Column7)


       ProductsRow rowProductsRow = ((ProductsRow)(this.NewRow()));


       rowProductsRow.ItemArray = new object[] {











          return rowProductsRow;



    The two bolded lines above are where the problem occurs.


    So, one possible option for you is to do this:


       DataSet1.ProductsRow dr;

       dr = dt.NewProductsRow();

       dr.ItemArray = new object[]{"Test1", "Test2", 2, null, "Test3", null, true};



    Sunday, July 15, 2007 6:33 AM