如何在listview中使用insert参数和文本框文本

时间:2014-07-23 14:11:56

标签: c# asp.net sql-server

我正在尝试使用我的SqlDataSource使用TextBox中的值将项目输入数据库,但是没有插入任何内容。

这是我的SqlDataSource

<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem FROM Core.SectionItem_Lkup">               
</asp:SqlDataSource>

<asp:SqlDataSource ID="SectionIDDataSource" runat="server " ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItemID, SectionItem, SectionItemLabel) VALUES (@SectionID, @SectionItem, @SectionItemLabel)" SelectCommand="  SELECT MAX(SectionItemID) + 1 AS SectionItemID FROM [ORHP_Dev03182014].[Core].[SectionItem_Lkup]" OnInserting="Section_OnInserted">
    <InsertParameters>
        <asp:Parameter Name="SectionID" />
        <asp:Parameter Name="SectionItem" />
        <asp:Parameter Name="SectionItemLabel" />
    </InsertParameters>
</asp:SqlDataSource>

以下是listview中的文本框:

<InsertItemTemplate>
    <tr style="">
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" On />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
        </td>
        <td>
            <div style="font-size: .8em;">
            <asp:FormView ID="SectionIDFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionIDDataSource">
            <ItemTemplate>
                <asp:Label ID="SectionIDLabel" runat="server" Text="SectionID" Font-Bold="true" Font-Size="1.2em" />
                <asp:TextBox ID="SectionIDTextBox" runat="server" Text='<%# Eval("SectionItemID") %>' Width="50px" />
                <asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
                <asp:TextBox ID="SectionItemTextBox" runat="server" Text="" />
                <asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
                <asp:TextBox ID="SectionItemLabelTextBox" runat="server" Text="" />
            </ItemTemplate>
            </asp:FormView>
        </div>
      </td>
   </tr>
</InsertItemTemplate>

这是我背后的代码:

using (SqlConnection connection = new SqlConnection("Data Source=RCK-HRSA-DB01;Initial Catalog=ORHP_Dev03182014;User ID=userid;Password=password"))
{
    try
    {
        SqlCommand cmd1 = new SqlCommand("INSERT INTO Core.SectionItem_Lkup(SectionItemID, SectionItem, SectionItemLabel) VALUES (" + SectionIDTextBox.Text + ", " + SectionItemTextBox.Text + ", " + SectionItemLabelTextBox.Text + ")");
        connection.Open();
        cmd1.Connection = connection;
        cmd1.CommandType = CommandType.Text;
        cmd1.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    }
}

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

当您走的路线时,您将遇到的问题是找到ControlParameters的控件。当控件位于数据绑定控件中时,该ID不是您声明的内容。在渲染后在页面上查看源时,您可以看到这一点。

对于FormViews,它通常会出现SectionIDFormView$SectionIDLabel,这显然与SectionIDLabel不同。所以使用像这样的ControlParame可能对你有用:

<InsertParameters>
    <asp:ControlParameter ControlID="SectionIDFormView$SectionIDLabel"
        Direction="Input" Name="SectionID" PropertyName="Text" />
</InsertParameters>

作为替代方案,当单击ListView上的Insert按钮时,处理click事件并在其中执行插入操作。将所有插入的SqlDataSource插入。

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Insert")
    {
        ListViewItem item = ListView1.InsertItem;
        FormView SectionIDFormView = (FormView)item.FindControl("SectionIDFormView");
        TextBox SectionIDTextBox = (TextBox)SectionIDFormView.FindControl("SectionIDTextBox");
        //Find your other controls
        //Do your insert
    }
}

尝试使用参数化查询:

SqlCommand cmd1 = new SqlCommand("INSERT INTO Core.SectionItem_Lkup(SectionItemID, SectionItem, SectionItemLabel) VALUES (@SectionItemID, @SectionItem, @SectionItemLabel)");

cmd1.Parameters.AddWithValue("@SectionItemID", SectionIDTextBox.Text);
cmd1.Parameters.AddWithValue("@SectionItem", SectionItemTextBox.Text);
cmd1.Parameters.AddWithValue("@SectionItemLabel", SectionItemLabelTextBox.Text);