This is a migrated thread and some comments may be shown as answers.
foreach loop not looping properly
1 Answer 33 Views
This is a migrated thread and some comments may be shown as answers.
Jerald
Top achievements
Rank 1
Jerald asked on 30 Jul 2018, 06:50 PM

I have a Grid control with one column of checkboxes.  When I click on the checkbox in the header, select a value in a dropdown list, and then click a button two sql commands should run.  However the sql commands are only executing for the top selected row in the Grid.  I need it to execute for all checked rows.  What's wrong with my code?  When I click on the checkbox in the header the checkboxes on each row within the visible Grid are selected as expected.

Grid Markup:

<telerik:RadGrid ID="rgUnassnd" runat="server" DataSourceID="sdsRgUnassnd" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True" Skin="Default" AllowMultiRowSelection="true">
        <ClientSettings AllowDragToGroup="True" AllowColumnsReorder="True" ReorderColumnsOnClient="True">
            <Selecting AllowRowSelect="true" />
        </ClientSettings>
        <MasterTableView DataSourceID="sdsRgUnassnd" DataKeyNames="assnmtIdPk" CommandItemDisplay="Top">
            <CommandItemSettings ShowRefreshButton="true" ShowAddNewRecordButton="false" />
            <Columns>
                <telerik:GridTemplateColumn UniqueName="CheckTemp">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkI" runat="server" />
                    </ItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkH" runat="server" AutoPostBack="true" OnCheckedChanged="chkH_CheckedChanged"/>
                    </HeaderTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="assnmtIdPk" ReadOnly="True" HeaderText="assnmtIdPk" SortExpression="assnmtIdPk" UniqueName="assnmtIdPk" DataType="System.Int32" FilterControlAltText="Filter assnmtIdPk column"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="clmNo" HeaderText="Claim" SortExpression="clmNo" UniqueName="clmNo" FilterControlAltText="Filter clmNo column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="assCrtdDt" ReadOnly="true" HeaderText="Date Received" SortExpression="assCrtdDt" UniqueName="assnmtCrtdDt" DataType="System.DateTime" FilterControlAltText="Filter assnmtCrtdDt column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="clientNm" HeaderText="Client" SortExpression="clientNm" UniqueName="clientNm" FilterControlAltText="Filter clientNm column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="carr" HeaderText="Carrier" SortExpression="carr" UniqueName="carr" FilterControlAltText="Filter carr column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="riskAddCity" HeaderText="Risk City" SortExpression="riskAddCity" UniqueName="riskAddCity" FilterControlAltText="Filter riskAddCity column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="provNm" HeaderText="Province" SortExpression="provNm" UniqueName="provNm" FilterControlAltText="Filter provNm column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="riskZip" HeaderText="Zip Code" SortExpression="riskZip" UniqueName="riskZip" FilterControlAltText="Filter riskZip column" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

 

Here's the code behind:

protected void assignUsers_Click(object sender, EventArgs e)
        {
            string tascnxn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
 
            GridHeaderItem hItem = rgUnassnd.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
 
            if (hItem != null)
            {
                foreach (GridDataItem item in rgUnassnd.MasterTableView.Items)
                {
                    CheckBox ChkChange = item.FindControl("chkI") as CheckBox;
                    if (ChkChange.Checked)
                    {
                        string assidpk = item.GetDataKeyValue("assnmtIdPk").ToString();
 
                        using (SqlConnection tasconn = new SqlConnection(tascnxn))
                        {
                            SqlCommand repAssnmtCmd = new SqlCommand("INSERT INTO repAssnmts (raCrtdBy, raCrtdDt, assnmtIdFk, repId) VALUES (LTRIM(RTRIM(@raCrtdBy)), LTRIM(RTRIM(@raCrtdDt)), LTRIM(RTRIM(@assnmtIdFk)), LTRIM(RTRIM(@repId)))", tasconn);
                            {
                                repAssnmtCmd.Parameters.AddWithValue("@assnmtIdFk", assidpk); //need to get the id of the checked row
                                repAssnmtCmd.Parameters.AddWithValue("@raCrtdBy", HttpContext.Current.User.Identity.GetUserId());
                                repAssnmtCmd.Parameters.AddWithValue("@raCrtdDt", DateTime.Now.ToString());
                                repAssnmtCmd.Parameters.AddWithValue("@repId", userList.SelectedValue);
 
                                tasconn.Open();
                                repAssnmtCmd.ExecuteNonQuery();
                            }
 
                            SqlCommand assnmtStatCmd = new SqlCommand("INSERT INTO assnmtStats (asCrtdBy, asCrtdDt, assnmtIdFk, aStatId) VALUES (LTRIM(RTRIM(@asCrtdBy)), LTRIM(RTRIM(@asCrtdDt)), LTRIM(RTRIM(@assnmtIdFk)), LTRIM(RTRIM(@aStatId)))", tasconn);
                            {
                                assnmtStatCmd.Parameters.AddWithValue("@assnmtIdFk", assidpk); //need to get the id of the checked row
                                assnmtStatCmd.Parameters.AddWithValue("@asCrtdBy", HttpContext.Current.User.Identity.GetUserId());
                                assnmtStatCmd.Parameters.AddWithValue("@asCrtdDt", DateTime.Now.ToString());
                                assnmtStatCmd.Parameters.AddWithValue("@aStatId", 2);
 
                                assnmtStatCmd.ExecuteNonQuery();
                                tasconn.Close();
 
                                if (IsPostBack)
                                {
                                    lblBtnAssignUsers.ForeColor = System.Drawing.ColorTranslator.FromHtml("#0AA10A");
                                    lblBtnAssignUsers.Text = "The file(s) were successfully assigned.";
                                    Response.Redirect(Request.RawUrl);
                                }
                                else
                                {
                                    lblBtnAssignUsers.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CA2823");
                                    lblBtnAssignUsers.Text = "The assignment(s) failed.  Please try again.";
                                }
                            }
                        }
                    }
                }
            }
        }

 

Here's the CheckChanged code:

protected void chkH_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridDataItem item in rgUnassnd.MasterTableView.Items)
            {
                CheckBox chkbx = (CheckBox)item["CheckTemp"].FindControl("chkI");
                chkbx.Checked = !chkbx.Checked;
            }
        }

 

1 Answer, 1 is accepted

Sort by
0
Jerald
Top achievements
Rank 1
answered on 02 Aug 2018, 03:03 PM
Please disregard this post.  I forgot to delete the Response.Redirect.  It works as expected now.
Tags
Grid
Asked by
Jerald
Top achievements
Rank 1
Answers by
Jerald
Top achievements
Rank 1
Share this question
or