Solved

Property 'item' is 'Read Only' error

Posted on 2013-12-11
13
428 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
13 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39710938
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

0
 

Author Comment

by:peter-cooper
ID: 39710954
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

0
 

Author Comment

by:peter-cooper
ID: 39711154
I am getting error of the following. Thanks

Error      1      Value of type 'String' cannot be converted to 'System.Windows.Forms.ListViewItem.ListViewSubItem'
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 42

Expert Comment

by:sedgwick
ID: 39711159
which line?
0
 

Author Comment

by:peter-cooper
ID: 39711189
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
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39711201
change
Dim val = lvRequests

Open in new window

to
Dim val as string = lvRequests

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 39711284
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

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39713088
The Is operator isn't appropriate here. You should use the equality operator instead.

If val = "D" Then
...

Open in new window


Which line has the error now?
0
 

Author Comment

by:peter-cooper
ID: 39713739
Option Strict disallows = Thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39714201
How did you arrive at that conclusion? If you'd like proof:

Screenshot
0
 

Author Comment

by:peter-cooper
ID: 39714235
well, if I use = it informs me to use Is. Hmmm, something weird happening here.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39714247
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).
0
 

Author Closing Comment

by:peter-cooper
ID: 39714256
Thank you
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question