ASP.NET 插入问题
在学习了几年 PHP 经验后,我正尝试开始学习 ASP.NET,但在掌握基础知识方面遇到了困难。
我只是想使用 ASP.NET 和 MS SQL Server Compact 4.0 数据库插入一行。 C#。但是,我的页面只插入一个空白行。编辑& GridView 的删除功能正常工作。我做错了什么?
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="testSite._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>People</h2>
<p>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="first" HeaderText="first" SortExpression="first" />
<asp:BoundField DataField="last" HeaderText="last" SortExpression="last" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sqltestConnectionString %>"
DeleteCommand="DELETE FROM [People] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [People] ([first], [last]) VALUES (@first, @last)"
SelectCommand="SELECT * FROM [People]"
UpdateCommand="UPDATE [People] SET [first] = @first, [last] = @last WHERE [ID] = @ID" >
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:FormParameter Name="first" FormField="txtFirst" />
<asp:FormParameter Name="last" FormField="txtLast" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="first" Type="String" />
<asp:Parameter Name="last" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="First"></asp:Label>
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Last"></asp:Label>
<asp:TextBox ID="txtLast" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add" />
</p>
</asp:Content>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace testSite
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
}
}
}
I am trying to start learning ASP.NET after a few years of PHP experience and I am having trouble getting the basics down.
I am just trying to insert a row into a MS SQL Server Compact 4.0 database with ASP.NET & C#. However, my page only inserts a blank row. The edit & delete functions of GridView work correctly. What am I doing wrong?
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="testSite._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>People</h2>
<p>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="first" HeaderText="first" SortExpression="first" />
<asp:BoundField DataField="last" HeaderText="last" SortExpression="last" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sqltestConnectionString %>"
DeleteCommand="DELETE FROM [People] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [People] ([first], [last]) VALUES (@first, @last)"
SelectCommand="SELECT * FROM [People]"
UpdateCommand="UPDATE [People] SET [first] = @first, [last] = @last WHERE [ID] = @ID" >
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:FormParameter Name="first" FormField="txtFirst" />
<asp:FormParameter Name="last" FormField="txtLast" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="first" Type="String" />
<asp:Parameter Name="last" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="First"></asp:Label>
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Last"></asp:Label>
<asp:TextBox ID="txtLast" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add" />
</p>
</asp:Content>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace testSite
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
try this