16,012,316 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View .NET questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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