Updating Repeater Control in Code-Behind

Experts, I'm using ASP.NET, VB, MS SQL Server, Repeater Control

I am trying to update multiple records in a repeater control.

This is for a content management page. I have a list of "page sections" for any given page. They are listed via the repeater control. I want to give the admin the ability to sort the page sections. To this end, I have a textbox that binds to the sectionOrder row of the database for each page section. This works fine.

I want the user to be able to change the numeric values of the sectionOrder textboxes and click one button to update all records.

I think I am close, but I am receiving an error "Conversion failed when converting the varchar value 'secID' to data type int."  Currently, the secID is pulling it's value from a HiddenField value that binds to the ID (primary key) of each repeater item.

Please take a look at my attached code. I have never tried to do an UPDATE using a repeater control (so that code may be questionable as well), much less trying to update multiple records at once. Thank you VERY much for taking a look at this.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim secID As Integer
        Dim secOrder As String
        Dim connStr As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
        Dim sql As String
        Dim conSql As SqlConnection
        conSql = New SqlConnection(connStr)
        Dim regUpdateCmd As New SqlCommand(sql, conSql)
        conSql.Open()

        For Each ri As RepeaterItem In Repeater1.Items
            secID = CType(ri.FindControl("Hiddenfield1"), HiddenField).Value
            secOrder = CType(ri.FindControl("TxtSectionOrder"), TextBox).ToString
            sql = "UPDATE mwcPages_Sections SET sectionOrder = 'secOrder' WHERE sectionID = 'secID'"

            regUpdateCmd.CommandText = sql
            regUpdateCmd.ExecuteNonQuery()

        Next
        conSql.Close()
        Repeater1.DataBind()
    End Sub

Open in new window

John0214Asked:
Who is Participating?
 
John0214Author Commented:
I figured this out, FINALLY.  I was missing a crucial part of code in updating the repeater control and then clearing the update parameters so the loop wouldn't error out. In addition, I wasn't using the update parameters correctly. If this helps anyone else, below is what I used to update multiple records with an ASP.NET repeater control.
<!-- BEGIN ASPX CODE -->

<asp:Repeater ID="Repeater1" runat="server" 
            DataSourceID="PageMgrDataListSqlDataSource">
            <ItemTemplate>
            <div id="sectionitem">
            <table cellpadding="0" cellspacing="0" border="0" width="800">
            <tr>
            <td>style="padding:0 0 0 10px"><asp:Label ID="sectionTitleLabel" runat="server" Text='<%# Eval("sectionTitle") %>' /></td>
            <td><asp:Label ID="sectionActiveLabel" runat="server" Text='<%# Eval("sectionActive") %>' /></td>
            <td>12/15/2009</td>
            <td>
                <asp:TextBox ID="TxtSectionOrder" runat="server" Text='<%# Bind("sectionOrder") %>' Width="30" ></asp:TextBox>
                <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("sectionID") %>' />
                </td>
            </tr>
            </table>  </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="Button1" runat="server" Text="Save Changes" OnClick="Button1_Click" />


<!-- CODE BEHIND -->

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim connStr As String = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
        Dim sql As String = "UPDATE mwcPages_Sections SET sectionOrder = @Order WHERE sectionID = @ID"
        Dim conSql As SqlConnection
        conSql = New SqlConnection(connStr)
        Dim regUpdateCmd As New SqlCommand(sql, conSql)
        conSql.Open()

        For Each ri As RepeaterItem In Repeater1.Items

            regUpdateCmd.Parameters.AddWithValue("@Order", DirectCast(ri.FindControl("TxtSectionOrder"), TextBox).Text)
            regUpdateCmd.Parameters.AddWithValue("@ID", DirectCast(ri.FindControl("Hiddenfield1"), HiddenField).Value)

            regUpdateCmd.CommandText = sql
            regUpdateCmd.ExecuteNonQuery()
            regUpdateCmd.Parameters.Clear()

        Next
        conSql.Close()
        Repeater1.DataBind()
    End Sub

Open in new window

0
 
rajeeshmcaCommented:
Hi john0214,

The problem is hiddenfield.Value does not return a int. so You will have to convert the hiddenfield value to int before assigning..
 you can do like

secID = Convert.ToInt32(DirectCast(ri.fincontrol("Hiddenfield1"), HiddenField))
0
 
John0214Author Commented:
rajeeshmca, thanks for the help!

I implemented the code you suggested and am now recieving the message:
Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.IConvertible'

Any ideas?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
rajeeshmcaCommented:
sorry i have missed iut the value property before converting.. here is the correct one

secID = Convert.ToInt32(DirectCast(ri.fincontrol("Hiddenfield1"), HiddenField).Value)
0
 
John0214Author Commented:
rajeeshmca, now I'm receiving the error message:
Conversion failed when converting the varchar value 'secID' to data type int.

I've been doing a lot of searching on the web, in addition to changing the control to a label hoping to have a different outcome, but so far nothing.
0
 
rajeeshmcaCommented:
Try something like

Just check iout..

whether the HiddenField has value in it...

and also if u can post both aspx and codebehind, i can have a go with that...
0
 
John0214Author Commented:
When I changed the HiddenField to a Label control, it was returning the sectionID number of the rows as expected, so it seems to be pulling the correct value.  Below is the code. Thanks again for all of your help!
<!-- BEGIN ASPX CODE -->

<asp:Repeater ID="Repeater1" runat="server" 
            DataSourceID="PageMgrDataListSqlDataSource">
            <ItemTemplate>
            <div id="sectionitem">
            <table cellpadding="0" cellspacing="0" border="0" width="800">
            <tr>
            <td>style="padding:0 0 0 10px"><asp:Label ID="sectionTitleLabel" runat="server" Text='<%# Eval("sectionTitle") %>' /></td>
            <td><asp:Label ID="sectionActiveLabel" runat="server" Text='<%# Eval("sectionActive") %>' /></td>
            <td>12/15/2009</td>
            <td>
                <asp:TextBox ID="TxtSectionOrder" runat="server" Text='<%# Bind("sectionOrder") %>' Width="30" ></asp:TextBox>
                <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("sectionID") %>' />
                </td>
            </tr>
            </table>  </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="Button1" runat="server" Text="Save Changes" OnClick="Button1_Click" />

<!-- BEGIN CODE BEHIND -->

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim secID As Integer
        Dim secOrder As String
        Dim connStr As String = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
        Dim sql As String
        Dim conSql As SqlConnection
        conSql = New SqlConnection(connStr)
        Dim regUpdateCmd As New SqlCommand(sql, conSql)
        conSql.Open()

        For Each ri As RepeaterItem In Repeater1.Items
            'If ri.ItemType = ListItemType.Item OrElse ri.ItemType = ListItemType.AlternatingItem Then
            secID = Convert.ToInt32(DirectCast(ri.FindControl("Hiddenfield1"), HiddenField).Value)
            secOrder = CType(ri.FindControl("TxtSectionOrder"), TextBox).ToString
            sql = "UPDATE mwcPages_Sections SET sectionOrder = 'secOrder' WHERE sectionID = 'secID'"

            regUpdateCmd.CommandText = sql
            regUpdateCmd.ExecuteNonQuery()

        Next
        conSql.Close()
        Repeater1.DataBind()
    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.