Link to home
Start Free TrialLog in
Avatar of peter-cooper
peter-cooper

asked on

Property 'item' is 'Read Only' error

I am getting the above error when I run my code. I think this is because the DataReader is Read-Only and therefore unable to amend the value.

As a new user still getting to grips with vb.Net coming from php/mysql background, can someone show me the correct way to code this please or offer an alternative method. Many thanks

While dr.Read()
        If dr.HasRows Then

          lvRequests.Items.Add((dr(0)).ToString()).UseItemStyleForSubItems = False
          lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(CDate(dr(5)).ToShortDateString())
          lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(1).ToString())
          With lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(3).ToString())
             If dr(3) Is "D" Then    
                dr(3) = "destroyed"  <--- ERROR

             ElseIf dr(3) Is "O" Then
                dr(3) = "out"        <--- ERROR

             End If

           End With

           lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(9).ToString())

           lvcount += 1
         End If

    End While

Open in new window

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

when iterating using While.Read(), each dr is readonly so u can't modify it.
 so instead of:
 With lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(3).ToString())
             If dr(3) Is "D" Then    
                dr(3) = "destroyed"  <--- ERROR

             ElseIf dr(3) Is "O" Then
                dr(3) = "out"        <--- ERROR

             End If

           End With

Open in new window



use:

 
lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(3).ToString())
dim val = lvRequests.Items(lvRequests.Items.Count - 1).SubItems(0).ToString()

             If val Is "D" Then    
                lvRequests.Items(lvRequests.Items.Count - 1).SubItems(0) = "destroyed"  <--- ERROR

             ElseIf val Is "O" Then
                lvRequests.Items(lvRequests.Items.Count - 1).SubItems(0) = "out"        <--- ERROR

             End If

           End With

Open in new window

Avatar of peter-cooper
peter-cooper

ASKER

Sedgewick

Thanks for reply. However, no changes are made and the columd still shows D or O instead of the amendments. Is my code correct. Thanks

Using dr = oledbCmd.ExecuteReader()


                'clear items in the list before populating with new values
                lvRequests.Items.Clear()

                While dr.Read()
                    If dr.HasRows Then

                        lvRequests.Items.Add((dr(0)).ToString()).UseItemStyleForSubItems = False
                        lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(CDate(dr(5)).ToShortDateString())
                        lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(1).ToString())
                        With lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(3).ToString())
                            Dim val = lvRequests.Items(lvRequests.Items.Count - 1).SubItems(3).ToString()

                            If val Is "D" Then
                                lvRequests.Items(lvRequests.Items.Count - 1).SubItems(3).Text = "destroyed"

                            ElseIf val Is "O" Then
                                lvRequests.Items(lvRequests.Items.Count - 1).SubItems(3).Text = "out"

                            End If

                        End With

                        lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(9).ToString())

                        lvcount += 1
                    End If

                End While
            End Using

Open in new window

I am getting error of the following. Thanks

Error      1      Value of type 'String' cannot be converted to 'System.Windows.Forms.ListViewItem.ListViewSubItem'
which line?
In my amended code, line 17. I know the value is being passed correctly because in debugger, it is showing as:

val      "ListViewSubItem: {D}"      String
change
Dim val = lvRequests

Open in new window

to
Dim val as string = lvRequests

Open in new window

still the same. I have posted updated code. Thanks

While dr.Read()
             If dr.HasRows Then

                 lvRequests.Items.Add((dr(0)).ToString()).UseItemStyleForSubItems = False
                 lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(CDate(dr(5)).ToShortDateString())
                  lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(1).ToString())
                  With lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(3).ToString())
                  Dim val As String = lvRequests.Items(lvRequests.Items.Count - 1).SubItems(3).ToString()

                      If val Is "D" Then
                                lvRequests.Items(lvRequests.Items.Count - 1).SubItems(3).Text = "destroyed"

                      ElseIf val Is "O" Then
                         lvRequests.Items(lvRequests.Items.Count - 1).SubItems(3).Text = "out"

                       End If

                   End With

                      lvRequests.Items(lvRequests.Items.Count - 1).SubItems.Add(dr(9).ToString())

                        lvcount += 1
                    End If

                End While

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Option Strict disallows = Thanks
How did you arrive at that conclusion? If you'd like proof:

User generated image
well, if I use = it informs me to use Is. Hmmm, something weird happening here.
The reason why Is is not appropriate is because its job is to compare references, not compare values. There is no guarantee in your code that string held in val will be refer to the same string (in memory) as "D", for example. This what my screenshot demonstrates. As I alluded to above, you are a looking for value equality (the equality operator), not reference equality (the Is operator).
Thank you