' nvarchar'附近的语法不正确。必须声明标量变量" @ RegID"

时间:2017-04-12 01:50:45

标签: c# asp.net sql-server visual-studio sql-update

***ERROR MESSAGE***

UPDATE Query 我试图从更新查询更新/编辑用户详细信息,但我收到错误必须声明一个标量变量@RegID。我尝试使用查询,但它仍然会出现相同的错误。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:rm3558nConnectionString2 %>" DeleteCommand="DELETE FROM [Registration] WHERE [RegID] = @original_RegID" InsertCommand="INSERT INTO [Registration] ([First Name], [Last Name], [Email ID], [Password], [Confirm Password], [DOB], [Gender]) VALUES (@First_Name, @Last_Name, @Email_ID, @Password, @Confirm_Password, @DOB, @Gender)" SelectCommand="SELECT * FROM [Registration] WHERE ([Email ID] = @Email_ID)" UpdateCommand="UPDATE [Registration] SET [First Name] = @First_Name, [Last Name] = @Last_Name, [Email ID] = @Email_ID, [Password] = @Password, [Confirm Password] = @Confirm_Password, [DOB] = @DOB, [Gender] = @Gender WHERE [RegID] = @original_RegID" OldValuesParameterFormatString="original_{0}">
                <DeleteParameters>
                    <asp:Parameter Name="original_RegID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="First_Name" Type="String" />
                    <asp:Parameter Name="Last_Name" Type="String" />
                    <asp:Parameter Name="Email_ID" Type="String" />
                    <asp:Parameter Name="Password" Type="String" />
                    <asp:Parameter Name="Confirm_Password" Type="String" />
                    <asp:Parameter DbType="Date" Name="DOB" />
                    <asp:Parameter Name="Gender" Type="String" />
                </InsertParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="Namelbl" Name="Email_ID" PropertyName="Text" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="First_Name" Type="String" />
                    <asp:Parameter Name="Last_Name" Type="String" />
                    <asp:Parameter Name="Email_ID" Type="String" />
                    <asp:Parameter Name="Password" Type="String" />
                    <asp:Parameter Name="Confirm_Password" Type="String" />
                    <asp:Parameter DbType="Date" Name="DOB" />
                    <asp:Parameter Name="Gender" Type="String" />
                    <asp:Parameter Name="original_RegID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>

网格视图

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="RegID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="RegID" HeaderText="RegID" InsertVisible="False" ReadOnly="True" SortExpression="RegID" />
                <asp:BoundField DataField="First Name" HeaderText="First Name" SortExpression="First Name" />
                <asp:BoundField DataField="Last Name" HeaderText="Last Name" SortExpression="Last Name" />
                <asp:BoundField DataField="Email ID" HeaderText="Email ID" SortExpression="Email ID" />
                <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
                <asp:BoundField DataField="Confirm Password" HeaderText="Confirm Password" SortExpression="Confirm Password" />
                <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#242121" />
            </asp:GridView>

1 个答案:

答案 0 :(得分:1)

SelectCommandSqlDataSource内输出时,使用带有空格的字段名称(标有SELECT * ...而不使用别名选择所有列)时会出现问题,而BoundField位于GridViewSELECT 1}}控件不接受使用空格的字段/列名称。

因此,您需要在SelectCommand中使用DataField语句,提及GridView中用于<asp:SqlDataSource ID="SqlDataSource1" runat="server" ... SelectCommand="SELECT [RegID], [First Name] AS First_Name, [Last Name] AS Last_Name, [Email ID] AS Email_ID, [Password], [Confirm Password] AS Confirm_Password, DOB, Gender FROM [Registration] WHERE ([Email ID] = @Email_ID)" ...> ... </asp:SqlDataSource> 属性的所有字段名称,并且仅对包含所有列的所有列进行别名他们的空白就像这样:

GridView

然后在DataField中,使用下划线声明所有SelectCommand字段名称,或者只删除SqlDataSource SortExpression<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="RegID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> <asp:BoundField DataField="RegID" HeaderText="RegID" InsertVisible="False" ReadOnly="True" SortExpression="RegID" /> <asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name" /> <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" /> <asp:BoundField DataField="Email_ID" HeaderText="Email ID" SortExpression="Email_ID" /> <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" /> <asp:BoundField DataField="Confirm_Password" HeaderText="Confirm Password" SortExpression="Confirm_Password" /> <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" /> <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" /> </Columns> ... </asp:GridView> 中约定的空格,并将其与BoundField一起使用} attribute:

...

通过上面的这些设置,{{1}}上的绑定应该可以正常工作,而无需更改DB中的字段名称。

注意:{{1}}标记了代码示例中删除的部分,以简洁起见。

类似问题:

Incorrect syntax near 'nvarchar' must declare scalar variable near @num

Incorrect syntax near 'nvarchar'

SqlDataSource/DataField Bug in 2.0(从这篇文章得出的结论)