none
Having trouble with Typed Data Sets and MVC 2.0 RRS feed

  • Question

  • Hello.

    I have been working with typed datasets with MVC 2.0 in VS 2010 Beta 2. For a test, I was trying to replace the Nerd Dinner LinqToSql model included in the sample Model with one that uses a typed data set generated from Visual Studio 2010. On the "Host Dinner functionality", when creating a new event I get the following InvalidCastException thrown- specifically when MVC tries to bind the typed datatable Dinner model to the Views/Dinner/Edit.aspx View:

    Unable to cast object of type 'System.DBNull' to type 'System.String'.

    The code which is culprit to these shenanigans is the get accessor to Country Property below:
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
    
    
    
    public string Country {
    
        get {
    
           return ((string)(this[this.tableDinner.CountryColumn]));}
    
    





    set { this[this.tableDinner.CountryColumn] = value;} }
    Any Thoughts?

    Best,
    Nick



    Friday, February 5, 2010 10:58 PM

Answers

  • Hello Nick,

     

    Based on my test, VS2010 Beta 2 does not change much on the typed DataSet comparing with VS2008.   If the column’s property AllowDBNull is False, auto-generated get/set codes are similar with your sample codes.  If the AllowDBNull property is True and NullValue property is (Throw exception), the get/set codes will be:

    ==========================================================================================

                public string Country {

                    get {

                        try {

                            return ((string)(this[this.tableDinner.CountryColumn]));

                        }

                        catch (global::System.InvalidCastException e) {

                            throw new global::System.Data.StrongTypingException("The value for column \'Country\' in table \'Dinner\' is DBNull.", e);

                        }

                    }

                    set {

                        this[this.tableDinner.CountryColumn] = value;

                    }

                }

    ==========================================================================================

     

    In your case, the Country column seems to contain NULL values at the database side.  However, it is set not AllowDBNull in the typed DataSet.  As a workaround, we can set the Country column’s AllowDBNull property to True and the NullValue property to (Null) or (Empty).  The auto-generated codes would be similar with:

    ==========================================================================================

                public string Country {

                    get {

                        if (this.IsCountryNull()) {

                            return null;

                                // return string.Empty;

                        }

                        else {

                            return ((string)(this[this.tableDinner.CountryColumn]));

                        }

                    }

                    set {

                        this[this.tableDinner.CountryColumn] = value;

                    }

                }

    ==========================================================================================

     

    Important to mention, this workaround can only work in string typed columns.  If the column is in other data types, we need to manually create the get/set codes like the above codes to handle the DBNull. 

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, February 8, 2010 8:05 AM
    Moderator

All replies

  • Hello Nick,

     

    Based on my test, VS2010 Beta 2 does not change much on the typed DataSet comparing with VS2008.   If the column’s property AllowDBNull is False, auto-generated get/set codes are similar with your sample codes.  If the AllowDBNull property is True and NullValue property is (Throw exception), the get/set codes will be:

    ==========================================================================================

                public string Country {

                    get {

                        try {

                            return ((string)(this[this.tableDinner.CountryColumn]));

                        }

                        catch (global::System.InvalidCastException e) {

                            throw new global::System.Data.StrongTypingException("The value for column \'Country\' in table \'Dinner\' is DBNull.", e);

                        }

                    }

                    set {

                        this[this.tableDinner.CountryColumn] = value;

                    }

                }

    ==========================================================================================

     

    In your case, the Country column seems to contain NULL values at the database side.  However, it is set not AllowDBNull in the typed DataSet.  As a workaround, we can set the Country column’s AllowDBNull property to True and the NullValue property to (Null) or (Empty).  The auto-generated codes would be similar with:

    ==========================================================================================

                public string Country {

                    get {

                        if (this.IsCountryNull()) {

                            return null;

                                // return string.Empty;

                        }

                        else {

                            return ((string)(this[this.tableDinner.CountryColumn]));

                        }

                    }

                    set {

                        this[this.tableDinner.CountryColumn] = value;

                    }

                }

    ==========================================================================================

     

    Important to mention, this workaround can only work in string typed columns.  If the column is in other data types, we need to manually create the get/set codes like the above codes to handle the DBNull. 

     

    If you have any questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, February 8, 2010 8:05 AM
    Moderator
  • Hello Nick,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, February 11, 2010 12:57 AM
    Moderator
  • Thanks for you help with this and yes, it did solve the binding problem. I did however run into another problem when doing inserts with the TableAdapterManager.UpdateAll(..) method: not resolving autogeneriated identity columns when inserting a new Typed Data Row after insert. In my case, DinnerDataRow with the System.Int32 DinnerID column did not contain the new identity value created in SQlServer for the row after the UpdateAll method completed. Is there anyway on the typed data set design surface to resolve this issue? (IE clicking a checkbox option?)

    Best,

    Nick Muhonen
    Friday, February 19, 2010 10:38 PM
  • Hello Nick,

    Could you please open a new thread to discuss the new question?   Please tell me the thread link, so that we can do some further discussion.  Also, it will benefit more community members who encounter the similar issues.   I will be always here to help.  

    Have a nice day!
     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, February 22, 2010 2:15 PM
    Moderator