Loupi
Loupi

Reputation: 1112

Casting object to CheckBox in bulk updates

I was following this so far everything is fine with a textbox but when try to modify it to checkbox it gives an error: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.Web.UI.WebControls.CheckBox'.

Its working when I use TextBox but how can I make all the CheckBox editable in bulk updates?

Here's the sample code behind

private bool tableCopied = false;
        private DataTable originalDataTable;

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                if (!tableCopied)
                {
                    originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
                    ViewState["originalValueTable"] = originalDataTable;
                    tableCopied = true;
                }
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            originalDataTable = (DataTable)ViewState["originalValueTable"];

            foreach (GridViewRow r in GridView1.Rows)
                if (IsRowModified(r))
                {
                    GridView1.UpdateRow(r.RowIndex, false);
                }

            tableCopied = false;
            GridView1.DataBind();
        }

        protected bool IsRowModified(GridViewRow r)
        {
            int currentID;
            string currentreservedate;
            string currentisapproved;

            currentID = Convert.ToInt32(GridView1.DataKeys[0].Value);


            currentreservedate = ((TextBox)r.FindControl("reservedateTextBox")).Text;
            currentisapproved = ((CheckBox)r.FindControl("isapprovedCheckBox")).Text;

            DataRow row = originalDataTable.Select(String.Format("reservationid = {0}", currentID))[0];

            if (!currentreservedate.Equals(row["reservedate"].ToString()))
            if (!currentisapproved.Equals(row["isapproved"].ToString()))
            {
                return true;
            }

            return false;

        }
    }
}

Here's the aspx markup

Pending Reservation<br />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
            SelectCommand="SELECT dbo.BookReservation.reservationid, dbo.BookReservation.bookid, dbo.BookReservation.EmployeeID, dbo.BookReservation.reservedate, dbo.BookReservation.isapproved, dbo.BookReservation.reschedule, dbo.BookReservation.isdeleted, dbo.TblBooks.booktitle FROM dbo.BookReservation INNER JOIN dbo.TblBooks ON dbo.BookReservation.bookid = dbo.TblBooks.bookid" 
            DeleteCommand="DELETE FROM [BookReservation] WHERE [reservationid] = @reservationid" 
            InsertCommand="INSERT INTO [BookReservation] ([bookid], [EmployeeID], [reservedate], [isapproved], [reschedule], [isdeleted]) VALUES (@bookid, @EmployeeID, @reservedate, @isapproved, @reschedule, @isdeleted)" 

            UpdateCommand="UPDATE [BookReservation] SET [bookid] = @bookid, [EmployeeID] = @EmployeeID, [reservedate] = @reservedate, [isapproved] = @isapproved, [reschedule] = @reschedule, [isdeleted] = @isdeleted WHERE [reservationid] = @reservationid">
            <DeleteParameters>
                <asp:Parameter Name="reservationid" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="bookid" Type="Int64" />
                <asp:Parameter Name="EmployeeID" Type="String" />
                <asp:Parameter Name="reservedate" Type="DateTime" />
                <asp:Parameter Name="isapproved" Type="Boolean" />
                <asp:Parameter Name="reschedule" Type="Boolean" />
                <asp:Parameter Name="isdeleted" Type="Boolean" />
                <asp:Parameter Name="reservationid" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="bookid" Type="Int64" />
                <asp:Parameter Name="EmployeeID" Type="String" />
                <asp:Parameter Name="reservedate" Type="DateTime" />
                <asp:Parameter Name="isapproved" Type="Boolean" />
                <asp:Parameter Name="reschedule" Type="Boolean" />
                <asp:Parameter Name="isdeleted" Type="Boolean" />
            </InsertParameters>
        </asp:SqlDataSource>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true"
            DataKeyNames="reservationid, bookid, EmployeeID, reservedate " DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >

            <Columns>

                <asp:BoundField DataField="reservationid" HeaderText="reservationid" 
                    InsertVisible="False" ReadOnly="True" 
                    SortExpression="reservationid" Visible="False" />

                <asp:BoundField DataField="bookid" HeaderText="bookid" 
                    SortExpression="bookid" Visible="False" />

                <asp:BoundField DataField="booktitle" HeaderText="Title" 
                    SortExpression="booktitle" />

                <asp:BoundField DataField="EmployeeID" HeaderText="Reserved by" 
                    SortExpression="EmployeeID" />

                <%--<asp:BoundField DataField="reservedate" HeaderText="Date reserved"
                    SortExpression="reservedate" />--%>

                <asp:TemplateField HeaderText="Reserve Date" 
                    SortExpression="reservedate">
                    <EditItemTemplate>
                        <asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:CheckBoxField DataField="isapproved" HeaderText="Approved" 
                    SortExpression="isapproved" />--%>

                <asp:TemplateField HeaderText="Apprvoed" 
                    SortExpression="isapproved">
                    <EditItemTemplate>
                        <asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
                        </asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:CheckBoxField DataField="reschedule" HeaderText="Reschedule" 
                    SortExpression="reschedule" />

                <asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted" 
                    SortExpression="isdeleted" />

            </Columns>

        </asp:GridView>
        <br />
        <br />

    <asp:Button ID="UpdateButton" runat="server" Text="Update" OnClick="UpdateButton_Click" />

        <br />

Help would be much appreciated! Thanks in advance!

Upvotes: 1

Views: 1142

Answers (1)

Virmundi
Virmundi

Reputation: 2631

I think that to answer this question, the contents of the aspx file are required, but I'll try to help. More than likely the element in your form, isapprovedCheckBox, is still set to the the tag asp:Text. As a result the system can't convert the textbox to a checkbox.

Could you include your aspx content as well?

Upvotes: 1

Related Questions