I'm converting my website from Classic ASP to ASP.net and outside of a few bumps, I've had no problems until today. But I am learning on the fly.
I'm trying to update a record in a formview and every time I do, I get the following error: "Must declare the scalar variable "@FirstName"".
I've googled and searched, and can't see what I'm doing wrong. I'm sure there's something little that I'm doing incorrect.
<asp:FormView ID="formRecruit" runat="server" DataSourceID="sourceRecruit"
EnableModelValidation="True" DataKeyNames="ID">
<ItemTemplate>
<asp:Table ID="Table1" Width="100%" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell Width="50%">
<asp:Table ID="Table2" Width="100%" HorizontalAlign="Center" runat="server">
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell>
<asp:Table ID="Table3" Width="100%" runat="server">
<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell>Name:</asp:TableCell>
<asp:TableCell><%#Eval("FirstName") %></asp:TableCell>
<a开发者_运维百科sp:TableCell><asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="LinkButton1" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
<EditItemTemplate>
<asp:Table ID="Table1" Width="100%" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell Width="50%">
<asp:Table ID="Table2" Width="100%" HorizontalAlign="Center" runat="server">
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell>
<asp:Table ID="Table3" Width="100%" runat="server">
<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell>Name:</asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName") %>'/></asp:TableCell>
<asp:TableCell><asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="LinkButton1" /><asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="LinkButton2" CausesValidation="false" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sourceRecruit" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT * FROM [Players] WHERE ([ID] = @ID)"
UpdateCommand="UPDATE [Players] SET [FirstName] = @FirstName WHERE [ID] = @ID">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Not 100% sure, but try to have the SqlDataSource
this way and see if it works:
<asp:SqlDataSource ID="sourceRecruit" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT * FROM [Players] WHERE ([ID] = @ID)"
UpdateCommand="UPDATE [Players] SET [FirstName] = @FirstName WHERE [ID] = @ID">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
You may also want to try converting the asp:Table
, asp:TableRow
tags etc to their HTML equivalent. It's been quite some time since I last worked with ASP.NET, but if I remember right, the controls somehow does not play well with data sources when you put them inside some other server controls.
精彩评论