gpt4 book ai didi

ASP.NET FormView 更新失败

转载 作者:行者123 更新时间:2023-11-29 01:01:01 24 4
gpt4 key购买 nike

下面是我的代码。我遇到的问题是我的绑定(bind)文本字段没有按预期将信息发送回 UpdateCommand。如果我将任何 @variables 替换为硬编码文本,则更新会按预期进行。因此,如果我将 @RegID 替换为“25”,那么我的记录已成功更新但空白,因为所有其他 @variables 在更新处理时均为 NULL。我的错误在哪里?

         <asp:FormView ID="formView" DataSourceID="Reg" runat="server" DataKeyNames="RegID" AllowPaging="true">

<ItemTemplate>
<h3 style="font-size:14px;"><%#Eval("Org") %></h3>
<table border="0">
<tr class="RowHighlight">
<td>Name:</td><td><%#Eval("Name") %></td>
</tr>
<tr>
<td>Phone Number:</td><td><%#Eval("PhoneNumber") %></td>
</tr>
<tr class="RowHighlight">
<td>Email Address:</td><td><%#Eval("EmailAddress")%></td>
</tr>
<tr>
<td>Address:</td><td><%#Eval("Address") %></td>
</tr>
<tr class="RowHighlight">
<td>Purchase Date:</td><td><%#Eval("MonthOfPurchase") + " " + Eval("YearOfPurchase")%></td>
</tr>
<tr>
<td>Notes:</td><td><%#Eval("Notes") %></td>
</tr>
<tr>
<td><asp:LinkButton ID="btnEdit" Text="Edit Details" runat="server" CommandName="Edit" /></td>
</tr>
</table>
</ItemTemplate>

<EditItemTemplate>
<h3 style="font-size:14px;"><%#Eval("Org") %></h3>
<table border="0">
<tr>
<td>ID</td><td><%#Eval("RegID")%></td>
</tr>
<tr class="RowHighlight">
<td>Name:</td><td><asp:TextBox ID="txtName" runat="server" Width="100%" Text='<%# Bind("Name") %>' /></td>
</tr>
<tr>
<td>Phone Number:</td><td><asp:TextBox ID="txtPhoneNumber" runat="server" Width="100%" Text='<%# Bind("PhoneNumber") %>' /></td>
</tr>
<tr class="RowHighlight">
<td>Email Address:</td><td><asp:TextBox ID="txtEmailAddress" runat="server" Width="100%" Text='<%# Bind("EmailAddress") %>' /></td>
</tr>
<tr>
<td>Address:</td><td><asp:TextBox ID="txtAddress" runat="server" Width="100%" Text='<%# Bind("Address") %>' /></td>
</tr>
<tr class="RowHighlight">
<td>Month Of Purchase:</td><td><asp:TextBox ID="txtMonthOfPurchase" runat="server" Width="100%" Text='<%# Bind("MonthOfPurchase") %>' /></td>
</tr>
<tr>
<td>Year Of Purchase:</td><td><asp:TextBox ID="txtYearOfPurchase" runat="server" Width="100%" Text='<%# Bind("YearOfPurchase") %>' /></td>
</tr>
<tr class="RowHighlight">
<td>Notes:</td><td><%#Eval("Notes") %></td>
</tr>
<tr>
<td><asp:LinkButton ID="btnSave" Text="Save Changes" runat="server" CausesValidation="true" CommandName="Update" /></td>
<td><asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" /></td>
</tr>
</table>
</EditItemTemplate>

</asp:FormView>

<asp:SqlDataSource ProviderName="System.Data.Odbc" ID="Reg" runat="server"
ConnectionString="DRIVER={MySQL ODBC 3.51 Driver}; SERVER=<SERVER_NAME>; PORT=3306; DATABASE=<DATABASE> UID=<USERID>; PWD=<PASSWORD>; OPTION=3;"

SelectCommand="SELECT * FROM tblRegsitration
WHERE EmailSent=0 ORDER BY Org"

UpdateCommand="UPDATE tblRegsitration SET Name=@Name,
PhoneNumber=@PhoneNumber,
EmailAddress=@EmailAddress,
Address=@Address,
MonthOfPurchase=@MonthOfPurchase,
YearOfPurchase=@YearOfPurchase
WHERE (RegID=@RegID)" />

最佳答案

您没有任何 parameters定义。

示例:

<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"

InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName);
SELECT @EmpID = SCOPE_IDENTITY()"
UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName
WHERE EmployeeID=@EmployeeID"
DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
RunAt="server">

<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>

<InsertParameters>
<asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
</InsertParameters>

</asp:sqlDataSource>

关于ASP.NET FormView 更新失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3229766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com