ASP.net、SQL 数据库、插入、文本框

发布于 2024-12-20 06:11:53 字数 2923 浏览 1 评论 0原文

我目前正在 Visual Basic 2010 中使用 Web 表单。我在 Visual Basic 中创建了一个 SQL 数据库,并尝试使插入命令将值从文本框移动到数据库中。

所以我的问题是:如何从文本框(即:NameTextBox、AddressTextBox..)获取值并将它们与参数(@Name、@Address1)关联?

数据库工作正常,我通过代码输入值来测试它。

这是相关代码:

  <script runat="server">
' InsertShipper
  Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
    SqlDataSource1.Insert()
 End Sub
   </script>


 <asp:Button ID="OrderButton" runat="server" onclick="InsertShipper" Text="Order" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:OrderListConnectionString %>" 
        DeleteCommand="DELETE FROM [ORDER] WHERE [OID] = @OID" 
        InsertCommand="INSERT INTO [ORDER] ([Name], [Address1], [Address2], [City],     [State], [Zip], [Phone], [Email], [Subtotal], [Shipping]) VALUES ( @Name, @Address1,      @Address2, @City, @State, @Zip, @Phone, @Email, @Subtotal, @Shipping)" 
        SelectCommand="SELECT * FROM [ORDER]" 

        UpdateCommand="UPDATE [ORDER] SET [Name] = @Name, [Address1] = @Address1, [Address2] = @Address2, [City] = @City, [State] = @State, [Zip] = @Zip, [Phone] = @Phone, [Email] = @Email, [Subtotal] = @Subtotal, [Shipping] = @Shipping WHERE [OID] = @OID">
        <DeleteParameters>
            <asp:Parameter Name="OID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>

            <asp:Parameter name="Name" Type="String" />
            <asp:Parameter name="Address1" Type="String" />
            <asp:Parameter Name="Address2" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Subtotal" Type="String" />
            <asp:Parameter Name="Shipping" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Address1" Type="String" />
            <asp:Parameter Name="Address2" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Subtotal" Type="String" />
            <asp:Parameter Name="Shipping" Type="String" />
            <asp:Parameter Name="OID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

I am currently working in Visual Basic 2010 with a webform. I created an SQL database inside of Visual Basic and I'm trying to make the insert command move values from the textboxes into the database.

So my question is: how do I get the values from the textboxes (i.e:NameTextBox, AddressTextBox..) and associate them with the parameters (@Name, @Address1)?

The database works, I tested it by inputing values through the code.

Here is the relevant code:

  <script runat="server">
' InsertShipper
  Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
    SqlDataSource1.Insert()
 End Sub
   </script>


 <asp:Button ID="OrderButton" runat="server" onclick="InsertShipper" Text="Order" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:OrderListConnectionString %>" 
        DeleteCommand="DELETE FROM [ORDER] WHERE [OID] = @OID" 
        InsertCommand="INSERT INTO [ORDER] ([Name], [Address1], [Address2], [City],     [State], [Zip], [Phone], [Email], [Subtotal], [Shipping]) VALUES ( @Name, @Address1,      @Address2, @City, @State, @Zip, @Phone, @Email, @Subtotal, @Shipping)" 
        SelectCommand="SELECT * FROM [ORDER]" 

        UpdateCommand="UPDATE [ORDER] SET [Name] = @Name, [Address1] = @Address1, [Address2] = @Address2, [City] = @City, [State] = @State, [Zip] = @Zip, [Phone] = @Phone, [Email] = @Email, [Subtotal] = @Subtotal, [Shipping] = @Shipping WHERE [OID] = @OID">
        <DeleteParameters>
            <asp:Parameter Name="OID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>

            <asp:Parameter name="Name" Type="String" />
            <asp:Parameter name="Address1" Type="String" />
            <asp:Parameter Name="Address2" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Subtotal" Type="String" />
            <asp:Parameter Name="Shipping" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Address1" Type="String" />
            <asp:Parameter Name="Address2" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Subtotal" Type="String" />
            <asp:Parameter Name="Shipping" Type="String" />
            <asp:Parameter Name="OID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海似她心 2024-12-27 06:11:53

尝试按如下方式设置参数的 ControlID 和 PropertyName:

  <asp:ControlParameter Name="Title" 
  ControlID="txtFoo"
  PropertyName="Text"/>

请参阅 http://msdn。 microsoft.com/en-us/library/xt50s8kz.aspx

Try setting the parameters' ControlID and PropertyName as such:

  <asp:ControlParameter Name="Title" 
  ControlID="txtFoo"
  PropertyName="Text"/>

Refer to http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx

少钕鈤記 2024-12-27 06:11:53

您的参数的 Name 属性缺少“@”字符。您需要添加此参数,否则它会将其视为完全不同的参数。

Your parameters' Name property are missing the "@" character. You need to add this or it sees it as a completely different parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文