0

I have a GridView that functions off an sqlDataSource, it allows inserting and updating data. However, after the data is inserted, the "UpdatedBY" should not be editable by the user. So I added a readonly property to that bound column and set it to true, which generates the following error upon attempting to edit/update a column: Must declare the scalar variable "@UpdateBY". Note: the error only occurs when I implement a readonly property onto the "UpdateBY" column. When removed, no error when updating the Grid, but I do not want the user to be able to modify the UpdateBY column

Hypertext:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Mode_ID" DataSourceID="SqlDataSource1"  
     AutoGenerateEditButton="True" AllowPaging="True" AllowSorting="True">
     <Columns>
        <asp:BoundField DataField="Mode_ID" HeaderText="Mode ID" SortExpression="Mode_ID" Visible="false" />

        <asp:BoundField DataField="ShipMode" HeaderText="Ship Mode" SortExpression="ShipMode" />

         <asp:TemplateField HeaderText="Active" SortExpression="Active">
             <EditItemTemplate>
                 <asp:CheckBox ID="chkactive" runat="server" Checked='<%# Bind("Active") %>'></asp:CheckBox>
             </EditItemTemplate>
             <ItemTemplate>
                 <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Active") %>'></asp:CheckBox>
             </ItemTemplate>
         </asp:TemplateField>

        <asp:BoundField DataField="UpdateDate" HeaderText="Update Date" SortExpression="UpdateDate" />

        <asp:BoundField DataField="UpdateBY" HeaderText="Update BY" SortExpression="UpdateBY" ReadOnly="true"  />
     </Columns>
    </asp:GridView> 

...the SQL data source:

 <asp:SqlDataSource 
    ID="SqlDataSource1" 
    ConnectionString="<%$ ConnectionStrings:ShipperNotificationConnectionString %>" 
    SelectCommand="SELECT Mode_ID, ShipMode, Active, UpdateDate, UpdateBY FROM [ShipMode]" 
    UpdateCommand="UPDATE ShipMode SET ShipMode=@ShipMode,
    Active=@Active,UpdateDate=@UpdateDate, UpdateBY=@UpdateBY
    WHERE Mode_ID=@Mode_ID"
   InsertCommand="INSERT ShipMode (ShipMode,Active,UpdateDate,UpdateBY)
            VALUES (@ShipMode,@Active,@UpdateDate,@UpdateBY)"    
   runat="server" 
        />

could I get some assistance as to what I'm missing / failing to understand please?

user1724708
  • 1,409
  • 7
  • 31
  • 53

1 Answers1

0

I belieave you are getting the error once you hit Update correct?

You should follow the approach described here

From what I understand, you want to restrict the user from changing the value of the "UpdatedBy" field since that will defeat the purpose of this field.

In you ASPX code you must set your GridView1 OnRowUpdating = GridView1_RowUpdating, here is the code-behind for that:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.Rows[GridView1.EditIndex];

    // Put a debug point here to check if value of originalUpdatedBy = UpdatedBy of Edit row
    string originalUpdatedBy= row.Cells[7].Text;         

    if(!string.IsNullOrEmpty(originalUpdatedBy))
    {
        SqlDataSource1.UpdateParameters.Add("@UpdateBy", originalUpdatedBy);
        SqlDataSource1.Update();
    }
}
Community
  • 1
  • 1
lucidgold
  • 4,432
  • 5
  • 31
  • 51