locked
...has a SelectedValue which is invalid because it does not exist in the list of items. Question RRS feed

  • Question

  • User1394791479 posted

     Hi-

    I have run across this issue a few times, and wondering if there is an eligant way to solve it.

    I have a web form that uses a detail view. Some of the fields are drop-downs, some are check boxes some are radio buttons in the edit section of the template fields. I am populating the controls with a datasource that gets unique fields from the database, and binds to that column. IF I don't include a null value in my data source, I get the dreaded error"

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

     

    It makes sense to me, Im just not sure how to fix it. I get the error for NEW records that have a null value in the specific field,

    In other words, this will work fine (first radio button represents a null value)

    Apply WS $ to this job?
    <input id="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_0" value="" name="ctl00$ContentPlaceHolder1$DetailsView1$RadioButtonList1" type="radio"> <input id="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_1" checked="checked" value="All" name="ctl00$ContentPlaceHolder1$DetailsView1$RadioButtonList1" type="radio"><label for="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_1">All</label> <input id="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_2" value="None" name="ctl00$ContentPlaceHolder1$DetailsView1$RadioButtonList1" type="radio"><label for="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_2">None</label> <input id="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_3" value="Split" name="ctl00$ContentPlaceHolder1$DetailsView1$RadioButtonList1" type="radio"><label for="ctl00_ContentPlaceHolder1_DetailsView1_RadioButtonList1_3">Split</label>


    The above would fail if I didnt incluse the null value in my select.

     

    TIA for any direction

     

    Dan 

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

     

    Thursday, October 18, 2007 7:19 AM

Answers

  • User1394791479 posted

     This may be more of a workaround, than the specific code I am looking for, but here goes:

    at the creation of a record (Function that starts a new profile) I am going to set default values at the sqldatasourceupdating event.

    its a bit of work, but looks like this:

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {//need to get max pronum, add 1 and set as new pronum, and return 1 if new
               
                String tempusername = Page.User.Identity.Name;
                Int32 pronum ;
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SAITS@SQLAuth"].ToString());
                connection.Open();
                SqlCommand command = new SqlCommand("Select max(ProNum) FROM TimeMyProfile WHERE UserName = '" + tempusername + "'", connection);
                string returnval = (command.ExecuteScalar().ToString()) ;
                    if (returnval.Length>0)//ie NOT first number
                        pronum = ((Int32)command.ExecuteScalar())+1;
                    else
                        pronum = 1;
                connection.Close();

            e.Command.Parameters["@ProNum"].Value = pronum;
            e.Command.Parameters["@UserName"].Value = Page.User.Identity.Name;
            e.Command.Parameters["@HRAcctCode"].Value = "      ";
            e.Command.Parameters["@ProfileOpened"].Value = DateTime.Now;
            e.Command.Parameters["@HourlyRate"].Value = 0.00;
            e.Command.Parameters["@JobDescription"].Value = "Enter Brief Desc.";
            e.Command.Parameters["@Status"].Value = "New";
            e.Command.Parameters["@WorkStudySpecificAmount"].Value = "None";
            e.Command.Parameters["@FirstJob"].Value = false;
          
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 19, 2007 11:44 AM

All replies

  • User844797000 posted

     Hi,

    please check whether the items you want to be selected is present in the radiobuttonlist?. Please check the data reder item for null value and if its null set the first item as checked...
     

    Thursday, October 18, 2007 7:41 AM
  • User1394791479 posted

     That sounds about right. How does one check the data reder item for null and then set first item checked?

    I am not using a datareader, I am using a sqldatasource, don't know if that makes a difference, but I assume so?


    TIA

    Dan

    Thursday, October 18, 2007 7:47 AM
  • User844797000 posted

     Hi,

    first you fill all the fileds like dropdown, radiobuttonlist, etc.,  then bind the sqldatasource to the fileds...

    Thursday, October 18, 2007 7:58 AM
  • User1394791479 posted

     let me ask it this way, is there a way I can set a default value in the radio buttons list?

    here is my code:

     

                    <EditItemTemplate>
                        <asp:RadioButtonList ID="RadioButtonList1" runat="server" BackColor="#C0FFFF" DataSourceID="SqlDataSourcerb"
                            DataTextField="WorkStudySpecificAmount" DataValueField="WorkStudySpecificAmount"
                            RepeatDirection="Horizontal" SelectedValue='<%# Bind("WorkStudySpecificAmount") %>'
                            Style="" Width="164px">
                            <asp:ListItem>All</asp:ListItem>
                            <asp:ListItem>None</asp:ListItem>
                            <asp:ListItem>Split</asp:ListItem>
                        </asp:RadioButtonList><asp:SqlDataSource ID="SqlDataSourcerb" runat="server" ConnectionString="<%$ ConnectionStrings:SAITS@SQLAuth %>"
                            SelectCommand="SELECT DISTINCT [WorkStudySpecificAmount] FROM [TimeMyProfile]&#13;&#10;where [workstudyspecificamount] is not null&#13;&#10; ">
                        </asp:SqlDataSource>
                    </EditItemTemplate>

    Thursday, October 18, 2007 8:02 AM
  • User1394791479 posted

     This may be more of a workaround, than the specific code I am looking for, but here goes:

    at the creation of a record (Function that starts a new profile) I am going to set default values at the sqldatasourceupdating event.

    its a bit of work, but looks like this:

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {//need to get max pronum, add 1 and set as new pronum, and return 1 if new
               
                String tempusername = Page.User.Identity.Name;
                Int32 pronum ;
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SAITS@SQLAuth"].ToString());
                connection.Open();
                SqlCommand command = new SqlCommand("Select max(ProNum) FROM TimeMyProfile WHERE UserName = '" + tempusername + "'", connection);
                string returnval = (command.ExecuteScalar().ToString()) ;
                    if (returnval.Length>0)//ie NOT first number
                        pronum = ((Int32)command.ExecuteScalar())+1;
                    else
                        pronum = 1;
                connection.Close();

            e.Command.Parameters["@ProNum"].Value = pronum;
            e.Command.Parameters["@UserName"].Value = Page.User.Identity.Name;
            e.Command.Parameters["@HRAcctCode"].Value = "      ";
            e.Command.Parameters["@ProfileOpened"].Value = DateTime.Now;
            e.Command.Parameters["@HourlyRate"].Value = 0.00;
            e.Command.Parameters["@JobDescription"].Value = "Enter Brief Desc.";
            e.Command.Parameters["@Status"].Value = "New";
            e.Command.Parameters["@WorkStudySpecificAmount"].Value = "None";
            e.Command.Parameters["@FirstJob"].Value = false;
          
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 19, 2007 11:44 AM