Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 21st, 2004, 11:00 AM
Authorized User
 
Join Date: Jun 2003
Location: Lisbon, , Portugal.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default Datagrid update error

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:

Code:
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
__________________
Elisa Resina
Reply With Quote
  #2 (permalink)  
Old October 21st, 2004, 12:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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 With Quote
  #3 (permalink)  
Old October 22nd, 2004, 03:24 AM
Authorized User
 
Join Date: Jun 2003
Location: Lisbon, , Portugal.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #4 (permalink)  
Old October 22nd, 2004, 07:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid.update() and DataAdapter.Update aarunlal ASP.NET 2.0 Professional 2 February 23rd, 2006 11:41 PM
Datagrid Update madhavigujja ASP.NET 1.0 and 1.1 Basics 1 December 18th, 2005 03:10 PM
Datagrid Update casselj ASP.NET 1.0 and 1.1 Professional 1 June 23rd, 2005 10:20 AM
Datagrid Does Not Update ... wecka .NET Framework 2.0 1 March 28th, 2005 04:46 AM
Dreamweaver MX2004 Datagrid update error CyberNet99 Dreamweaver (all versions) 1 December 10th, 2003 12:47 PM



All times are GMT -4. The time now is 02:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.