Click here to Skip to main content
15,895,011 members

Comments by Member 10576807 (Top 2 by date)

Member 10576807 6-Feb-14 15:14pm View    
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
Member 10576807 6-Feb-14 13:10pm View    
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