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
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
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
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
ASKER
I am getting error of the following. Thanks
Error 1 Value of type 'String' cannot be converted to 'System.Windows.Forms.ListViewItem.L istViewSub Item'
which line?
ASKER
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
toDim val as string = lvRequests
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Option Strict disallows = Thanks
ASKER
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).
ASKER
Thank you
so instead of:
Open in new window
use:
Open in new window