3
我試圖在PHP經歷了幾年後開始學習ASP.NET,並且無法獲得基礎知識。ASP.NET插入問題
我只是試圖插入一行到MS SQL Server Compact 4.0數據庫與ASP.NET & 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();
}
}
}
這工作,謝謝!儘管如此,似乎必須有一個更簡單的方法 – 2011-04-30 19:09:34
很高興它幫助你。 – 2011-04-30 19:23:06