Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem about the updating a column of my gridview; when I try to update the field I get this error:

Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "@pid".

Here is a piece of my code:

<asp:GridView ID="GridView1" runat="server"  name="gd"  Height="100px" 
            Width="435px" AutoGenerateColumns="False" DataKeyNames="pid" DataSourceID="SqlDataSource1" >
    <Columns>
        <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
        <asp:BoundField DataField="pid" HeaderText="pid" 
                    SortExpression="pid" ReadOnly="True" />
        <asp:BoundField DataField="name" HeaderText="name" 
                    SortExpression="name" />
        <asp:BoundField DataField="hoghoghe rozane" HeaderText="hoghoghe rozane" 
                    SortExpression="hoghoghe rozane" />
        <asp:BoundField DataField="paye" HeaderText="paye" 
                    SortExpression="paye" />
    <\Columns>
<asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"  
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
  UpdateCommand="UPDATE [post] SET [name] = @name, [hoghoghe rozane] = @hoghoghe_rozane, [paye] = @paye WHERE (pid = @pid)" >   
     <UpdateParameters>
         <asp:Parameter Name="name" Type="String" />
         <asp:Parameter Name="hoghoghe_rozane" Type="Int32" />
         <asp:Parameter Name="paye" Type="Int32" />  
         <asp:Parameter Name="pid" Type="String" />
     </UpdateParameters>
 </asp:SqlDataSource>
Posted
Updated 30-Mar-17 13:23pm
Comments
[no name] 4-May-13 15:27pm    
<asp:Parameter Name="@pid" Type="String" />
sara-setare 4-May-13 15:32pm    
no,it didn't solve the problem!
[no name] 4-May-13 15:37pm    
So what other problem do you have?
sara-setare 4-May-13 15:45pm    
I said,i tried your solution and it didn't solve my problem,and I have thid error again!
sara-setare 4-May-13 15:46pm    
I said,I tried your solution and I get this error again!

Check here: http://msdn.microsoft.com/en-us/library/ms972948.aspx[^].
What you should have seen is, that during update, you have original and new value of a field. You have to refferre the original value of pid. Try this:
SQL
UPDATE [post] SET [name] = @name, [hoghoghe rozane] = @hoghoghe_rozane, [paye] = @paye WHERE (pid = @original_pid)
 
Share this answer
 
Comments
sara-setare 4-May-13 16:01pm    
thank you,I tried it and i get the same error!!
I guess it is a DataType conflict.
That means the DataType of pid in DataBase is different from the data you are sending.

As the name is pid, it may be of type integer and the type you are sending is string. I may be wrong.

So, check once what are DataType in DataBase.
 
Share this answer
 
I think the issue is with the field "hoghoghe rozane". It has a space in it. Although you name your parameter as hoghoghe_rozane, the GridView field name has a space in it. I am not sure if or how GridView will associate the field with the parameter when they have different names but I believe the Update command of the GridView includes the field name (with a space), so the update execution breaks. The update execution in the background is done via sp_executesql so a syntax error occurs when the parameter name has a space (you can check the T-SQL script that is executed with SQL Server Profiler).

A solution to this situation, is to provide an alias name to the column in the Select statement that populates the Gridview. I don't see your SelectCommand in your code. If you have one, it probably looks like:

SQL
SELECT ..., [hoghoghe rozane], ...
FROM ...


So what I suggest is to rewrite it as:

SQL
SELECT ..., [hoghoghe rozane] AS hoghoghe_rozane, ...
FROM ...


Since I anticipate that you may want to show the column header with a space instead of an underscore, you can set the HeaderText property of this field to what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900