Binding RadioButtonList to nullable varchar workaround? (SelectedValue which is invalid because it does not exist…)

后端 未结 2 1825
小蘑菇
小蘑菇 2021-01-24 02:20

I have a nullable varchar column in a table in my database.
I am binding a gridview to this table via an objectdatasource and have a TemplateField like so:

          


        
相关标签:
2条回答
  • 2021-01-24 03:00

    I'm not sure what DB you're using, but I'd start by using the NullIf statement for the nullable database fields.

    Select NullIf(RequirementOption, 'None') From ...
    

    That will help you avoid the Object Reference Not Set to an Instance of an Object errors because you won't be returning nulls from the data store.

    0 讨论(0)
  • 2021-01-24 03:13

    you shoud use:

       <asp:RadioButtonList runat=server ID="rd" SelectedValue='<%# Bind("sex").GetType() == typeof(DBNull) ? null : Bind("sex") %>'
               <asp:ListItem Text="male" Value="1"></asp:ListItem>
               <asp:ListItem Text="female" Value="2"></asp:ListItem>
               </asp:RadioButtonList>
    
    0 讨论(0)
提交回复
热议问题