Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 文本框赢得';更新数据库_Asp.net_Vb.net - Fatal编程技术网

Asp.net 文本框赢得';更新数据库

Asp.net 文本框赢得';更新数据库,asp.net,vb.net,Asp.net,Vb.net,我无法让我的文本框更新数据库。更改看起来像是保存了(保存在页面上),模式弹出窗口消失了,但我试图在数据库中更改的文本保持不变 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 Updat

我无法让我的文本框更新数据库。更改看起来像是保存了(保存在页面上),模式弹出窗口消失了,但我试图在数据库中更改的文本保持不变

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>
更新:找到了答案。这是隐藏的代码,所有其他代码保持不变

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

您需要更改行:

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

更新:

由于文本框位于listview项模板中,因此还需要通过查看当前选定的列表项来更改查找文本框的方式。为此,请更改以下行:

Dim myControl As Control = FindControl("txtData")
致:


我还更新了sqlParameters分配以删除DirectCast,因为我们现在将控件声明为文本框(但仍需要更新以使用原始代码中的Text属性)。

您需要更改行:

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

更新:

由于文本框位于listview项模板中,因此还需要通过查看当前选定的列表项来更改查找文本框的方式。为此,请更改以下行:

Dim myControl As Control = FindControl("txtData")
致:


我还更新了sqlParameters分配以删除DirectCast,因为我们现在将控件声明为文本框(但仍需要更新以使用原始代码中的Text属性)。

我没有向右滚动以查看所有更新文本;您缺少@PickListID的参数。我怀疑正在抛出一个您没有看到的异常;我强烈建议将click事件的内容包装在try/catch语句中,然后在catch中设置一个断点,以查看生成了什么错误并解决该错误。是的,我还以为是PicklistID,所以我将其更改为@Title,因为这是模态所基于的另一个东西,但我仍然无法更新数据库。检查上面的编辑,我不确定我是否正确使用了try-catch。问题是您仍然没有在任何地方设置@Title参数:sqlcmd.Parameters.Add(新的SqlParameter(“@Title”,DirectCast(myTitleControl,TextBox.Text))。您使用的try-catch是正确的,但是您永远不会看到问题,因为最后一条语句(response.redirect)总是被执行的。您可以为测试目的向页面添加标签并将消息存储在标签中,也可以在catch语句中设置断点并检查在调试器中引发的异常。我遍历了代码,代码在If语句处停止,因此我删除了它,但不确定它为什么会出现。然后它在@Data parameters行停止,并说
对象引用未设置为对象的实例。
这意味着它找不到我的编辑文本框?是的,这正是它的意思,我应该更仔细地查看您的HTML声明以实现这一点。查看更新后的答案,了解要做的其他更改。我没有向右滚动查看所有更新文本;您缺少@PickListID的参数。我怀疑正在抛出一个您没有看到的异常;我强烈建议将click事件的内容包装在try/catch语句中,然后在catch中设置一个断点,以查看生成了什么错误并解决该错误。是的,我还以为是PicklistID,所以我将其更改为@Title,因为这是模态所基于的另一个东西,但我仍然无法更新数据库。检查上面的编辑,我不确定我是否正确使用了try-catch。问题是您仍然没有在任何地方设置@Title参数:sqlcmd.Parameters.Add(新的SqlParameter(“@Title”,DirectCast(myTitleControl,TextBox.Text))。您使用的try-catch是正确的,但是您永远不会看到问题,因为最后一条语句(response.redirect)总是被执行的。您可以为测试目的向页面添加标签并将消息存储在标签中,也可以在catch语句中设置断点并检查在调试器中引发的异常。我遍历了代码,代码在If语句处停止,因此我删除了它,但不确定它为什么会出现。然后它在@Data parameters行停止,并说
对象引用未设置为对象的实例。
这意味着它找不到我的编辑文本框?是的,这正是它的意思,我应该更仔细地查看您的HTML声明以实现这一点。有关要进行的其他更改,请参阅更新的答案。
Dim myControl As TextBox = DirectCast(lvDescriptions.Items(lvDescriptions.SelectedIndex).FindControl("txtData"), TextBox)