943,102 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 688
  • VB.NET RSS
Mar 21st, 2010
0

multiple rows in add row problem

Expand Post »
have a method who gets data from one table, put the data into variables before it it returned into the same table with changed ID etc.
My problem is that it adds the first row without any problems, but when it is supposed to add row number 2 the error says tha the row allready exists in the table. and I can not understand why.
I have put in a couple of messageboxes to se that the ID is changing and that the ordreID is correct and changing as they should and they do, but stll I get that errormessage.

My code looks like this:
VB.NET Syntax (Toggle Plain Text)
  1. Public Sub Ordreupdate()
  2.  
  3. Dim dtbl As New DataTable
  4. Dim dt As New DataTable
  5. dtbl = Kalkyle1DataSet.Tables("Ordre")
  6. dt = dtbl.Clone
  7.  
  8. Dim copyRows1 As DataRow
  9. Dim copyRows() As DataRow = _
  10. Kalkyle1DataSet.Ordre.Select("OrdreID = 1")
  11. Dim newOrdreRow As DataRow = Kalkyle1DataSet.Tables("Ordre").NewRow()
  12.  
  13. For Each copyRows1 In copyRows
  14. dt.Rows.Clear()
  15. Next
  16. For Each copyRows1 In copyRows
  17. dt.ImportRow(copyRows1)
  18. Next
  19. DataGridView1.DataSource = dt
  20.  
  21.  
  22. '*********************************************************************
  23. Dim conn = New SqlClient.SqlConnection
  24. conn = New SqlConnection(Form1.DS2)
  25. Dim myScalarQuery As String
  26. Dim ID As Integer
  27. Dim MaxID As Integer
  28. Dim KundeID As Integer
  29.  
  30. conn.Open()
  31. myScalarQuery = " Select Max(OrdreID) As ID From Ordre"
  32. Dim myCommand As New SqlCommand(myScalarQuery, conn)
  33. ID = myCommand.ExecuteScalar()
  34. conn.Close()
  35. MaxID = ID + 1
  36. KundeID = CInt(KundeIDTextBox.Text)
  37.  
  38.  
  39.  
  40.  
  41. Dim n As Integer = 0
  42. For Each copyRows1 In copyRows
  43.  
  44. Dim Ordredato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Ordedato")
  45. Dim Kalkopprettet As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Kalkyle_opprettet")
  46. Dim LevDato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Leveringsdato")
  47. Dim Virkelig_LevDato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Virkelig_leveringsdato")
  48. Dim Hovedtegningnr As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Hovedtegningnr")
  49. Dim Prodtype As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Produkttype")
  50. Dim MatType As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Materialtype")
  51. Dim KonstrDato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Konstruktor_dato")
  52. Dim KalkUtarb As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Kalkyle_utarbeidet")
  53. Dim EtterkalkUtarb As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Etterkalkyle_utarbeidet")
  54. Dim TegnRev As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Tegningsrevisjon")
  55. Dim Status As Integer = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Status")
  56.  
  57.  
  58. newOrdreRow("OrdreID") = MaxID
  59. newOrdreRow("KundeID") = KundeID
  60. newOrdreRow("Ordedato") = Ordredato
  61. newOrdreRow("Kalkyle_opprettet") = Kalkopprettet
  62. newOrdreRow("Leveringsdato") = LevDato
  63. newOrdreRow("Virkelig_leveringsdato") = Virkelig_LevDato
  64. newOrdreRow("Hovedtegningnr") = Hovedtegningnr
  65. newOrdreRow("Produkttype") = Prodtype
  66. newOrdreRow("Materialtype") = MatType
  67. newOrdreRow("Konstruktor_dato") = KonstrDato
  68. newOrdreRow("Kalkyle_utarbeidet") = KalkUtarb
  69. newOrdreRow("Etterkalkyle_utarbeidet") = EtterkalkUtarb
  70. newOrdreRow("Tegningsrevisjon") = TegnRev
  71. newOrdreRow("Status") = Status
  72.  
  73.  
  74. Kalkyle1DataSet.Tables("Ordre").Rows.Add(newOrdreRow)
  75. n = n + 1
  76. Next
  77.  
  78. SaveProsess()
  79.  
  80. End Sub

Annyone who can see why I get that error?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
johmolan is offline Offline
45 posts
since Aug 2009
Mar 22nd, 2010
0
Re: multiple rows in add row problem
Move the line Dim newOrdreRow As DataRow = Kalkyle1DataSet.Tables("Ordre").NewRow() so that it's inside the third iteration, just above the line
newOrdreRow("OrdreID") = MaxID (this is for fixing the error message).
And add the line Kalkyle1DataSet.AcceptChanges() just after the iteratation.
Also, if OrdreID is the unique identifier you need to increment it on every itereration. Not just once before you start to copy records.
So, change the line newOrdreRow("OrdreID") = MaxID to
VB.NET Syntax (Toggle Plain Text)
  1. MaxID += 1
  2. newOrdreRow("OrdreID") = MaxID
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
651 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Simple Timer with Progress Bar and NumericUpDown
Next Thread in VB.NET Forum Timeline: VB.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC