[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Updating Repeater Control in Code-Behind

Posted on 2009-12-20
7
Medium Priority
?
934 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:John0214
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26093704
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
 

Author Comment

by:John0214
ID: 26101743
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
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26102341
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:John0214
ID: 26109826
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
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26110199
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
 

Author Comment

by:John0214
ID: 26119812
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
 

Accepted Solution

by:
John0214 earned 0 total points
ID: 26356167
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question