Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
   protected void gvTimeSheet_DataBound(object sender, EventArgs e)
    
    {
        DataTable dt = new DataTable();
        int intUserID = TypeConvert.IsNullInt32(ddlUsers.SelectedValue, 0);
        DateTime fromDate = Convert.ToDateTime(UCFromDate.selectedDate);
        DateTime toDate = Convert.ToDateTime(UCToDate.selectedDate);
        string strQuery = "EXEC SPRptUserTimeSheet_AP @User=" + intUserID + ",@dateFrom='" + fromDate + "',@dateTo='" + toDate + "'";
        dt = DataHelper.ExecuteQuery(strQuery);
        dt.Columns.Add("Booked Hrs");
        dt.Columns.Add("Time Zone");

        

        foreach (DataRow row in dt.Rows)
        {
            string userName = row[0].ToString();
            string bookedHr = row[6].ToString();
            string timezone = row[7].ToString();

            if (userName == string.Empty)
            {
                row[0] = ddlUsers.SelectedItem.Text;
            }

            if (bookedHr == string.Empty)
            {
                row["Booked Hrs"] = 0.0;
            }
            if (bookedHr != string.Empty)
            {
                row["Booked Hrs"] = objCommon.fnGetEffortInHrMinFormat(Convert.ToInt32(bookedHr));
            }

            if (timezone == "0")
            {
                row["Time Zone"] = "Offshore";

            }
            if (timezone == "1")
            {
                row["Time Zone"] = "Onsite";
            }

        }
        ViewState["dtExport"] = dt;
    }
}

What I have tried:

protected void gvTimeSheet_DataBound(object sender, EventArgs e)
    
    {
        DataTable dt = new DataTable();
        int intUserID = TypeConvert.IsNullInt32(ddlUsers.SelectedValue, 0);
        DateTime fromDate = Convert.ToDateTime(UCFromDate.selectedDate);
        DateTime toDate = Convert.ToDateTime(UCToDate.selectedDate);
        string strQuery = "EXEC SPRptUserTimeSheet_AP @User=" + intUserID + ",@dateFrom='" + fromDate + "',@dateTo='" + toDate + "'";
        dt = DataHelper.ExecuteQuery(strQuery);
        dt.Columns.Add("Booked Hrs");
        dt.Columns.Add("Time Zone");

        

        foreach (DataRow row in dt.Rows)
        {
            string userName = row[0].ToString();
            string bookedHr = row[6].ToString();
            string timezone = row[7].ToString();

            if (userName == string.Empty)
            {
                row[0] = ddlUsers.SelectedItem.Text;
            }

            if (bookedHr == string.Empty)
            {
                row["Booked Hrs"] = 0.0;
            }
            if (bookedHr != string.Empty)
            {
                row["Booked Hrs"] = objCommon.fnGetEffortInHrMinFormat(Convert.ToInt32(bookedHr));
            }

            if (timezone == "0")
            {
                row["Time Zone"] = "Offshore";

            }
            if (timezone == "1")
            {
                row["Time Zone"] = "Onsite";
            }

        }
        ViewState["dtExport"] = dt;
    }
}
Posted

1 solution

Don't change the enumerable you are looping through. It will always break the loop.

Instead add the required rows to a new datatable and use that.

There are options to clone the column structure from a datatable to a new one
 
Share this answer
 

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