【问题标题】:Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@RegID"'nvarchar' 附近的语法不正确。必须声明标量变量“@RegID”
【发布时间】:2017-04-12 01:50:44
【问题描述】:

我正在尝试从更新查询中更新/编辑用户详细信息,但出现错误必须声明标量变量@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>

【问题讨论】:

  • @Div 没有帮助
  • 您的查询似乎可以运行,您是否将SqlDataSource 绑定到另一个控件(例如GridView)?确保您的 Bind 命令不包含这样的括号:&lt;%# Bind("[RegID]") %&gt;
  • @TetsuyaYamamoto 我也编辑并添加了网格视图代码,请看上面
  • 我发现您的问题并不是真正来自UpdateCommand,而是来自您的SelectCommandSelectCommand="SELECT * FROM [Registration] WHERE ([Email ID] = @Email_ID) 在不使用别名的情况下返回 所有 字段名称,并且当绑定到 GridView 时,您的列名称上不能有空格。尝试使用SelectCommand 中的下划线为每个列名称添加别名,并使用下划线更改所有BoundField 名称。

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


【解决方案1】:

SelectCommandSqlDataSource 中使用带空格的字段名称输出(标记为SELECT * ...,它选择所有列而不使用别名),而BoundField 内部GridView 控件不接受字段/使用空格的列名。

因此,您需要在SelectCommand 中使用SELECT 语句,并在GridView 中提及用于DataField 属性的所有字段名称,并且只为所有带有空格的列起别名,例如这个:

<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 字段名称,或者仅删除SelectCommandSqlDataSource 中的空格,并将它们与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 上的绑定应该可以正常工作,而无需更改 DB 中的字段名称。

注意:为简洁起见,... 标记了代码示例中已删除的部分。

类似问题:

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

Incorrect syntax near 'nvarchar'

SqlDataSource/DataField Bug in 2.0(此帖得出的结论)

【讨论】:

    猜你喜欢
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多