|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
error when updating Listview after record addI have a Listview that shows some records from a database. When I click on one of the files in the Listview the details are shown in a subwindow on my form. Now when I add a new record by DragDrop on the Listview the view gets updated so the newly added file is shown. In the subwindow I can then edit the details of the record. But when I edit a newly added record and click save I get an error message: InvalidArgument = Value of '6' is not valid for 'index'. The next code saves the details of the record in the Subwindow: Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & IDclicked Dim da As New OleDb.OleDbDataAdapter(sql, conn) Dim cb As New OleDb.OleDbCommandBuilder(da) Dim ds As New DataSet conn.Open() Try da.SelectCommand = New OleDb.OleDbCommand(sql, conn) da.Fill(ds) If ds.Tables.Count > 0 _ AndAlso ds.Tables(0).Rows.Count > 0 Then Dim dr As DataRow = ds.Tables(0).Rows(0) dr.Item("kenmerk") = tbKenmerk.Text dr.Item("status") = tbStatus.Text dr.Item("auteur") = tbAuteur.Text dr.Item("datum_gewijzigd") = tbGewijzigd.Text dr.Item("expires") = tbVerloopt.Text dr.Item("hddlokatie") = tbFysiekeLokatie.Text dr.Item("samenvatting") = tbSamenvatting.Text dr.Item("soort") = tbSoort.Text dr.Item("rubricering") = tbRubricering.Text dr.Item("inuit") = tbInUit.Text 'sent the updated dataSet to the database da.Update(ds) UpdateListViewNode(IDclicked, dr) End If Catch oException As OleDbException MessageBox.Show(oException.Message) Catch oException As Exception MessageBox.Show(oException.Message) End Try conn.Close() EnableAllFields(False) btnSave.Visible = False btnCancel.Visible = False btnExploreFysiekeLokatie.Visible = False tbSamenvatting.ForeColor = Color.Gray tbVerloopt.ReadOnly = True End Sub The next code updates the Listview and raises the error message: Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As DataRow) Dim Key As String = CStr(ID) If ListView.Items.ContainsKey(Key) Then Dim lvItem As ListViewItem = ListView.Items(Key) lvItem.Text = CStr(Dr.Item("filenaam")) lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) lvItem.SubItems(5).Text = FixNull(Dr.Item ("datum_gewijzigd")) lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) End If End Sub Can anyone tell me why this error occures? Regards Marco The Netherlands (Not SA). Co,
The answer is simple, use a DataGridView and remove the vertical header column and set some more properties to by instance remove the add row button, than you have a better listview, which you simply can use with a datasource for your datatable. Cor Show quoteHide quote "Co" <vonclausow***@gmail.com> wrote in message news:674a145e-70a5-4e2e-96b6-4133497fda28@s28g2000vbp.googlegroups.com... > Hi All, > > I have a Listview that shows some records from a database. > When I click on one of the files in the Listview the details are shown > in a subwindow on my form. > Now when I add a new record by DragDrop on the Listview the view gets > updated so the newly added > file is shown. > In the subwindow I can then edit the details of the record. But when I > edit a newly added record and click > save I get an error message: > > InvalidArgument = Value of '6' is not valid for 'index'. > > The next code saves the details of the record in the Subwindow: > > Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles btnSave.Click > > Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & > IDclicked > Dim da As New OleDb.OleDbDataAdapter(sql, conn) > Dim cb As New OleDb.OleDbCommandBuilder(da) > Dim ds As New DataSet > conn.Open() > Try > da.SelectCommand = New OleDb.OleDbCommand(sql, conn) > da.Fill(ds) > If ds.Tables.Count > 0 _ > AndAlso ds.Tables(0).Rows.Count > 0 Then > Dim dr As DataRow = ds.Tables(0).Rows(0) > dr.Item("kenmerk") = tbKenmerk.Text > dr.Item("status") = tbStatus.Text > dr.Item("auteur") = tbAuteur.Text > dr.Item("datum_gewijzigd") = tbGewijzigd.Text > dr.Item("expires") = tbVerloopt.Text > dr.Item("hddlokatie") = tbFysiekeLokatie.Text > dr.Item("samenvatting") = tbSamenvatting.Text > dr.Item("soort") = tbSoort.Text > dr.Item("rubricering") = tbRubricering.Text > dr.Item("inuit") = tbInUit.Text > > 'sent the updated dataSet to the database > da.Update(ds) > UpdateListViewNode(IDclicked, dr) > End If > Catch oException As OleDbException > MessageBox.Show(oException.Message) > > Catch oException As Exception > MessageBox.Show(oException.Message) > > End Try > conn.Close() > EnableAllFields(False) > btnSave.Visible = False > btnCancel.Visible = False > btnExploreFysiekeLokatie.Visible = False > tbSamenvatting.ForeColor = Color.Gray > tbVerloopt.ReadOnly = True > End Sub > > The next code updates the Listview and raises the error message: > > Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As > DataRow) > > Dim Key As String = CStr(ID) > If ListView.Items.ContainsKey(Key) Then > Dim lvItem As ListViewItem = ListView.Items(Key) > lvItem.Text = CStr(Dr.Item("filenaam")) > lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) > lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) > lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) > lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) > lvItem.SubItems(5).Text = FixNull(Dr.Item > ("datum_gewijzigd")) > lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) > lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) > lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) > End If > > End Sub > > Can anyone tell me why this error occures? > > Regards > Marco > The Netherlands (Not SA).
Show quote
Hide quote
On 17 mei, 22:19, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> Thanks for the advice Cor but I use a Listview.wrote: > Co, > > The answer is simple, use a DataGridView and remove the vertical header > column and set some more properties to by instance remove the add row > button, than you have a better listview, which you simply can use with a > datasource for your datatable. > > Cor > > "Co" <vonclausow***@gmail.com> wrote in message > > news:674a145e-70a5-4e2e-96b6-4133497fda28@s28g2000vbp.googlegroups.com... > > > Hi All, > > > I have a Listview that shows some records from a database. > > When I click on one of the files in the Listview the details are shown > > in a subwindow on my form. > > Now when I add a new record by DragDrop on the Listview the view gets > > updated so the newly added > > file is shown. > > In the subwindow I can then edit the details of the record. But when I > > edit a newly added record and click > > save I get an error message: > > > InvalidArgument = Value of '6' is not valid for 'index'. > > > The next code saves the details of the record in the Subwindow: > > > Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles btnSave.Click > > > Dim sql As String = "SELECT * FROM Bestanden WHERE Id=" & > > IDclicked > > Dim da As New OleDb.OleDbDataAdapter(sql, conn) > > Dim cb As New OleDb.OleDbCommandBuilder(da) > > Dim ds As New DataSet > > conn.Open() > > Try > > da.SelectCommand = New OleDb.OleDbCommand(sql, conn) > > da.Fill(ds) > > If ds.Tables.Count > 0 _ > > AndAlso ds.Tables(0).Rows.Count > 0 Then > > Dim dr As DataRow = ds.Tables(0).Rows(0) > > dr.Item("kenmerk") = tbKenmerk.Text > > dr.Item("status") = tbStatus.Text > > dr.Item("auteur") = tbAuteur.Text > > dr.Item("datum_gewijzigd") = tbGewijzigd.Text > > dr.Item("expires") = tbVerloopt.Text > > dr.Item("hddlokatie") = tbFysiekeLokatie.Text > > dr.Item("samenvatting") = tbSamenvatting.Text > > dr.Item("soort") = tbSoort.Text > > dr.Item("rubricering") = tbRubricering..Text > > dr.Item("inuit") = tbInUit.Text > > > 'sent the updated dataSet to the database > > da.Update(ds) > > UpdateListViewNode(IDclicked, dr) > > End If > > Catch oException As OleDbException > > MessageBox.Show(oException.Message) > > > Catch oException As Exception > > MessageBox.Show(oException.Message) > > > End Try > > conn.Close() > > EnableAllFields(False) > > btnSave.Visible = False > > btnCancel.Visible = False > > btnExploreFysiekeLokatie.Visible = False > > tbSamenvatting.ForeColor = Color.Gray > > tbVerloopt.ReadOnly = True > > End Sub > > > The next code updates the Listview and raises the error message: > > > Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As > > DataRow) > > > Dim Key As String = CStr(ID) > > If ListView.Items.ContainsKey(Key) Then > > Dim lvItem As ListViewItem = ListView.Items(Key) > > lvItem.Text = CStr(Dr.Item("filenaam")) > > lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) > > lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) > > lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) > > lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) > > lvItem.SubItems(5).Text = FixNull(Dr.Item > > ("datum_gewijzigd")) > > lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) > > lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) > > lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) > > End If > > > End Sub > > > Can anyone tell me why this error occures? > > > Regards > > Marco > > The Netherlands (Not SA). MArco Co wrote:
> In the subwindow I can then edit the details of the record. But when I It would help if you said exactly which line generates the error.> edit a newly added record and click > save I get an error message: > > InvalidArgument = Value of '6' is not valid for 'index'. > > Can anyone tell me why this error occures? If you edit an item that isn't at the end of the list of records, does it update the wrong one, indicating an off-by-one error? Andrew
Show quote
Hide quote
On 18 mei, 12:03, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote: Andres,> Co wrote: > > In the subwindow I can then edit the details of the record. But when I > > edit a newly added record and click > > save I get an error message: > > > InvalidArgument = Value of '6' is not valid for 'index'. > > > Can anyone tell me why this error occures? > > It would help if you said exactly which line generates the error. > > If you edit an item that isn't at the end of the list of records, does it > update the wrong one, indicating an off-by-one error? > > Andrew It does update the right record but I think it has something to do with loading the Listview. This code produces the error: Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As DataRow) Dim Key As String = CStr(ID) If ListView.Items.ContainsKey(Key) Then Dim lvItem As ListViewItem = ListView.Items(Key) lvItem.Text = CStr(Dr.Item("filenaam")) lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) lvItem.SubItems(5).Text = FixNull(Dr.Item ("datum_gewijzigd")) lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) End If End Sub Not sure which line but I think it has to do with the SubItems. Wild guess... Marco Co wrote:
> On 18 mei, 12:03, "Andrew Morton" wrote: <snip>>> Co wrote: >>> In the subwindow I can then edit the details of the record. But >>> when I edit a newly added record and click >>> save I get an error message: >> >>> InvalidArgument = Value of '6' is not valid for 'index'. >> >>> Can anyone tell me why this error occures? >> >> It would help if you said exactly which line generates the error. > > This code produces the error: > > Not sure which line but I think it has to do with the SubItems. Wild guesses will not help. Have you tried using the debugger? Then you can > Wild guess... see exactly where the error comes from and, if it were relevant, examine things like lvItem to see if it has the subitems you think it has. Andrew
Show quote
Hide quote
On 18 mei, 15:04, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote: Andrew,> Co wrote: > > On 18 mei, 12:03, "Andrew Morton" wrote: > >> Co wrote: > >>> In the subwindow I can then edit the details of the record. But > >>> when I edit a newly added record and click > >>> save I get an error message: > > >>> InvalidArgument = Value of '6' is not valid for 'index'. > > >>> Can anyone tell me why this error occures? > > >> It would help if you said exactly which line generates the error. > > > This code produces the error: > > <snip> > > > Not sure which line but I think it has to do with the SubItems. > > Wild guess... > > Wild guesses will not help. Have you tried using the debugger? Then you can > see exactly where the error comes from and, if it were relevant, examine > things like lvItem to see if it has the subitems you think it has. > > Andrew I tested the code with the debugger and found the following. First the Listview gets loaded through this code: Public Sub LoadListView(ByVal tblID As Integer) ' Add items to the listview based on the selected item in the treeview Dim lvItem As ListViewItem ListView.Items.Clear() Dim sql As String = "SELECT * FROM Bestanden WHERE lokatie=" & tblID Dim cmd As New OleDbCommand(sql, conn) Dim dr As OleDbDataReader conn.Open() dr = cmd.ExecuteReader() Do While (dr.Read()) Dim Key As String = CStr(dr.Item("Id")) Dim ImgIndex As Integer = CInt(dr.Item("extensie")) lvItem = ListView.Items.Add(Key, CStr(dr.Item ("filenaam")), ImgIndex) lvItem.Tag = dr.Item("Id") lvItem.SubItems.AddRange(New String() { _ FixNull(dr.Item("kenmerk")), _ CStr(dr.Item("grootte")), _ CStr(dr.Item("auteur")), _ CStr(dr.Item("datum_gemaakt")), _ FixNull(dr.Item("datum_gewijzigd")), _ FixNull(dr.Item("status")), _ FixNull(dr.Item("soort")), _ FixNull(dr.Item("inuit"))}) Loop ListView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize) dr.Close() conn.Close() EmptyAllFields() End Sub Then I add a new record and immediately click Edit. I save the record and then get the error I mentioned, here: Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As DataRow) Dim Key As String = CStr(ID) If ListView.Items.ContainsKey(Key) Then Dim lvItem As ListViewItem = ListView.Items(Key) lvItem.Text = CStr(Dr.Item("filenaam")) lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) lvItem.SubItems(5).Text = FixNull(Dr.Item ("datum_gewijzigd")) lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) -------> This line the error occures!!!! lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) End If End Sub Regards Marco Co wrote:
Show quoteHide quote > Dim Key As String = CStr(ID) The SubItems probably start with an index of 0, not 1, i.e. they go from 0 > If ListView.Items.ContainsKey(Key) Then > Dim lvItem As ListViewItem = ListView.Items(Key) > lvItem.Text = CStr(Dr.Item("filenaam")) > lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) > lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) > lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) > lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) > lvItem.SubItems(5).Text = FixNull(Dr.Item > ("datum_gewijzigd")) > lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) > lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) > -------> This line the error occures!!!! > lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) > End If to 7, not 1 to 8. Andrew
Show quote
Hide quote
On 18 mei, 17:07, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote: Andrew,> Co wrote: > > Dim Key As String = CStr(ID) > > If ListView.Items.ContainsKey(Key) Then > > Dim lvItem As ListViewItem = ListView.Items(Key) > > lvItem.Text = CStr(Dr.Item("filenaam")) > > lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) > > lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) > > lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) > > lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) > > lvItem.SubItems(5).Text = FixNull(Dr.Item > > ("datum_gewijzigd")) > > lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) > > lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) > > -------> This line the error occures!!!! > > lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) > > End If > > The SubItems probably start with an index of 0, not 1, i.e. they go from 0 > to 7, not 1 to 8. > > Andrew Tried it but the error stays and the text get stored in the wrong field of the Listview, so it ain't that. MArco On 18 mei, 18:26, Co <vonclausow***@gmail.com> wrote:
Show quoteHide quote > On 18 mei, 17:07, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote: I tried following code but the error stays the same:> > > > > Co wrote: > > > Dim Key As String = CStr(ID) > > > If ListView.Items.ContainsKey(Key) Then > > > Dim lvItem As ListViewItem = ListView.Items(Key) > > > lvItem.Text = CStr(Dr.Item("filenaam")) > > > lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) > > > lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) > > > lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) > > > lvItem.SubItems(4).Text = CStr(Dr.Item("datum_gemaakt")) > > > lvItem.SubItems(5).Text = FixNull(Dr.Item > > > ("datum_gewijzigd")) > > > lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) > > > lvItem.SubItems(7).Text = FixNull(Dr.Item("soort")) > > > -------> This line the error occures!!!! > > > lvItem.SubItems(8).Text = FixNull(Dr.Item("inuit")) > > > End If > > > The SubItems probably start with an index of 0, not 1, i.e. they go from 0 > > to 7, not 1 to 8. > > > Andrew > > Andrew, > > Tried it but the error stays and the text get stored in the wrong > field of the Listview, so it ain't that. > > MArco Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As DataRow) Dim Key As String = CStr(ID) If ListView.Items.ContainsKey(Key) Then Dim lvItem As ListViewItem = ListView.Items(Key) lvItem.Text = CStr(Dr.Item("filenaam")) If lvItem.SubItems.Count > 0 Then lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) lvItem.SubItems(2).Text = CStr(Dr.Item("grootte")) lvItem.SubItems(3).Text = CStr(Dr.Item("auteur")) lvItem.SubItems(4).Text = CStr(Dr.Item ("datum_gemaakt")) lvItem.SubItems(5).Text = FixNull(Dr.Item ("datum_gewijzigd")) lvItem.SubItems(6).Text = FixNull(Dr.Item("status")) If lvItem.SubItems(7).Text <> "" Then lvItem.SubItems(7).Text = FixNull(Dr.Item ("soort")) End If If lvItem.SubItems(8).Text <> "" Then lvItem.SubItems(8).Text = FixNull(Dr.Item ("inuit")) End If End If End If End Sub The SubItems.Count value = 7 when I run it. MArco Co wrote:
> I tried following code but the error stays the same: <snip>> > Private Sub UpdateListViewNode(ByVal ID As Integer, ByVal Dr As > DataRow) > > Dim Key As String = CStr(ID) > If ListView.Items.ContainsKey(Key) Then > Dim lvItem As ListViewItem = ListView.Items(Key) > lvItem.Text = CStr(Dr.Item("filenaam")) > If lvItem.SubItems.Count > 0 Then > lvItem.SubItems(1).Text = FixNull(Dr.Item("kenmerk")) > That tells you that the subitems go from 0 to 6, i.e. there are 7 of them.> The SubItems.Count value = 7 when I run it. > Whereever you've added the subitems to it, you apparently haven't added all the ones you intended. Andrew
Can you edit auto generated Partial Class Designer VB code?
create reference number based on old one... Send Email Using VB 2008 Express Detecting Design Time vs Run Time Property Set action Problem with turning off Appication Frameworks and with posting al Test - dont bother to read Drawing images in asp.net Vb.net[2008] Combo box population from text file (40,000 lines!) BigInteger and BigDecimal [equivalents] for VB.NET? Running App in design environment is very slow |
|||||||||||||||||||||||