Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 84751

Why Is key value wronf when editing a radgrid record

$
0
0

I have the following setup in my RadGrid lines in bold contain my database table primary key named LinkAnalysisId

<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton">
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="LinkAnalysisId" HeaderText="Link ID" ReadOnly="true"
ForceExtractValue="Always" ConvertEmptyStringToNull="true" Display="False" />
<telerik:GridHyperLinkColumn FilterControlAltText ="Filter LinkID2 column"
Uniquename="LinklId2" DataNavigateUrlFormatString ="/SubjectList2.aspx?LinkID={0}"
DataTextField="LinkAnalysisName" HeaderText="Link Analysis Name"
DataNavigateUrlFields="LinkAnalysisId" />

<telerik:GridBoundColumn DataField="LinkAnalysisName" HeaderText="LinkAnalysisName" SortExpression="LinkAnalysisName"
UniqueName="LinkAnalysisName" Display="False">

Other bound columns are not shown  when I update a records and try to save I run this C# code

protectedvoidRadGrid1_UpdateCommand(objectsender, Web.UI.GridCommandEventArgs e)
        {        
               var editableItem = ((GridEditableItem)e.Item);
 
           var linkAnalysisId = (int)RadGrid1.MasterTableView.Items[0].GetDataKeyValue("LinkAnalysisId");
           
              //retrive entity from the Db
 
           var record = DbContext.LinkAnalysis.Where(n => n.LinkAnalysisId == linkAnalysisId).FirstOrDefault();
 
              if(record != null)
              {
                  //update entity's state
                  editableItem.UpdateValues(record);
                  try
                  {
                      //save chanages to Db
                      DbContext.SaveChanges();
                  }
                  catch(System.Exception)
                  {
                      ShowErrorMessage();
                  }
 
             }
       }
     The save does not pass the try catch block  I have done some trouble shooting and I have found the code      

var linkAnalysisId = (int)RadGrid1.MasterTableView.Items[0].GetDataKeyValue("LinkAnalysisId");

which sets the linkAnalysis variable is not getting the correct ID number so it appears I have something wrong. In debug mode when I inspect the values in the var editableItem I am seeing the correct linkAnalysis ID number it is just the code above is not getting the right record what have I done wrong here?

 

Thanks


Viewing all articles
Browse latest Browse all 84751

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>