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

  • Question

  • User940894612 posted

    Hi

    I was wondering if someone could help me.  I am getting the error below

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentOutOfRangeException: 'CurrentStatusDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    The front end is as follows:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:FormView ID="FormView1" RenderOuterTable="False" runat="server" DefaultMode="Edit" >
            <EditItemTemplate>
                <div class="TestimonialDetailsWrapper">
                    <asp:Label ID="TestimonialIDLabel" runat="server" Text="Testimonial ID:"></asp:Label><br />
                    <asp:TextBox ID="TestimonialIDTextBox" Text=<%# Eval("ID") %> CssClass="textbox" ReadOnly="true" runat="server"></asp:TextBox>
                    <br /><br />
                    <asp:Label ID="FullNameLabel" runat="server" Text="Full Name:"></asp:Label><br />
                    <asp:TextBox ID="FullNameTextBox" Text=<%# Eval("Full Name") %> CssClass="textbox" ReadOnly="true" runat="server"></asp:TextBox>
                    <br /><br />
                    <asp:Label ID="CommentsLabel" runat="server" Text="Comments:   (MAX 400 Characters)"></asp:Label><br />
                    <asp:TextBox ID="CommentsTextBox" Text=<%# Eval("Comments") %> CssClass="multitextbox" runat="server" TextMode="MultiLine"></asp:TextBox>
                    <br /><br />
                    <asp:Label ID="ImprovementsLabel" runat="server" Text="Improvements/Suggestions:   (MAX 400 Characters)"></asp:Label><br />
                    <asp:TextBox ID="ImprovementsTextBox" Text=<%# Eval("Improvements") %> CssClass="multitextbox" TextMode="MultiLine" runat="server"></asp:TextBox>
                    <br /><br />
                    <asp:Label ID="RatingLabel" runat="server" Text="User Rating (Out of 10):"></asp:Label><br />
                    <asp:TextBox ID="RatingTextBox" Text=<%# Eval("Rating") %> CssClass="textbox" runat="server"></asp:TextBox>
                    <br /><br />
                    <asp:Label ID="CurrentStatusLabel" runat="server" Text="Current Status:"></asp:Label><br />
                    <asp:DropDownList ID="CurrentStatusDropDownList" SelectedValue='<%# Eval("Status Name") %>' runat="server">                   
                    </asp:DropDownList>
                    <br /><br />
                    <asp:Button ID="CancelButton" runat="server" Text="Cancel" CssClass="button" onclick="CancelButton_Click" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="UpdateButton" runat="server" Text="Update" CssClass="button" onclick="UpdateButton_Click" />
                </div>
            </EditItemTemplate>
        </asp:FormView>
        <br /><br />
    </asp:Content>

    What is happening is the code-behind: 

    TestimonialBLL testimonialbll = new TestimonialBLL();
        TestimonialCommon testimonialcommon = new TestimonialCommon();
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string s = Request.QueryString["TestimonialId"];
    
                if (String.IsNullOrEmpty(s))
                {
                    Response.Redirect("AllTestimonials.aspx");
                }
                else
                {
                    try
                    {
                        testimonialcommon.TestimonialID = int.Parse(s);
                        bool isvalidtestimonial = testimonialbll.IsValidTestimonial(testimonialcommon);
    
                        if (!isvalidtestimonial)
                        {
                            Response.Redirect("AllTestimonials.aspx");
                        }
                        {
                            DataTable dt = testimonialbll.GetTestimonialsByTestimonialID(testimonialcommon.TestimonialID);
                            
                            
                            FormView1.DataSource = dt;                        
                            FormView1.DataBind();
                            DropDownList currentstatus = (DropDownList)FormView1.FindControl("CurrentStatusDropDownList");
                            currentstatus.DataSource = Enum.GetNames(typeof(TestimonialStatus.CurrentTestimonialStatus));
                            currentstatus.DataBind();                        
                        }
                    }
                    catch (System.FormatException ex)
                    {
                        Response.Redirect("AllTestimonials.aspx");
                    }
                }
            }                    
        }

    The dropdown is being populated from this class here:

    public class TestimonialStatus
    {
    	public TestimonialStatus()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}
    
        public enum CurrentTestimonialStatus
        {
            New = 1,
            Approved = 2,
            Visible = 3,
            Deleted = 4
        }
    }

    The BLL method being called is:

    public DataTable GetTestimonialsByTestimonialID(int TestimonialID)
        {
            DataTable gettestimonialbytestimonialID = testimonialDAL.GetTestimonialsDetailsByTestimoialID(TestimonialID);
            return gettestimonialbytestimonialID;
        }

    which calls the DAL method here:

    public DataTable GetTestimonialsDetailsByTestimoialID(int TestimonialID)
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["DbConnection"].ToString();
            DbConnection con = new SqlConnection(connectionstring);
    
            DbCommand command = DataAccess.CreateCommand();
    
            command.CommandText = "Testimonial_GetTestimonialDetailsByTestimonialID";
            command.CommandType = CommandType.StoredProcedure;
    
            //Create a new parameter
            DbParameter param = command.CreateParameter();
            param.ParameterName = "@TestimonialID";
            param.Value = TestimonialID;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);
    
            DataTable table = new DataTable();
    
            try
            {
                con.Open();
                table = DataAccess.ExecuteSelectCommand(command);
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
            }
            finally
            {
                con.Close();
            }
    
            return table;
        }

    The SP being called is as follows:

    ALTER PROCEDURE [dbo].[Testimonial_GetTestimonialDetailsByTestimonialID]
    
    	@TestimonialID		NVARCHAR(50)
    
    AS
    
    SELECT	t.ID, ui.FirstName + ' ' + ui.LastName AS "Full Name", t.Comments AS "Comments", t.BookingID AS "BookingID", t.Improvements AS "Improvements", 
    			t.Rating, ts.Name AS "Status Name"
    			
        FROM	Testimonials t LEFT JOIN [UserInformation] ui    
    	ON		t.UserID = ui.UserID INNER JOIN TestimonialStatus ts
        ON		t.StatusID = ts.ID
    		
    WHERE	t.ID = @TestimonialID

    Any help would be greatly appreciated.

    Thanks

    Rob

    Wednesday, March 4, 2015 9:17 AM

Answers

  • User1728944201 posted

    I don't think you are understanding what I am saying.

    You Testimonial table has values in it that do not appear in your status table.   Since you are doing a left join and not an inner join, they are being included in your query.

    If you query the Testimonial for status id's that are null or not in (1,2,3,4)  I think you will find this is the case.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 1:29 PM
  • User1918509225 posted

    Hi Robbie81,

    The issue is that you dropdownlist contain selected value which didn't exist in your datasource control.

    please check your selected value and your datasource value.

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2015 1:35 AM

All replies

  • User1728944201 posted

    This is happening because the value of current status in your record is not in the list you have for you dropdown:  

    public enum CurrentTestimonialStatus
       
    {
           
    New = 1,
           
    Approved = 2,
           
    Visible = 3,
           
    Deleted = 4
       
    }
    }

    You either have a status that is not included in this list or you have a status of null.  You can fix this in the db by giving the status a default value of 1 and not allowing nulls or you can replace null values in your query: select field1,field2, isNull(currentStatus, 1) from .....

    Wednesday, March 4, 2015 11:03 AM
  • User940894612 posted

    Hi Joyce

    Thanks for the update.  Unfortunately all those values are in the database.  The status table is simply a table

    Id, Status Name

    1  New

    2  Approved

    3  Visible

    4  Deleted

    Wednesday, March 4, 2015 11:51 AM
  • User1728944201 posted

    I don't think you are understanding what I am saying.

    You Testimonial table has values in it that do not appear in your status table.   Since you are doing a left join and not an inner join, they are being included in your query.

    If you query the Testimonial for status id's that are null or not in (1,2,3,4)  I think you will find this is the case.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 1:29 PM
  • User1918509225 posted

    Hi Robbie81,

    The issue is that you dropdownlist contain selected value which didn't exist in your datasource control.

    please check your selected value and your datasource value.

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2015 1:35 AM
  • User940894612 posted

    Hi Joyce

    I am getting a bit confused as the page should only display 1 result i.e. where the TestimonialID = 1 - for this the value is New

    Thanks

    Rob

    Thursday, March 5, 2015 10:34 AM
  • User940894612 posted

    As an FYI I did the following:

    FormView1.DataSource = dt;
                            FormView1.DataBind();
                            DropDownList currentstatus = (DropDownList)FormView1.FindControl("CurrentStatusDropDownList");
                            currentstatus.DataSource = Enum.GetNames(typeof(TestimonialStatus.CurrentTestimonialStatus));
                            currentstatus.SelectedValue = "Approved";
                            currentstatus.DataBind();

    And the list automatically has Approved as the selected Value.  

    I hope this helps.

    Rob

    Thursday, March 5, 2015 10:45 AM