Jamie
Jamie

Reputation: 1679

Textbox Won't Update Database

I can't get my textbox to update the database. The changes look like they are saved (they are saved on the page) and the modal pop up goes away, but the text that I tried to change in the database stays the same.

Protected Sub SubmitEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim myControl As Control = FindControl("txtData")
    If (Not myControl Is Nothing) Then
        Dim UpdateSql As String = "UPDATE Picklist SET (Data) = @Data WHERE PicklistID = @PicklistID"
        Using cn As New SqlConnection
        (System.Configuration.ConfigurationManager.ConnectionStrings
        ("LocalSqlServer").ConnectionString)
            Using sqlcmd As New SqlCommand(UpdateSql, cn)
                sqlcmd.Parameters.Add(New SqlParameter("@Data", myControl))
                cn.Open()
                sqlcmd.ExecuteNonQuery()
            End Using
        End Using
    Else
    End If
End Sub


 <asp:TabPanel ID="tab2"  runat="server" HeaderText="Descriptions">
<HeaderTemplate>Descriptions</HeaderTemplate>
    <ContentTemplate>
        <ul class="info">
        <asp:ListView ID="lvDescriptions" runat="server" 
          DataSourceID="dsAdminMarketingDescriptions" DataKeyNames="MarketingID">

        <ItemTemplate>
            <li class="item">
                <asp:LinkButton ID="ViewDescriptionButton" runat="server">
                <%# Eval("Title")%>
                </asp:LinkButton>

               <asp:ImageButton ID="DeleteDescriptionButton" runat="server"  
                Style="float:right;" AlternateText="" 
                ImageUrl="../../images/delete.png" CommandName="Delete" 
                OnClientClick="return confirm('Are you sure you want to delete this 
                description?')" />

                <asp:Panel ID="ViewDescriptionPanel" runat="server"
                  CssClass="DescModalPopup">                                   
                <div class="PopupHeader">View Description -- <%#Eval("Title") %>
                <asp:ImageButton ID="CancelDescriptionButton" runat="server" 
                  ImageUrl="../../images/cancel.png" AlternateText="" 
                  Style="float:right;"/>
                 <asp:ImageButton ID="EditDescriptionButton" runat="server" 
                   ImageUrl="../../images/edit.png" AlternateText="" 
                   Style="float:right;" CommandName="Edit" AutoPostBack="false" />
                </div>

                    <asp:Label ID="Description" runat="server" style="padding:2px;">
                    <%# Eval("Data")%>
                    </asp:Label>
                </asp:Panel> 

                <asp:ModalPopupExtender ID="ViewDescriptionModal" runat="server" 
                  BackgroundCssClass="modalBackground" DropShadow="false" 
                  DynamicServicePath="" Enabled="true" 
                  PopupControlID="ViewDescriptionPanel" 
                  TargetControlID="ViewDescriptionButton" 
                  CancelControlID="CancelDescriptionButton"></asp:ModalPopupExtender> 


                <asp:Panel ID="EditDescriptionPanel" runat="server" 
                  CssClass="DescModalPopup">

                <div class="PopupHeader">Edit Description -- <%# Eval("Title")%>
                <asp:ImageButton ID="Cancel" runat="server" 
                  ImageUrl="../../images/cancel.png" AlternateText="" 
                  Style="float:right;"/>
                </div>

                    <asp:TextBox ID="txtData" runat="server" TextMode="MultiLine" 
                     Text='<%# Eval("Data")%>'>
                    </asp:TextBox><br />
                <asp:Button ID="SubmitEdit" runat="server" Text="Submit" />
                <asp:Button ID="CancelEdit" runat="server" Text="Cancel" />
                </asp:Panel>

                <asp:ModalPopupExtender ID="EditDescriptionModal" runat="server" 
                BackgroundCssClass="modalBackground" DropShadow="false" 
                DynamicServicePath="" Enabled="true" 
                PopupControlID="EditDescriptionPanel" 
                TargetControlID="EditDescriptionButton">
                </asp:ModalPopupExtender>            
            </li>
        </ItemTemplate>

UPDATE: I tried using a try catch to get an error message, but it didn't work.

Protected Sub SubmitEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim myControl As Control = FindControl("txtData")
    If (Not myControl Is Nothing) Then
        Try
            Dim UpdateSql As String = "UPDATE Picklist 
                                       SET (Data) = @Data 
                                       WHERE Title = @Title"
            Using cn As New SqlConnection
             (System.Configuration.ConfigurationManager.ConnectionStrings
             ("LocalSqlServer").ConnectionString)
                Using sqlcmd As New SqlCommand(UpdateSql, cn)
                    sqlcmd.Parameters.Add(New SqlParameter("@Data", 
                     DirectCast(myControl, TextBox).Text))
                    cn.Open()
                    sqlcmd.ExecuteNonQuery()
                End Using
                cn.Close()
            End Using
        Catch ex As Exception
            Response.Write(ex.Message())
        End Try
    Else
    End If
    Response.Redirect(Request.RawUrl)
End Sub

UPDATE: Figured it out. Here is the code behind, all other code stayed the same

Protected Sub SubmitEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
    For Each item As ListViewDataItem In lvDescriptions.Items
        Dim txtData As TextBox = DirectCast(item.FindControl("txtData"), TextBox)
        Dim ltlTitle As Literal = DirectCast(item.FindControl("ltlTitle"), Literal)
        Dim UpdateSql As String = "UPDATE Picklist 
                                   SET Data = @Data 
                                   WHERE Title = @Title"
        Using cn As New SqlConnection
        (System.Configuration.ConfigurationManager.ConnectionStrings
        ("LocalSqlServer").ConnectionString)
            Using sqlcmd As New SqlCommand(UpdateSql, cn)
                sqlcmd.Parameters.Add(New SqlParameter("@Data", txtData.Text))
                sqlcmd.Parameters.Add(New SqlParameter("@Title", ltlTitle.Text))

                cn.Open()
                sqlcmd.ExecuteNonQuery()

            End Using
            cn.Close()
        End Using
    Next
    Response.Redirect(Request.RawUrl)
End Sub

Upvotes: 0

Views: 1085

Answers (1)

competent_tech
competent_tech

Reputation: 44931

You need to change the line:

sqlcmd.Parameters.Add(New SqlParameter("@Data", myControl))

to

sqlcmd.Parameters.Add(New SqlParameter("@Data", myControl.Text))

Update:

Since the textbox is in a listview itemtemplate, you also need to change how it is discovered by looking at the currently selected list item. To do this, change the following line:

Dim myControl As Control = FindControl("txtData")

to:

Dim myControl As TextBox = DirectCast(lvDescriptions.Items(lvDescriptions.SelectedIndex).FindControl("txtData"), TextBox)

I have also updated the sqlParameters assignment to remove the DirectCast since we are now declaring the control as a TextBox (but it still needs to be updated to use the Text property from the original code).

Upvotes: 3

Related Questions