locked
ObjectDataSource Cant set a nullable GUID field RRS feed

  • Question

  • User613489665 posted

    Hi

    I have entity object that has a nullable GUID property. I use a ObjectDataSource to bind a GridView to a list of my entity object. I use a DropDown (ComboBox) to select a value for this GUID property. ListItem accepts string for value and text for dropdown items. I use empty string for null value but ObjcetDataSource cant convert empty string to NULL or DBNull.Value in this case I get an Exception that says "Cant Convert System.String to System.GUID.

    Does any body know how to resolve this problem?

    Monday, June 6, 2011 7:56 AM

Answers

  • User613489665 posted

    Hi Guys none of the above solutions worked for me. I did override ObjectDataSource and ObjectDataSourceView

    In update and Insert method I checked Nullable Guid's and replaced empty string with null.

    It works fine.

    Thanks a lot

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2011 4:43 AM

All replies

  • User1564875471 posted

    Try to use "Object" for the paramter type instead of Guid.

    Monday, June 6, 2011 8:00 AM
  • User613489665 posted

    I cant change the entity object.

    Monday, June 6, 2011 8:07 AM
  • User1564875471 posted

    Try to Set the "ConvertEmptyStringToNull" property for your paramter to true:

    <asp:ControlParameter ConvertEmptyStringToNull="true" Name="..." />

    If it doesn't worked, try this solution:

    Instead of using ControlParameter, use the normal Parameter like below:

     <asp:Parameter Name="..." Type="Object" />

    Then, you should handle the Selecting event of the ObjectDataSource and pass the selected value programatically like below:

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (String.IsNullOrEmpty(DropDownList1.SelectedValue))
            {
                //There is no selected value , so we pass null for your method paramter
                e.InputParameters[0] = null;
            }
            else
            {
             //There is a value selected in the DropDowList, so we pass it to the 
                e.InputParameters[0] = DropDownList1.SelectedValue;
            }
        }
    Tuesday, June 7, 2011 3:34 AM
  • User3866881 posted

    Hello:)

    Please pay attention to it that Guid is a very special string which has a serious of chars combined together. So a single string cannot be substituted for that.

    And what's more, Please try the Moderator's idea and make sure that your Dropdownlist's SelectedValue should be a right proper form of GUID, please use "new Guid().ToString()" to generate it.

    Thx

    Tuesday, June 7, 2011 9:44 PM
  • User613489665 posted

    Hi Guys none of the above solutions worked for me. I did override ObjectDataSource and ObjectDataSourceView

    In update and Insert method I checked Nullable Guid's and replaced empty string with null.

    It works fine.

    Thanks a lot

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2011 4:43 AM