Monday, March 12, 2012

how to handle nulls in a RadioButtonList

My webform RadioButtonList is bound to a column from a Sql Server dataSource. The radioButtonList is in a formView which starts up in edit mode. My question is how to handle null values because the only way I could get the page to come up when the columnn value is null is to have an item in the radio list which is setup for nulls such as, "Unknown" in the Text property and -1 in the value property. I understand that the RadioButtonList control is supposed to default to nulls which means that nothing in the radio list is selected - this is the behavior that I want, but I get the error below complaining that null (-1) is not valid because it does not exist as a valid value in the radio list: I have tried -1, '', etc. with no success.

'RadioButtonList2' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

How should I handle nulls at page startup?

Thanks.

While setting SelectedValue = null will normally allow no default selection for the RadioButtonList control, trying to set it to null in binding (using either Eval or Bind) doesn't work and throws an exception. Going through a method, however, seems to work as follows:

[ASPX]

<ItemTemplate>

<asp:RadioButtonListID="RadioButtonList1"runat="server"SelectedValue='<%# myFunc(Eval("NumValue").ToString()) %>'>

<asp:ListItemValue="1"Text="Item 001"></asp:ListItem>

<asp:ListItemValue="2"Text="Item 002"></asp:ListItem>

<asp:ListItemValue="3"Text="Item 003"></asp:ListItem>

<asp:ListItemValue="4"Text="Item 004"></asp:ListItem>

<asp:ListItemValue="5"Text="Item 005"></asp:ListItem>

</asp:RadioButtonList>

</ItemTemplate>

[code-behind: C#]

publicstring myFunc(string val)

{

if (val =="")

returnnull;

else

return val;

}


Thank you, the myFunc worked great for handling nulls, But it caused another problem with the update statement. My update statement no longer works because it has lost the identity of the bound variable somehow.

The default SelectedValue works fine:

aspx:

SelectedValue='<%# Bind("myField") %>'

UpdateCommand="update myTable setmyField=@.myField

But with your custom SelectedValue code I get the error at the bottom:

SelectedValue='<%# myFunc(Eval("r1").ToString()) %>'

UpdateCommand="update myTable setmyField=@.myField

here is the error:

Must declare the variable'@.myField'.

[SqlException (0x80131904): Must declare the variable'@.myField'.]

The customization to SelectedValue caused this, any idea why?

Thanks.


I can see why this would be an issue when trying to update since it deviates from the binding expected by the framework. Interesting situation you have here. I'll have to look into this before giving you an answer. I wouldn't be surprised if someone already faced such situation and resolved it.
Thanks. I am appreciative of your help.
I tried to see if there's a workaround for this issue, but I'm afraid there isn't as far as I can see. I thought that there might be a way to bind a NULL value into SelectedValue property (which can be done if a direct null value is assigned), but when binding to a table column, it rejects it for some reason. This problem/behavior may be related to the fact that RadioButtonList isn't meant to provide an option for "no selection" (i.e. once a selection is made, you cannot go back to a no-selection state), although the starting point allows no selection state. Perhaps the DropDownList control serves your purpose better since you can have a default value that represents no selection. Another option might be to add another item for your RadioButtonList that represents no selection and default what would have been null value to that choice. Sorry I don't have a better news for you.

Here's another way to handle this. Create a user control with your RadioButtonList in it, and create a property in it called SelectedValue such as following:

public string SelectedValue
{
get
{
return RadioButtonList1.SelectedValue;
}
set
{
if (value != "")
RadioButtonList1.SelectedVAlue = value;
}
}

This way you conditionally avoid setting the RadioButtonList value when it's null. Then you can happily use the SelectedValue='<%# Bind("FieldName") %>' syntax in your user control tag declaration.

Labels: , , , , , , , , , , , , ,

How to Handle Nulls

Nulls continue to kick my rear. It would have been so much easier to just
fill the DB with Zeros. But then people flame me for wasting drive space.
I just cant win! :)
Anyways, below is a snippet of code I'm using (minus the extras vacation and
holiday...). I need to add these fields up (along with other math) but a
lot of times the fields will be null. I cant seem to be able to do anything
with null fields regardless of how I format them. So I used try statements
and new variables to move the field data in to. Since the only result I can
get from adding these fields is an error the try statement is perfect
because then I can continue and fill with "0" since that would be the case.
BUT
Doing it this way takes a LONG time because you have to wait to each to
error out. I'm looping through these calcs many many times.
Try
RegHours = EmployeeRow.Item("Reg Hours")
Catch ex As Exception
RegHours = "0"
End Try
Try
OverHours = EmployeeRow.Item("Over Hours")
Catch ex As Exception
OverHours = "0"
End Try
If RegHours + OverHours < 80 Then...
So how can I format the following where if one field = 8 and the other field
= Null I still wind up with 8 after adding them instead of a system error
stating "anything" cant be converted to null.
EmployeeRow.Item("Reg Hours") + EmployeeRow.Item("Over Hours")
Thanks for any help,
JusitnHi Justin,
Not your code however maybe this helps better..
Using VB it is
datarowfield Is DbValue.Null (means Null in database notation)
Objectreference Is Nothing (means there is no reference)
Value = Nothing (means the value is in the defaultstate what can be "", 0 or
01-01-1900 00:00:00)
I hope this helps?
Cor
Hi
Or you can convert them to 0 while retrieving values from the table. For example in SQL Server you can do something like
select columnA = coalesce(ColumnA,'0'), columnB = coalesce(ColumnB,'0') from TableA
So, if ColumnA and B are null they are replaced with Zeros. This will prevent you from checking for nulls in your api. Hope this helps.
--
Ibrahim
"Justin Emlay" wrote:
> Nulls continue to kick my rear. It would have been so much easier to just
> fill the DB with Zeros. But then people flame me for wasting drive space.
> I just cant win! :)
> Anyways, below is a snippet of code I'm using (minus the extras vacation and
> holiday...). I need to add these fields up (along with other math) but a
> lot of times the fields will be null. I cant seem to be able to do anything
> with null fields regardless of how I format them. So I used try statements
> and new variables to move the field data in to. Since the only result I can
> get from adding these fields is an error the try statement is perfect
> because then I can continue and fill with "0" since that would be the case.
> BUT
> Doing it this way takes a LONG time because you have to wait to each to
> error out. I'm looping through these calcs many many times.
> Try
> RegHours = EmployeeRow.Item("Reg Hours")
> Catch ex As Exception
> RegHours = "0"
> End Try
> Try
> OverHours = EmployeeRow.Item("Over Hours")
> Catch ex As Exception
> OverHours = "0"
> End Try
> If RegHours + OverHours < 80 Then...
>
> So how can I format the following where if one field = 8 and the other field
> = Null I still wind up with 8 after adding them instead of a system error
> stating "anything" cant be converted to null.
> EmployeeRow.Item("Reg Hours") + EmployeeRow.Item("Over Hours")
> Thanks for any help,
> Jusitn
>
>
Is that supposed to be my select statement? I didn't understand it :(
This is one of my statements that I'm currently using:
SELECT TimeSheets.Employee, SUM(TimeSheets.[Hol Hours]) AS [Hol Hours],
TimeSheets.[Job Number], SUM(TimeSheets.[Over Hours]) AS [Over Hours],
SUM(TimeSheets.[Reg Hours]) AS [Reg Hours], SUM(TimeSheets.[Vac Hours]) AS
[Vac Hours], Employee.Type, TimeSheets.Dept, TimeSheets.GL FROM (Employee
INNER JOIN TimeSheets ON Employee.Employee = TimeSheets.Employee) WHERE
[Period Start] = @.PeriodStart GROUP BY TimeSheets.Employee, TimeSheets.[Job
Number], Employee.Type, TimeSheets.Dept, TimeSheets.GL ORDER BY
TimeSheets.Employee, TimeSheets.GL
"Ibrahim Shameeque" <IbrahimShameeque@.discussions.microsoft.com> wrote in
message news:AEAE83CC-FE57-4306-8D46-FC068A0AFFEC@.microsoft.com...
> Hi
> Or you can convert them to 0 while retrieving values from the table. For
example in SQL Server you can do something like
> select columnA = coalesce(ColumnA,'0'), columnB = coalesce(ColumnB,'0')
from TableA
> So, if ColumnA and B are null they are replaced with Zeros. This will
prevent you from checking for nulls in your api. Hope this helps.
> --
> Ibrahim
>
> "Justin Emlay" wrote:
> > Nulls continue to kick my rear. It would have been so much easier to
just
> > fill the DB with Zeros. But then people flame me for wasting drive
space.
> > I just cant win! :)
> >
> > Anyways, below is a snippet of code I'm using (minus the extras vacation
and
> > holiday...). I need to add these fields up (along with other math) but
a
> > lot of times the fields will be null. I cant seem to be able to do
anything
> > with null fields regardless of how I format them. So I used try
statements
> > and new variables to move the field data in to. Since the only result I
can
> > get from adding these fields is an error the try statement is perfect
> > because then I can continue and fill with "0" since that would be the
case.
> >
> > BUT
> >
> > Doing it this way takes a LONG time because you have to wait to each to
> > error out. I'm looping through these calcs many many times.
> >
> > Try
> > RegHours = EmployeeRow.Item("Reg Hours")
> > Catch ex As Exception
> > RegHours = "0"
> > End Try
> > Try
> > OverHours = EmployeeRow.Item("Over Hours")
> > Catch ex As Exception
> > OverHours = "0"
> > End Try
> >
> > If RegHours + OverHours < 80 Then...
> >
> >
> > So how can I format the following where if one field = 8 and the other
field
> > = Null I still wind up with 8 after adding them instead of a system
error
> > stating "anything" cant be converted to null.
> >
> > EmployeeRow.Item("Reg Hours") + EmployeeRow.Item("Over Hours")
> >
> > Thanks for any help,
> > Jusitn
> >
> >
> >
> >
I googled for "Coalesce" and got nothing but unrelated items. The one link
that did look like a match discussed T-SQL.

Labels: , , , , , , , , , , , ,