Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on a Gridview Edit Delete operation which has a dropdownlist. When i click on edit , I GET this Error:



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


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

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: 'ddlDepartment' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value



But my dropwdownlist is populated. What the error mean. Ty
Posted
Updated 6-Feb-14 6:39am
v3
Comments
joginder-banger 6-Feb-14 11:33am    
Share your coding parts...for more clarification.
Member 10576807 6-Feb-14 13:10pm    
Hi Joginder,

Please find my Default.aspx.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGridView();
DropDownlistDepartment();
}
}

SqlConnection con = new SqlConnection(@"Data Source=SONY-VAIO\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True");
protected void btnSubmit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Editt(Name,Address,Department,Salary,Status,Active)values(@Name,@Address,@Department,@Salary,@Status,@Active)", con);


cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString());
cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString());
cmd.Parameters.AddWithValue("@Department", ddlDepartment.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@Salary", txtSalary.Text.ToString());
cmd.Parameters.AddWithValue("@Active", ChkActive.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@Status", rdStatus.SelectedValue.ToString());

cmd.ExecuteNonQuery();

con.Close();
FillGridView();



}

protected void FillGridView()
{
DataTable dt = new DataTable();


SqlCommand cmd = new SqlCommand("Select * from Editt", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView.DataSource = dt;
GridView.DataBind();
}


protected void DropDownlistDepartment()
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("Select Department_Name ,Department_ID from DepartmentTable1", con); //Don't forget con
SqlDataAdapter da = new SqlDataAdapter(cmd); ;

da.Fill(dt);
ddlDepartment.DataSource = dt;
ddlDepartment.DataTextField = "Department_Name";
ddlDepartment.DataValueField = "Department_ID";
ddlDepartment.DataBind();
ddlDepartment.Items.Insert(0, "-Select Department Name");
}

protected void Edit(object sender, GridViewEditEventArgs e) //GridViewEditEventArgs
{

GridView.EditIndex = e.NewEditIndex;
FillGridView();
}

protected void Update(object sender, GridViewUpdateEventArgs e) //GridViewUpdateEventArgs
{

int Id = Convert.ToInt32(GridView.DataKeys[e.RowIndex].Value.ToString()); //(GridView.DataKeys[e.RowIndex].Value.ToString());
SqlCommand cmd = new SqlCommand("Update Editt set Name=@Name , Address=@Address,Status=@Status,Active=@Active,Department=@Department,Salary=@Salary WHERE Id=@Id", con);
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString());
cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString());
cmd.Parameters.AddWithValue("@Department", ddlDepartment.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@Salary", txtSalary.Text.ToString());
cmd.Parameters.AddWithValue("@Active", ChkActive.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@Status", rdStatus.SelectedValue.ToString());

GridViewRow row = (GridViewRow)GridView.Rows[e.RowIndex]; //GridView.Rows[e.RowIndex]
RadioButtonList rd = (RadioButtonList)GridView.Rows[e.RowIndex].FindControl("rdStatus"); //GridView.Rows[e.RowIndex].FindControl("Name")
CheckBoxList ck = (CheckBoxList)GridView.Rows[e.RowIndex].FindControl("ChkActive");
TextBox tx1 = (TextBox)GridView.Rows[e.RowIndex].FindControl("txtName");
TextBox tx2 = (TextBox)GridView.Rows[e.RowIndex].Fin
ZurdoDev 6-Feb-14 14:57pm    
It means the value you tried to set in the dropdown does not exist in the dropdown.
Member 10576807 6-Feb-14 15:14pm    
No , when i click on edit button i get this error :

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


Does there is something wrong with my dropdownlist dcoding in edit or update

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900