Hi frnds,
I have an array called"List" with 1 item and a gridview "grdWorkingDays" with two columns.
In the First column, I want to display the working days dynamically from the arraylist and in the second column I need to display a dropdown with CL/PL/Halfday(Which has values from a database table and is already written in a method) against each date of array list.
The code is:
int NoOfWorkingDays = List.Count;
for (int j = 0; j < NoOfWorkingDays; j++){
String Date = ((Label)grdWorkingDays.Rows[j].Cells[0].FindControl("lblLeaveType")).Text;
Date = List[j].ToString();
DropDownList ddl = (DropDownList)grdWorkingDays.Rows[j].Cells[1].FindControl("cboLeaveType");
FillLeaveTypeDropdown(ddl);}
public void FillLeaveTypeDropdown(DropDownList ddl) {
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
SqlCommand cmd = new SqlCommand(); SqlDataReader sdr;
cmd.Connection=con; con.Open();
cmd.CommandText="select leaveid,type from leave_type";
sdr = cmd.ExecuteReader(); while (sdr.Read())
{ ListItem lst = new ListItem();
lst.Value = sdr[0].ToString();
lst.Text = sdr[1].ToString();
ddl.Items.Add(lst); } }
When I run this, I am getting error as:
"Index was out of range. Must be non-negative and less than the size of the collection." pointing to the 1st row of for loop.
I am struggling with this for past few days and count find what the error is.
FYI, the array count is being calculated correctly as "1" .