Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I handle Null values in RadioButtonList controls?

Posted on 2007-12-06
8
Medium Priority
?
1,940 Views
Last Modified: 2012-06-27
I have a several bound fields in a formview whose values and visibility depend on a State field value. If the State= "Texas" then I need to hide five fields and display two other fields.

Null values in records where State="Texas" however, produce the 'SelectedValue which is invalid' error since the values are indeed NULL when I try to edit the record.

How can I work around the Null values? At what point in the process do I add code to show or hide the fields?

Thanks,
The Lake Man
<asp:FormView ID="FormView1" runat="server" DataSourceID="MeterObjectDataSource">
<EditItemTemplate>
......
<asp:RadioButtonList ID="TownshipDirRadioButtonList" runat="server" 
     RepeatDirection="Horizontal" 
     SelectedValue='<%# Bind("TownshipDir") %>'
     AppendDataBoundItems="True">
     <asp:ListItem>N</asp:ListItem>
     <asp:ListItem>S</asp:ListItem>
</asp:RadioButtonList>
.....
</EditItemTemplate>
</asp:FormView>

Open in new window

0
Comment
Question by:TheLakeMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 20423383
the formview has an itemdatabound event, you should handle null checking there
you can use DbNull to check if the value is in fact a null value or not then proceed
if (myvalue==DbNull.value)
{
//Do something
}
else
{
//Do something else.
}
check out this posting in the itemdatabound

http://forums.asp.net/t/1184912.aspx
0
 
LVL 33

Expert Comment

by:raterus
ID: 20423729
Ideally, I'd handle this in your database query with the IsNull (This is for SQL Server).  You can provide a value to return when its null.
0
 

Author Comment

by:TheLakeMan
ID: 20423985
Thanks guys for the quick response.

sammy1971: My formview does not have itemdatabound event. MSDN does not show an itemdatabound event either. The reference you gave discussed an OnDataBinding event. How would I code that in ASP and code behind?

raterus: I'm using the SQLServer. There is no value to return if it does not apply to that State. I just want to hide it from view.

What is the workaround to prevent the error message?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:raterus
ID: 20424003
if you don't even want it there, why not put a (where someColumn is not NULL) in your SQL Statement?
0
 

Author Comment

by:TheLakeMan
ID: 20424318
raterus:
The Where clause will not work because that would exclude the very record I am trying to bind.

I did find that when I added <asp:ListItem></asp:ListItem> the error message went away, but it also produced a third radio button. That is not a problem as long as I can hide the field.

How and under what event do I hide or display the fields?
0
 
LVL 33

Expert Comment

by:raterus
ID: 20427266
These are situations I ran into as well when I used the datasource controls.  I'm sure there are ways to work around it, but for me, I just avoid using these controls since there isn't a clear order of operations in my head.  I know that isn't really going to help you here...

Don't add the blank <asp:ListItem> for one,

And can you really try to explain exactly what you are doing here, in real terms.  What control are you binding that may have null values, and when you see a null value, what else should happen.
0
 
LVL 27

Accepted Solution

by:
Sammy Ageil earned 2000 total points
ID: 20429335
ooops I meant to say ItemUpdating thinking you were trying to do this after an update
anyway the OnDataBound event should handle this as well.

hope this helps

aspx should look like 
<asp:FormView ID="FormView1" runat="server" DataSourceID="MeterObjectDataSource" ondatabound="fViewOnDataBound">
<EditItemTemplate>
......
<asp:RadioButtonList ID="TownshipDirRadioButtonList" runat="server" 
     RepeatDirection="Horizontal" 
     SelectedValue='<%# Bind("TownshipDir") %>'
     AppendDataBoundItems="True">
     <asp:ListItem>N</asp:ListItem>
     <asp:ListItem>S</asp:ListItem>
</asp:RadioButtonList>
.....
</EditItemTemplate>
</asp:FormView>
 
protected void fViewOnDataBound(object sender, EventArgs e)
{
//Check if selectedvalue  is DBNull here and hide/show any other controls
 
}

Open in new window

0
 

Author Comment

by:TheLakeMan
ID: 20429721
Sammy1971:
The OnDataBound works great for hiding known items like TownshipDirRadioButtonList. But how do I iterate through all of the columns to see which ones are NULL. I think it has something to do with System.Data.DataRowView. Am I on the right track?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question