2017-04-12 101 views
0

***ERROR MESSAGE***「nvarchar」附近的語法不正確。必須聲明標量變量「@RegID」

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

[接近 'nvarchar的' 語法錯誤的可能的複製必須申報近@num標量變量](http://stackoverflow.com/questions/11801373/incorrect-syntax-near-nvarchar-must-declare-scalar-variable-near-num) –

+0

@Div沒有幫助 – Rajat

+0

您的查詢似乎可以運行,您是否已將'SqlDataSource'綁定到另一個控件(例如, 'GridView')?確保你的'Bind'命令不包含這樣的括號:'<%#Bind(「[RegID]」)%>' –

回答

1

當使用具有空白字符(標有SELECT * ...,其選擇的所有列,而無需使用別名)字段名內部SqlDataSourceSelectCommand輸出將出現問題,而BoundField內部GridView控制沒有按不接受使用空格的字段/列名。

因此,你需要使用SELECT聲明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上的綁定應該正常工作,而不更改DB中的字段名稱。

注:...爲簡潔起見刪除了代碼示例中的部分。

類似的問題:

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

Incorrect syntax near 'nvarchar'

SqlDataSource/DataField Bug in 2.0(從這篇帖子中的結論)