Results 1 to 7 of 7

Thread: Only add String as new Item to Listview if New

  1. #1

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Red face Only add String as new Item to Listview if New

    Using the following code to add results from sql to a listview. Is there anyway that I can make a comparison from the reader against all currently existing item strings? Thanks in advance.


    vb.net Code:
    1. LstResults.Items.Clear()
    2.         If con.State <> ConnectionState.Open Then
    3.             Try
    4.                 con.Close()
    5.             Catch ex As Exception
    6.  
    7.             End Try
    8.             con.Open()
    9.         End If
    10.         Dim comm As New SqlCommand("select * from maxcom WHERE LastName like '%" & TxtLastName.Text & "%' OR FirstName like '%" & TxtLastName.Text & "%'", con)
    11.         ' OR Position like '%" & TxtLastName.Text & "%'"
    12.         ' OR FirstName like '%" & TxtLastName.Text & "%' OR Position like '%" & TxtLastName.Text & "%' OR "
    13.         Try
    14.             Dim reader As SqlDataReader = comm.ExecuteReader()
    15.             While reader.Read()
    16.                 LstResults.Items.Add(reader("FirstName").ToString() & " " & reader("LastName").ToString() & "  " & reader("Extension").ToString() & "  " & reader("DIDNumber").ToString())
    17.                 LstResults.Items.Add("-----------------------------------------------------------------------------------------------------------------------------------------------------------------")
    18.             End While
    19.             reader.Close()
    20.         Catch ex As Exception
    21.  
    22.         End Try
    23.         If TxtLastName.Text.ToString.Contains(Chr(32)) Then
    24.             Dim line As String = TxtLastName.Text.ToString
    25.             Dim i As Integer
    26.             Dim aryTextFile() As String
    27.  
    28.             aryTextFile = line.Split(Chr(32))
    29.  
    30.             For i = 0 To UBound(aryTextFile)
    31.                 Dim comm2 As New SqlCommand("select * from maxcom WHERE LastName like '%" & aryTextFile(i).ToString & "%' OR FirstName like '%" & aryTextFile(i).ToString & "%'", con)
    32.                 Dim reader As SqlDataReader = comm2.ExecuteReader()
    33.                 While reader.Read()
    34.                     LstResults.Items.Add(reader("FirstName").ToString() & " " & reader("LastName").ToString() & "  " & reader("Extension").ToString() & "  " & reader("DIDNumber").ToString())
    35.                     LstResults.Items.Add("-----------------------------------------------------------------------------------------------------------------------------------------------------------------")
    36.                 End While
    37.                 reader.Close()
    38.  
    39.             Next i
    40.         End If
    41.         con.Close()
    42.         TxtLastName.Text = ""
    43.         TxtLastName.Focus()

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Only add String as new Item to Listview if New

    First up, you should generally avoid adding items to a control, particularly a ListView, in a loop. The preferred method of adding multiple items is to create all the items first and then add them in a batch by calling AddRange. If you really must add the items one by one then make sure that you call BeginUpdate on the ListView first and then EndUpdate at the end.

    As for the question, the simplest way to determine whether a ListView already contains an item displaying particular text is using LINQ, e.g.
    vb.net Code:
    1. If myListView.Items.Cast(Of ListViewItem)().Any(Function(lvi) lvi.Text = myString) Then
    2.     'myString is already contained in an item in the list.
    3. End If

  3. #3

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Only add String as new Item to Listview if New

    Getting an error:

    Unable to cast object of type 'System.String' to type 'System.Windows.Forms.ListViewItem'.

    vb Code:
    1. For i = 0 To UBound(aryTextFile)
    2.                 Dim comm2 As New SqlCommand("select * from maxcom WHERE LastName like '%" & aryTextFile(i).ToString & "%' OR FirstName like '%" & aryTextFile(i).ToString & "%'", con)
    3.                 Dim reader As SqlDataReader = comm2.ExecuteReader()
    4.                 While reader.Read()
    5.                     Dim text As String = reader("FirstName").ToString() & " " & reader("LastName").ToString() & "  " & reader("Extension").ToString() & "  " & reader("DIDNumber").ToString()
    6.                    
    7. If LstResults.Items.Cast(Of ListViewItem)().Any(Function(lvi) lvi.Text = (Text)) Then
    8.                         'myString is already contained in an item in the list.End If
    9.                     Else
    10.                         LstResults.Items.Add(text)
    11.                         LstResults.Items.Add("-----------------------------------------------------------------------------------------------------------------------------------------------------------------")
    12.                     End If
    13.                 End While
    14.                 reader.Close()

  4. #4

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Only add String as new Item to Listview if New

    I personally can't yet fully understand the syntax of the command:

    If LstResults.Items.Cast(Of ListViewItem)().Any(Function(lvi) lvi.Text = (Text))

    But rest assured I cannot execute that code. Text cannot be a member of the cast because text is a string and not a ListViewItem... or did i miss something?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Only add String as new Item to Listview if New

    Works fine for me. Just tested a ListView contain items with Text of "A", "B" and "C" with this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    4.         Dim values = New String() {"X", "B", "Z"}
    5.  
    6.         For Each value In values
    7.             If Not Me.ListView1.Items.Cast(Of ListViewItem)().Any(Function(lvi) lvi.Text = value) Then
    8.                 Me.ListView1.Items.Add(value)
    9.             End If
    10.         Next
    11.     End Sub
    12. End Class
    and two new items with Text of "X" and "Z" was added.

    The LINQ code gets the Items collection of ListView, casts each one as type ListViewItem and returns then as a list, then checks whether any of them return True when passed to a function that returns whether its Text is equal to the specified value. The example above uses Any and Not, but you could get the same effect by removing the Not, changing Any to All and inverting the equality comparison:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    4.         Dim values = New String() {"X", "B", "Z"}
    5.  
    6.         For Each value In values
    7.             If Me.ListView1.Items.Cast(Of ListViewItem)().All(Function(lvi) lvi.Text <> value) Then
    8.                 Me.ListView1.Items.Add(value)
    9.             End If
    10.         Next
    11.     End Sub
    12. End Class

  6. #6

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Only add String as new Item to Listview if New

    Rewrote the process could anyone please show me why it is not functioning?


    vb Code:
    1. Dim i As Integer = 0
    2.         Dim answer(0 To i) As String
    3.         LstResults.Items.Clear()
    4.         If con.State <> ConnectionState.Open Then
    5.             Try
    6.                 con.Close()
    7.             Catch ex As Exception
    8.  
    9.             End Try
    10.             con.Open()
    11.         End If
    12.         Dim comm As New SqlCommand("select * from maxcom WHERE LastName like '%" & TxtLastName.Text & "%' OR FirstName like '%" & TxtLastName.Text & "%'", con)
    13.         ' OR Position like '%" & TxtLastName.Text & "%'"
    14.         ' OR FirstName like '%" & TxtLastName.Text & "%' OR Position like '%" & TxtLastName.Text & "%' OR "
    15.         Try
    16.             Dim reader As SqlDataReader = comm.ExecuteReader()
    17.             Dim text As String = reader("FirstName").ToString() & " " & reader("LastName").ToString() & "  " & reader("Extension").ToString() & "  " & reader("DIDNumber").ToString()
    18.             While reader.Read()
    19.                 answer(i) = (reader("FirstName").ToString() & " " & reader("LastName").ToString() & "  " & reader("Extension").ToString() & "  " & reader("DIDNumber").ToString())
    20.                 i = i + 1
    21.                 answer(i) = "-----------------------------------------------------------------------------------------------------------------------------------------------------------------"
    22.                 i = i + 1
    23.             End While
    24.             reader.Close()
    25.             con.Close()
    26.         Catch ex As Exception
    27.  
    28.         End Try
    29.         If con.State <> ConnectionState.Open Then
    30.             Try
    31.                 con.Close()
    32.             Catch ex As Exception
    33.  
    34.             End Try
    35.             con.Open()
    36.         End If
    37.         If TxtLastName.Text.ToString.Contains(Chr(32)) Then
    38.             Dim line As String = TxtLastName.Text.ToString
    39.             Dim aryTextFile() As String
    40.  
    41.             aryTextFile = line.Split(Chr(32))
    42.             Dim t As Integer
    43.             For t = 0 To UBound(aryTextFile)
    44.                 Dim comm2 As New SqlCommand("select * from maxcom WHERE LastName like '%" & aryTextFile(i).ToString & "%' OR FirstName like '%" & aryTextFile(i).ToString & "%'", con)
    45.                 Dim reader2 As SqlDataReader = comm2.ExecuteReader()
    46.                 While reader2.Read()
    47.                     If answer.Any(Function(lvi) lvi <> (Text)) Then
    48.                         answer(i) = (reader2("FirstName").ToString() & " " & reader2("LastName").ToString() & "  " & reader2("Extension").ToString() & "  " & reader2("DIDNumber").ToString())
    49.                         i = i + 1
    50.                         answer(i) = "-----------------------------------------------------------------------------------------------------------------------------------------------------------------"
    51.                         i = i + 1
    52.                     Else
    53.                     End If
    54.                 End While
    55.                 reader2.Close()
    56.             Next t
    57.         End If
    58.         Do Until i < 0
    59.             LstResults.Items.Add(answer(i).ToString)
    60.             i = i - 1
    61.         Loop
    62.         con.Close()
    63.         TxtLastName.Text = ""
    64.         TxtLastName.Focus()

    Getting issue that:

    Dim reader2 As SqlDataReader = comm2.ExecuteReader() results in VVV
    There is already an open DataReader associated with this Command which must be closed first.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Only add String as new Item to Listview if New

    I think you'll find that the error message mentions the connection, not the command.

    The error message is quite clear on what the issue is: you can't open a data reader on a connection that already has an open data reader. I would say the cause is the fact that you are just ignoring exceptions rather than actually handling them. If exceptions are being thrown, you can't just add a Catch block and ignore it. Turning off the warning lights in your car won't stop the engine breaking down.

    At the moment, you open a data reader and then, if an exception is thrown, you don't close it. If you actually do catch an exception then you need to clean up in a Finally block, to ensure that it gets done whether an exception is thrown or not.

    If an exception is thrown, work out what it is and why it's being thrown and, if possible, remove the cause.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width