Link to home
Create AccountLog in
Avatar of d2fox
d2fox

asked on

getting selected value when there is no selected value in asp.net

So i have the code in my page below which is displayed for the first time to collect data from the user, when I  don't selected anything, submit the screen and then execute the statement:

If Insurance.SelectedValue Is Nothing Then

it fails this check.  It also stores a 'false' in my database, when I
store
Insurance.SelectedValue, tied to a bit field in my database.



How can I find out if there was nothing selected by the user.
<asp:RadioButtonList ID="Insurance" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Value="True">Yes</asp:ListItem>
                        <asp:ListItem Value="False">No</asp:ListItem>
                    </asp:RadioButtonList>

Open in new window

Avatar of Si_Hibbard
Si_Hibbard
Flag of United States of America image

A dropdown list will always have something selected.

IF you dont specify the selected value it will just be the last one - which is why you get false entered into the DB. What else do you want to store in the DB anyway?

If you want to leave the DB entry as null then you could add another item like
 <asp:ListItem Value="">Please select</asp:ListItem>

then in code behind
If Insurance.SelectedValue !=""
{
 store in DB
}

So if the value is blank then dont update the DB.
Avatar of d2fox
d2fox

ASKER

wow, I did not know that...this is a radiobutton list, does that make any difference?  So it defaults to the LAST one

so can I create a last one that's not visible, tie it to my bit field and have the bit field stay null?

that way I wouldn't need to code checks on each field (I have several like this)

or is it better to code each one?
Avatar of rajeeshmca
HI d2fox,

u can use the SelectedIndex property of the RadioButtonList....

If nothing is selected then it will be -1.

if -1 u can insert null in to the Database
> A dropdown list will always have something selected.
Only when it is a default dropdown. Setting "size" to more than 1 or using "multiple" will both allow for dropdown lists with no selected value. And of course a dropdown with no options will have no selected value.


> IF you dont specify the selected value it will just be the last one
No. It will by default select the first option.

> How can I find out if there was nothing selected by the user.
I would check the Request.Form. If Request.Form("yourradiobutton") has no value, nothing was checked by the user. But I realize that this depends on avoiding webforms.


You can set which listitem you want selected by using Selected="True" and maybe set the value to null, but I am not sure if it will insert a 'null' value like you want without doing some codebehind check, you could try this but I havent had a chance to try myself.

<asp:ListItem Text="null" Value="null" Selected="True"></asp:ListItem>
or
<asp:ListItem Text="null" Value='<%= System.DBNull.Value %>' Selected="True"></asp:ListItem>
Hi,
You should use a check box for this. The user has the option to check it or not check it.
Chris.
Avatar of d2fox

ASKER

so if I have a database field that I want to represent this value, I said:

 .Add(New SqlParameter("@Insurance", Insurance.SelectedValue))

Where the field that I am updating with the @Insurance parameter is a bit field.

but this doesn't work, as if there is nothing selected yet, it stores a false.  How can i do ths?
what do you exactly want to do??? if nothing is selected what should be stored???
Hi,
If you choose to use a checkbox, then it is this...
dim InsuranceSelected as boolean
if checkbox1.checked then
   InsuranceSelected = true
else
   InsuranceSelected = false
end if

or, more simply...
dim InsuranceSelected as boolean = checkbox1.checked

then
.Add(New SqlParameter("@Insurance", InsuranceSelected))
Chris
Avatar of d2fox

ASKER

fajeeshmca, if there is nothing selected, I want to store a null
Avatar of d2fox

ASKER

so if it's 'true' selected, i want to store 'true', if it's 'false' I want to store 'false' and if there is nothing selected I want to store a null (or nothing).


HI d2fox,

Try this

.Add(New SqlParameter("@Insurance", Insurance.SelectedIndex == -1? (object)DBNull.Value:(object)Insurance.SelectedValue))
Avatar of d2fox

ASKER

rajeeshmca, thank you, that's interesting, can you explain?
Avatar of d2fox

ASKER

So here is what I wrote in VB to correspond to this:

 .Add(New SqlParameter("@Insurance", If((Insurance.SelectedValue = -1), System.DBNull.Value, DirectCast(Insurance.SelectedValue, Object))))


I get an error  BC30201: Expression expected.

Please let me know if this is not what I should write

ASKER CERTIFIED SOLUTION
Avatar of rajeeshmca
rajeeshmca
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Its called the ternary operator.... short form for the if condition...