Search code examples
asp.netradiobuttonlist

Input string not in correct format (Radiobuttonlist) asp.net


I'm trying to retrieve the text from a radiobutton to use in my sql query. I typed in this:

 int cardtype = int.Parse(rbcard.SelectedItem.Text);

and an error occurs

 Input string not in correct format

What seems to be the problem?


Solution

  • Lets say radio button list look like :

     <asp:RadioButtonList ID="rdID" runat="server">
    
    <asp:ListItem Text ="Item1" Value="1" />
    <asp:ListItem Text ="Item2" Value="2" />
    <asp:ListItem Text ="Item3" Value="3" />
    <asp:ListItem Text ="Item4" Value="4" />
    
    </asp:RadioButtonList>
    

    then to get selected value do:

    string selectedValue = rdID.SelectedValue;
            Response.Write(selectedValue);
    

    Then after getting the value as string, then you can parse it as int like:

    int x = Int32.Parse(selectedValue );
    

    Hopefully this can help you