Subject: Datagrid update error
Posted By: eresina Post Date: 10/21/2004 11:00:13 AM
Hello, I have a webform with a datagrid and I receive the following error when doing the update command in the datagrid after editing it:

"Cannot update identity column 'Id_FHora_Horas'"

Here is the code for the datagrid update command:


Private Sub dgeditponto_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgeditponto.UpdateCommand
        'colcoar o valor da ddl no campo da fk
        CType(e.Item.Cells(5).Controls(0), TextBox).Text = _
        CType(e.Item.FindControl("ddlE_S2"), DropDownList).SelectedItem.Value()

        'encher o dataset 
        FHorasDataAdapter2.Fill(DataSetE_S1)
        TipoMovDataAdapter3.Fill(DataSetE_S1)

        'identificaar a linha a editar
        Dim objEditRow As DataRow = DataSetE_S1.Tables(0).Rows.Find(CType(e.Item.Cells(5).Controls(0), TextBox).Text)
         'andar através das celulas da datagrid e colocar a informação no dataset
                
        Dim intCount As Integer
        For intCount = 0 To e.Item.Cells.Count - 3
            If e.Item.Cells(intCount).Controls.Count > 0 Then
                If TypeOf (e.Item.Cells(intCount).Controls(0)) Is TextBox Then
                    'se é uma etxtbox com dados...
                    Dim strValue As String = CType(e.Item.Cells(intCount).Controls(0), TextBox).Text
                    'colocar o valor (or ou o nulo...) no campo respectivo do dataset
                    If strValue = "" Then
                        objEditRow.Item(dgeditponto.Columns(intCount).SortExpression) = System.DBNull.Value
                    Else
                        If intCount <> 2 Then
                            objEditRow.Item(dgeditponto.Columns(intCount).SortExpression) = strValue
                        End If
                    End If

                End If
            End If
        Next
      
        ''uptade backend data
        FHorasDataAdapter2.Update(DataSetE_S1, "fhoras_horas")

        'deselecciona items da datagrid e faz o rebind
        With dgeditponto
            .SelectedIndex = -1
            .EditItemIndex = -1
                        .DataSource = DataSetE_S1
            .DataBind()
        End With
    End Sub


Any suggestions?

Thanks,


Elisa Resina
Reply By: bmains Reply Date: 10/21/2004 12:14:07 PM
Is your ID field the first field?  Try changing your looping to start from the 1 position.  You can't change the identity field once it is set.

Brian
Reply By: eresina Reply Date: 10/22/2004 3:24:26 AM
yes, is the first field in table "Fhoras_Horas" . I don't show it in my datagrid. I changed the counter and I received the same message. I don't have any idea how I can solved it because I don't see any reason for the code not work. In this situation how would you write it?

Thanks again,

Elisa

Elisa Resina
Reply By: bmains Reply Date: 10/22/2004 7:15:40 AM
Hello,

I usually make the ID field bound, readonly, and hidden and in the first position.  You access it with the index of zero.  The others can be bound fields, or template columns.  Bound fields are accessed by e.item.cells(<index>).text, whereas controls are accessed the way you did it; using findcontrols or the controls collection.  I would go for a more manual approach:

With  e.Item
  Dim intID As Integer = Ctype(.Cells(0).Text, Integer)
  Dim strField1 As String = .Cells(1).Text
  Dim strField2 As String = .Cells(2).Text

  Dim objEditRow As DataRow = DataSetE_S1.Tables(0).Rows.Find(intID)
  objEditRow.BeginEdit()

  objEditRow("Field1") = strfield1
  if (strField2 <> string.Empty) Then
    objEditRow("Field2") = strField2
  else
    objEditRow("Field2") = DbNull.Value
  end if

  objEditRow.EndEdit()
  FHorasDataAdapter2.Update(DataSetE_S1, "fhoras_horas")
End With

Brian

Go to topic 21087

Return to index page 736
Return to index page 735
Return to index page 734
Return to index page 733
Return to index page 732
Return to index page 731
Return to index page 730
Return to index page 729
Return to index page 728
Return to index page 727