ASP.net、SQL 数据库、插入、文本框
我目前正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试按如下方式设置参数的 ControlID 和 PropertyName:
请参阅 http://msdn。 microsoft.com/en-us/library/xt50s8kz.aspx
Try setting the parameters' ControlID and PropertyName as such:
Refer to http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx
您的参数的 Name 属性缺少“@”字符。您需要添加此参数,否则它会将其视为完全不同的参数。
Your parameters' Name property are missing the "@" character. You need to add this or it sees it as a completely different parameter.