Solved

Property 'item' is 'Read Only' error

Posted on 2013-12-11
13
371 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 74

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 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now