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

ASP.NET

Avatar of undefined
Last Comment
rajeeshmca

8/22/2022 - Mon
Si_Hibbard

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.
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?
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sybe

> 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.


Si_Hibbard

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>
chrisgreaves

Hi,
You should use a check box for this. The user has the option to check it or not check it.
Chris.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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?
rajeeshmca

what do you exactly want to do??? if nothing is selected what should be stored???
chrisgreaves

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
d2fox

ASKER
fajeeshmca, if there is nothing selected, I want to store a null
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).
rajeeshmca



HI d2fox,

Try this

.Add(New SqlParameter("@Insurance", Insurance.SelectedIndex == -1? (object)DBNull.Value:(object)Insurance.SelectedValue))
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
d2fox

ASKER
rajeeshmca, thank you, that's interesting, can you explain?
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
rajeeshmca

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rajeeshmca

Its called the ternary operator.... short form for the if condition...
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy