Answered by:
Update multiple records at once in gridview

Question
-
User1028962535 posted
Hello, I have a grid view with a text box and a checkbox. I would like to be able to update several records at once,and am trying to amend some code I found. However the checkbox code is not working, probably because of syntax issues, I think. I get this message
"Input string was in incorrect format" on line 38. I tried changing it to boolean with no luck. Can someone take a look at the code to see if it can be amended to update the check boxes, and where I have been going wrong.
Thank you
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="OPcollection.aspx.vb" Inherits="OPcollection" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body><form id="form1" runat="server">
<div><asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Audit %>"
DeleteCommand="DELETE FROM [Main] WHERE [ScriptID] = @ScriptID"
InsertCommand="INSERT INTO [Main] ([TicketNo], [CollectedMM]) VALUES (@TicketNo, @CollectedMM)"
SelectCommand="SELECT ScriptID,TicketNo, CollectedMM FROM dbo.Main WHERE (Location = 'Mortimer Mkt') AND (CollectedMM IS NULL) and (DateChecked is Not Null) and dateadded BETWEEN CAST(GETDATE() AS DATE) AND DATEADD(DAY, 3, CAST(GETDATE() AS DATE))order by DateAdded desc"
UpdateCommand="UPDATE [Main] SET [TicketNo] = @TicketNo, [CollectedMM] = @CollectedMM WHERE [ScriptID] = @ScriptID">
<DeleteParameters>
<asp:Parameter Name="ScriptID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CollectedMM" Type="Boolean" />
<asp:Parameter Name="TicketNo" Type="Int32" />
<asp:Parameter Name="ScriptID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CollectedMM" Type="Boolean" />
<asp:Parameter Name="TicketNo" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="ScriptID"
DataSourceID="SqlDataSource1" EnableModelValidation="True" Font-Names="Calibri">
<Columns>
<asp:BoundField DataField="ScriptID" HeaderText="ID"
InsertVisible="False" ReadOnly="True" SortExpression="ScriptID" /><asp:TemplateField HeaderText="Ticket No" SortExpression="TicketNo">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TicketNo")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TicketNoTextBox" runat="server" MaxLength="20"
Text='<%# Bind("TicketNo")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField><asp:TemplateField HeaderText="Collected" SortExpression="CollectedMM">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("CollectedMM") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked= '<%# IIf(Eval("CollectedMM") Is DBNull.Value, "False", Eval("CollectedMM")) %>'/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField></Columns>
</asp:GridView>
<asp:Button ID="UpdateButton" runat="server" Height="26px" Text="Update" /></div>
</form>the code:
Partial Class OPcollection
Inherits System.Web.UI.PagePrivate tableCopied As Boolean = False
Private originalDataTable As System.Data.DataTableProtected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If Not tableCopied Then
originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
ViewState("originalValuesDataTable") = originalDataTable
tableCopied = True
End If
End If
End SubProtected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UpdateButton.Click
originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)For Each r As GridViewRow In GridView1.Rows
If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, False)
Next' Rebind the Grid to repopulate the original values table.
tableCopied = False
GridView1.DataBind()
End SubProtected Function IsRowModified(ByVal r As GridViewRow) As Boolean
Dim currentID As Integer
Dim currentTicketNo As String
Dim currentCollected As BooleancurrentID = Convert.ToInt32(GridView1.DataKeys(r.RowIndex).Value)
currentTicketNo = CType(r.FindControl("TicketNoTextBox"), TextBox).Text
currentCollected = CType(r.FindControl("Checkbox1"), CheckBox).TextDim row As System.Data.DataRow = _
originalDataTable.Select(String.Format("ScriptID = {0}", currentID))(0)If Not currentTicketNo.Equals(row("TicketNo").ToString()) Then Return True
If Not currentCollected.Equals(row("Checkbox1").ToString()) Then Return True - I think the problem is this lineReturn False
End Function
End ClassTuesday, September 27, 2016 2:34 PM
Answers
-
User1028962535 posted
Thanks for your help, I am not sure why, i changed the code to (see below)...and it worked!
currentCollected = CType(r.FindControl("Checkbox1"), CheckBox).Checked If Not currentCollected.Equals(row("Checkbox1").GetType())Then Return True
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 3, 2016 8:31 AM
All replies
-
User-359936451 posted
Are you trying to update all the rows at once or only selected rows?
That is to say, check all check boxes at once, or uncheck all text boxes at once?
anything other than that is a single row update as each click event will fire a separate event.
Tuesday, September 27, 2016 2:59 PM -
User1028962535 posted
Which ever row is modified will be updated. There is not edit button, its like gridview is already in edit mode.
See the original code here
Tuesday, September 27, 2016 3:57 PM -
User-359936451 posted
But you did not share your requirement. That project may be over complicating what you are trying to do.
Are you looking to update multiple rows in your database, based on the single update of the row being modified?
Tuesday, September 27, 2016 5:16 PM -
User1028962535 posted
Yes, I am looking to update multiple rows, by clicking a single update button once.
Tuesday, September 27, 2016 6:21 PM -
User-707554951 posted
Hi Dan5,
Dim currentCollected As BooleancurrentCollected = CType(r.FindControl("Checkbox1"), CheckBox).TextIf Not currentCollected.Equals(row("Checkbox1").ToString()) Then Return TrForm your error message and code above, I find that you define currentCollected variable as Boolean type, However, you assign a value with text type to the variable. So, the error message arise. I suggest you could replace the last two code line above with the code below:
currentCollected = CType(r.FindControl("Checkbox1"), CheckBox).Checked If Not currentCollected.Equals(row("Checkbox1").Checked) Then Return True
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards
CathyWednesday, September 28, 2016 2:40 AM -
User1028962535 posted
Thanks for your help, I am not sure why, i changed the code to (see below)...and it worked!
currentCollected = CType(r.FindControl("Checkbox1"), CheckBox).Checked If Not currentCollected.Equals(row("Checkbox1").GetType())Then Return True
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 3, 2016 8:31 AM