ASP gridview 不更新数据库
Posted
技术标签:
【中文标题】ASP gridview 不更新数据库【英文标题】:ASP gridview not updating database 【发布时间】:2013-08-25 18:37:37 【问题描述】:当我在 gridview 的一行上单击编辑时,它会显示应有的数据。它显示了我希望看到它们的文本框,它允许我修改框中的数据。当我单击该行上的更新时,它的作用与取消相同;页面重新填充而不保存任何数据更改。数据库也没有改变。
<%@ Page Title="Employee Lookup"Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="EmployeeLookup.aspx.cs" Inherits="WSC.EmployeeLookup" %>
<p>
<asp:Table ID="Table1" runat="server" CellPadding="5" Width="442px">
<asp:TableRow>
<asp:TableCell><asp:Button ID="NewUserButton" PostBackUrl="~/EmployeeInfo.aspx" runat="server" Text="New Employee" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="eID"
AutoGenerateEditButton="True" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to display." AllowSorting="True" AllowPaging="True">
<Columns>
<asp:BoundField DataField="eID" HeaderText="Employee ID" ReadOnly="True" SortExpression="eID" />
<asp:BoundField DataField="eFirstName" HeaderText="FirstName" SortExpression="eFirstName" />
<asp:BoundField DataField="eLastName" HeaderText="LastName" SortExpression="eLastName" />
<asp:TemplateField HeaderText="JobTitle" SortExpression="eJobTitle">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("eJobTitle") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("eJobTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept Code" SortExpression="deptNumber">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("deptNumber") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("deptNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName" SortExpression="eUserName">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("eUserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("eUserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="ePassword">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ePassword") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="*********"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="eEmail">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("eEmail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("eEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hire Date" SortExpression="eDateHire">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("eDateHire") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eDateHire") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="securityID" SortExpression="securityID">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("securityID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("securityID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="ErrorMessageLabel" runat="server" Text="" ></asp:Label>
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="C:\WSC\WSC\WSC\CIS470_Database.accdb"
SelectCommand="SELECT `eID`, `eFirstName`, `eLastName`, `eJobTitle`, `deptNumber`, `eUserName`, `ePassword`, `eEmail`, `eDateHire`, `securityID` FROM `Employee`"
UpdateCommand="UPDATE `Employee` SET `eFirstName` = ?, `eLastName` = ?, `eJobTitle` = ?, `deptNumber` = ?, `eUserName` = ?, `ePassword` = ?, `eEmail` = ?, `eDateHire` = ?, `securityID` = ? WHERE `eID` = ?">
<DeleteParameters>
<asp:Parameter Name="eID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="eFirstName" Type="String" />
<asp:Parameter Name="eLastName" Type="String" />
<asp:Parameter Name="eJobTitle" Type="String" />
<asp:Parameter Name="deptNumber" Type="String" />
<asp:Parameter Name="eUserName" Type="String" />
<asp:Parameter Name="ePassword" Type="String" />
<asp:Parameter Name="eEmail" Type="String" />
<asp:Parameter Name="ePhone" Type="String" />
<asp:Parameter Name="eDateHire" Type="DateTime" />
<asp:Parameter Name="eDateTerm" Type="DateTime" />
<asp:Parameter Name="securityID" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
</p>
【问题讨论】:
在这里查看我的答案:***.com/questions/31044683/… 【参考方案1】:添加:
<asp:Parameter Name="eID" Type="Int32" />
收件人:
<UpdateParameters>
<asp:Parameter Name="eFirstName" Type="String" />
<asp:Parameter Name="eLastName" Type="String" />
<asp:Parameter Name="eJobTitle" Type="String" />
<asp:Parameter Name="deptNumber" Type="String" />
<asp:Parameter Name="eUserName" Type="String" />
<asp:Parameter Name="ePassword" Type="String" />
<asp:Parameter Name="eEmail" Type="String" />
<asp:Parameter Name="ePhone" Type="String" />
<asp:Parameter Name="eDateHire" Type="DateTime" />
<asp:Parameter Name="eDateTerm" Type="DateTime" />
<asp:Parameter Name="securityID" Type="Int32" />
</UpdateParameters>
并使用:
eFirstName = @eFirstName, ... WHERE eID = @eID"
而不是您在 UpdateCommand 中使用的内容。问题是您的参数必须在更新和删除命令中以 @ 开头。然后他们之间的参数会很大
<DeleteParameters></DeleteParameters>
和
<UpdateParameters></UpdateParameters>
【讨论】:
以上是关于ASP gridview 不更新数据库的主要内容,如果未能解决你的问题,请参考以下文章
ASP .NET - 在 GridView 中更新之前保存现有数据的副本