Radio Button Values from Database

Hello:

I have a form with radion buttons that I am I trying to show checked or not based on value in the database. The N is always checked even if the value is Y.

    <asp:RadioButtonList RepeatDirection="Horizontal" AutoPostBack="True" ID="Contract" runat="server">
    <asp:ListItem Value="Y">Y</asp:ListItem>
    <asp:ListItem Value="N">N</asp:ListItem>
    </asp:RadioButtonList>  

Open in new window


                if (reader["Contract"].ToString() == "Y")
                {
                    Contract.SelectedValue = "Y";
                }
                else
                {
                    Contract.SelectedValue = "N";
                }

Open in new window

LVL 1
RecipeDanAsked:
Who is Participating?
 
guru_samiCommented:
Because you are never setting any item as Selected i.e. your SelectedItem is null and when you try setting it's Value,  you get that exception.
0
 
NavneetSoftware EngineerCommented:
Hi!

Check if it's case-sensitive
reader["Contract"]..ToString().ToUpper() == "Y"

Thanks!
0
 
NavneetSoftware EngineerCommented:
Hi!

I tried
                if ("Y" == "Y")
                {
                    Contract.SelectedValue = "Y";
                }
                else
                {
                    Contract.SelectedValue = "N";
                }

Open in new window


It seems to work good

Thanks!
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
RecipeDanAuthor Commented:
All the Values are Upper Case in the Database.

@ navneethegde I get an unreachable code detected error message
0
 
NavneetSoftware EngineerCommented:
HI!

what does this gives you

                if (reader["Contract"].ToString().ToUpper() == "Y")
                {
                    Contract.SelectedValue = "Y";
                }
                else
                {
                    Contract.SelectedValue = "N";
                }

Open in new window


Can you  paste the event of code-behind where you calling this.
0
 
RecipeDanAuthor Commented:
The Selected Value is always N even if the value being pulled from the database is Y
0
 
NavneetSoftware EngineerCommented:
Yea!

That's why I want to check your code-behind to see where si the function calledtine in which rou
0
 
RecipeDanAuthor Commented:
I made some changes I now I am getting an error message

Object reference not set to an instance of an object.

Contract.SelectedItem.Value = reader["Contract"].ToString(); 

Open in new window

0
 
RecipeDanAuthor Commented:
Yes. I know...I figured it out

Contract.SelectedValue = reader["Contract"].ToString();

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.