Solved

Property 'item' is 'Read Only' error

Posted on 2013-12-11
13
388 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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