Facing this issue when i am updating department in Employee Detail table using dropdownlist on event of Updating.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
On Gridview_RowUpdating , on following line getting this erro
string NamDept =(GridView.Rows[e.RowIndex].FindControl("ddlDepartment") as DropDownList).SelectedItem.Value;
SqlConnection myConn = default(SqlConnection);
SqlCommand sqComm = default(SqlCommand);
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlDataAdapter SqlAdapter;
protected void Page_Load(object sender, EventArgs e)
{
if (setConn())
{
PopulateDS(); // FILL DATASET WITH MASTER DATA.
ShowEmpDetails(); // SHOW EMPLOYEE DETAILS IN THE GRIDVIEW.
}
}
private bool setConn()
{
// SET DATABASE CONNECTION.
try
{
myConn = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement");
myConn.Open();
sqComm = new SqlCommand();
sqComm.Connection = myConn;
}
catch (Exception ex) { return false; }
return true;
}
// CANCEL ROW EDITING.
protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView.EditIndex = -1;
ShowEmpDetails();
}
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
// BIND THE "DROPDOWNLIST" WITH THE DATASET FILLED WITH "QUALIFICATION" DETAILS.
DropDownList ddldept = new DropDownList();
ddldept = (DropDownList)e.Row.FindControl("ddlDepartment");
if (ddldept != null)
{
ddldept.DataSource = ds.Tables["Department"];
ddldept.DataTextField = ds.Tables["Department"].Columns["NamDept"].ColumnName.ToString();
ddldept.DataValueField = ds.Tables["Department"].Columns["IDdept"].ColumnName.ToString();
ddldept.DataBind();
// ASSIGN THE SELECTED ROW VALUE ("QUALIFICATION CODE") TO THE DROPDOWNLIST SELECTED VALUE.
((DropDownList)e.Row.FindControl("ddlDepartment")).SelectedValue = DataBinder.Eval(e.Row.DataItem, "IDdept").ToString();
}
}
}
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditIndex;
ShowEmpDetails();
}
// MASTER DATA IN A DATASET.
private void PopulateDS()
{
ds.Clear();
SqlAdapter = new System.Data.SqlClient.SqlDataAdapter("select NamDept,IDdept from Department", myConn);
SqlAdapter.Fill(ds, "Department");
SqlAdapter.Dispose();
}
private void ShowEmpDetails()
{
string sQuery = "SELECT EmployeeDetails.EmpID ,EmployeeDetails.EmpName, EmployeeDetails.EmpContact, Department.NamDept , SubDepartment.NamSubDept ,EmployeeDetails.Status,EmployeeDetails.JoinDate,EmployeeDetails.LeftDate,EmployeeDetails.Empcur,EmployeeDetails.Lentitled,EmployeeDetails.PL,EmployeeDetails.Address,EmployeeDetails.OTEntitled,EmployeeDetails.LTime,EmployeeDetails.OTRate,EmployeeDetails.Dhour,Designation.NamDesig FROM EmployeeDetails INNER JOIN Department ON Department.IDDept = EmployeeDetails.IDDept INNER JOIN SubDepartment ON SubDepartment.IDSubDept = EmployeeDetails.IDSubDept INNER JOIN Designation ON Designation.IDDesig = EmployeeDetails.IDDesig where empcur = 'join'";
SqlDataReader sdrEmp = GetDataReader(sQuery);
try
{
if (sdrEmp.HasRows)
{
DataTable dt = new DataTable();
dt.Load(sdrEmp);
GridView.DataSource = dt;
GridView.DataBind(); // BIND DATABASE TABLE WITH THE GRIDVIEW.
}
}
catch (Exception ex) { }
finally
{
sdrEmp.Close();
sdrEmp = null;
}
}
private SqlDataReader GetDataReader(string sQuery)
{
SqlDataReader functionReturnValue = default(SqlDataReader);
sqComm.CommandText = sQuery;
sqComm.ExecuteNonQuery();
functionReturnValue = sqComm.ExecuteReader();
sqComm.Dispose();
return functionReturnValue;
}
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string NamDept = (GridView.Rows[e.RowIndex].FindControl("ddlDepartment") as DropDownList).SelectedItem.Value;
}