This is a migrated thread and some comments may be shown as answers.

Codebehind UPDATE updates ALL rows!

1 Answer 110 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John Hamman
Top achievements
Rank 1
John Hamman asked on 08 Jul 2010, 04:47 PM
Hi all, I am having unusual behavior for my radgrid. I update in code-behind using OnUpdateCommand and when I submit the change for that row, it changes ALL rows as if it's looping through all rows and updating all with the change!!

Here is my radgrid code
<telerik:RadGrid 
        ID="VendProdGrid"
        GridLines="None"
        runat="server"
        AllowAutomaticDeletes="True"
        AllowAutomaticInserts="False" 
        AllowAutomaticUpdates="False"
        PageSize="10"
        AllowPaging="True"
        AutoGenerateColumns="False"
        DataSourceID="VendProdGridSource"       
        OnItemUpdated="VendProdGrid_ItemUpdated"
        OnItemDeleted="VendProdGrid_ItemDeleted"
        OnItemInserted="VendProdGrid1_ItemInserted"
        OnItemCommand="VendProdGrid_ItemCommand"
        OnUpdateCommand="VendProdGrid_Update"
         
        OnDataBound="VendProdGrid_DataBound"
        EnableEmbeddedScripts="false">
            <PagerStyle Mode="NextPrevAndNumeric" />
     
        <MasterTableView
            Width="100%"
            CommandItemDisplay="TopAndBottom"
            DataKeyNames="VProductID"
            DataSourceID="VendProdGridSource"
            HorizontalAlign="NotSet"
            AutoGenerateColumns="False">
             
            <Columns>
                 
                <telerik:GridEditCommandColumn ButtonType="ImageButton" />
                                    
                <telerik:GridTemplateColumn  UniqueName="TemplateColumn">
                      <HeaderTemplate>
                        <strong>Product Type</strong>
                      </HeaderTemplate>
                      <ItemTemplate>
                            <strong><%# DataBinder.Eval(Container.DataItem, "VProductType")%></strong><br />
                            <em><%# DataBinder.Eval(Container.DataItem, "VProductDescription")%></em>                               
                      </ItemTemplate>
                </telerik:GridTemplateColumn>
                 
                <telerik:GridBoundColumn DataField="SupplierName" HeaderText="Supplier" SortExpression="SupplierName"
                            UniqueName="SupplierName"></telerik:GridBoundColumn>
                 
                
                 
                <telerik:GridButtonColumn CommandName="Delete" ButtonType="ImageButton" />
                 
            </Columns>
             
            <EditFormSettings EditFormType="Template">
                 
                <EditColumn UniqueName="EditCommandColumn1">
                </EditColumn>
                 
                <FormTemplate>
                    <table cellspacing="2" cellpadding="1" width="100%" border="0">
                        <tr>
                             
                                <td>Supplier:</td>
                                
                               <td>
                               <telerik:RadComboBox
                                        ID="SupplierCB"
                                        runat="server" 
                                        DropDownWidth="300"
                                        DataSourceID="SQLSupplierSource"
                                        DataValueField="SupplierID"
                                        DataTextField="SupplierName"
                                        AppendDataBoundItems="false"
                                        EnableLoadOnDemand="false"
                                        >
                                        
                               </telerik:RadComboBox>  
                                    
                                
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Product Type:
                            </td>
                            <td>
                              <telerik:RadComboBox
                                        ID="VProductTypeCB"
                                        runat="server" 
                                        DropDownWidth="300"
                                        DataSourceID="SQLVendorProductTypeList"
                                        DataValueField="VProductTypeID"
                                        DataTextField="VProductType"
                                         
                                        AppendDataBoundItems="false"
                                        EnableLoadOnDemand="false"
                                        >
                                        
                               </telerik:RadComboBox>
                             
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Description:</td>
                            <td>
                                <asp:textbox
                                    runat="server"
                                    id="VProductDescriptionEdit"
                                    causesvalidation="False"
                                    textmode="MultiLine"
                                    width="300px"
                                    uniqueid="VProductDescriptionEdit"
                                    rows="5"
                                    text='<%# DataBinder.Eval(Container.DataItem, "VProductDescription") %>'
                                    ></asp:textbox>
                             
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Limitations:</td>
                            <td>
                            <asp:textbox
                                    runat="server"
                                    id="VProductLimitationsEdit"
                                    CausesValidation="False"
                                    TextMode="MultiLine"
                                    Width="300px"
                                    UniqueID="VProductLimitationsEdit"
                                    Rows="5"
                                    Text='<%# DataBinder.Eval(Container.DataItem, "VProductLimitations") %>'
                                    ></asp:textbox>
                             
                            </td>
                        </tr>
                        <tr>
                           <td>
                                Reference Standards:</td>
                            <td>
                            <asp:textbox
                                    runat="server"
                                    id="VRefStandardEdit"
                                    CausesValidation="False"
                                    TextMode="MultiLine"
                                    Width="300px"
                                    UniqueID="VRefStandardEdit"
                                    Rows="5"
                                    Text='<%# DataBinder.Eval(Container.DataItem, "VRefStandard") %>'
                                    ></asp:textbox>
                            
                            </td>
                        </tr>
                        
                        <tr>
                            <td colspan="2">
                                <asp:Button ID="Button1" runat="server" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
                                <asp:Button ID="Button2" runat="server" Text="Cancel" CausesValidation="false" CommandName="Cancel"></asp:Button>
                            </td>
                        </tr>
                    </table>
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

And here is the codebehind:
protected void VendProdGrid_Update(object source, GridCommandEventArgs e)
    {
 
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            if (e.Item is GridEditFormItem)
            {
                 
 
                GridEditFormItem item = (GridEditFormItem)e.Item;
                int id = Convert.ToInt32(item.GetDataKeyValue("VProductID"));
                if (id != 0)
                {
 
 
                    RadComboBox SupplierCB = item.FindControl("SupplierCB") as RadComboBox;
                    RadComboBox VProductTypeID = item.FindControl("VProductTypeCB") as RadComboBox;
                    TextBox VProductDescription = item.FindControl("VProductDescriptionEdit") as TextBox;
                    TextBox VProductLimitations = item.FindControl("VProductLimitationsEdit") as TextBox;
                    TextBox VRefStandard = item.FindControl("VRefStandardEdit") as TextBox;
 
                    try
                    {
                        VendProdGridSource.UpdateParameters.Add("SupplierID", TypeCode.Int32, SupplierCB.SelectedValue.ToString());
                        VendProdGridSource.UpdateParameters.Add("VProductTypeID", TypeCode.Int32, VProductTypeID.SelectedValue.ToString());
                        VendProdGridSource.UpdateParameters.Add("VProductDescription", TypeCode.String, VProductDescription.Text);
                        VendProdGridSource.UpdateParameters.Add("VProductLimitations", TypeCode.String, VProductLimitations.Text);
                        VendProdGridSource.UpdateParameters.Add("VRefStandard", TypeCode.String, VRefStandard.Text);
                        VendProdGridSource.Update();
                       //
                        //e.KeepInEditMode = false;
                        //e.ExceptionHandled = true;
 
                        SetMessage("New Product Type " + id + " is updated!");
                    }
                    catch (Exception ex)
                    {
 
                        // e.ExceptionHandled = true;
                        SetMessage("Product Type " + id + " cannot be updated. Reason: " + ex.Message);
                    }
 
                  //  e.Canceled = true;
                    VendProdGrid.Rebind();
                }
            }
        }
    }

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Jul 2010, 10:35 AM
Hello John,

The problem might occur if the Update query is not well formed. Make sure that you have added 'WHERE' condition in your update query.


Hope this information helps,
Princy.
Tags
Grid
Asked by
John Hamman
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or