Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Property 'item' is 'Read Only' error

Posted on 2013-12-11
13
Medium Priority
?
438 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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