Hi all

I've been struggling with this one for some time now and I would realy appreciate some help.

I have a string which gets data from mssql server, reader collects data (among others also "person)

SQLStr = "select * from tablename order by id desc

command = New System.Data.SqlClient.SqlCommand(SQLStr, connection)
reader = command.ExecuteReader()

While reader.Read()
ListView1SelectedItem = reader.Item("PERSON")

List view contains list of persons (with IDs), including "person" from the reader.

What I want is that item from reader.Item("PERSON") is selected/highlighted in the listview.

I've started from this:

ListView1.Items(0).Selected = True
ListView1.Select()
ListView1.EnsureVisible(0)

but it always select first item in list view and not the actual reader.item.

Anybody has an idea how to do this?

tnx

Recommended Answers

All 8 Replies

This should help you.

ListView1.Items(reader.Item("PERSON")).Selected = True

This will only select the VERY first item

ListView1.Items(0).Selected = True

Hi, I have tried this before but all I got is this error:

NuulreferenceExeption was unhandeled-object variable or with block variable not set

don't know how to handle this...?

you forgot the word Add....

it shud be Listview1.Items.Add and then the reader....

    Try
        Dim myCommand As SqlCommand
        myCommand = New SqlCommand("SELECT  * FROM DoctorRegister ", Connection)
        Dim reader As SqlDataReader = myCommand.ExecuteReader
        While reader.Read
            ListView1.Items.Add(reader.Item("FirstName")).Selected = True
        End While
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

selectedItem is not the property for listview.....

ListView1SelectedItem = reader.Item("PERSON")

Assuming that the item is not already present in the ListView. :)

@poojavb; I don't want to add any items to listview-it allready contains items that are read from sql db.
Every name is writen in db just once, but there can be multiple "records" containing that name but with a different ID.
And by that ID, I am quering from db. I want to select that item/person in list view which is contained in this record.

On form load, the list view already contains list of names...
when entering record ID number in TB9, the form loads also other data (all in same table) which belont to same redord/ID.

Here is some code to make it more clear:

Private Sub TextBox9_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox9.KeyDown

        If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then

            Dim SQLStr As String = ""
            Dim connection As New System.Data.SqlClient.SqlConnection("Data Source")
            Dim command As New System.Data.SqlClient.SqlCommand(SQLStr, connection)

            'Read from SQL
            connection.Open()

            Dim reader As System.Data.SqlClient.SqlDataReader

            If TextBox9.Text.ToString = Nothing Then

                SQLStr = "select top 1 * from table.dbo.table order by id desc"

            ElseIf e.KeyCode = Keys.Left And TextBox9.Text.ToString = Nothing Then

                SQLStr = "select top 1 * from table.dbo.table order by id desc" 

            ElseIf e.KeyCode = Keys.Right And Not TextBox9.Text.ToString = Nothing Then

                SQLStr = "select * from table.dbo.table where id = " & CDbl(TextBox9.Text) + 1

            ElseIf e.KeyCode = Keys.Left And Not TextBox9.Text.ToString = Nothing Then

                SQLStr = "select * from table.dbo.table where id = " & CDbl(TextBox9.Text) - 1

            ElseIf e.KeyCode = Keys.Return Then

                SQLStr = "select * from table.dbo.table where id = " & CDbl(TextBox9.Text)

            End If

            command = New System.Data.SqlClient.SqlCommand(SQLStr, connection)
            reader = command.ExecuteReader()

            While reader.Read()

                TextBox9.Text = reader.Item("ID")
                Me.Controls("TextBox3").Text = reader.Item("ORDER")
                ComboBox1.SelectedItem = reader.Item("INTENTION")
                Me.Controls("TextBox8").Text = reader.Item("DESCRIPTION")
                DateTimePicker1.Value = reader.Item("DEPARTURE")
                DateTimePicker2.Value = reader.Item("ARRIVAL")
                ListView1SelectedItem = reader.Item("PERSON")

                '??'
            End While

            If Not reader.HasRows Then

                MessageBox.Show("record with ID " & TextBox9.Text & "doesn't exists!")

            End If

            ElseIf e.KeyCode = Keys.Left Then
            MessageBox.Show("back key")
            End If
End Sub

Then you should use the code I gave you.

ListView1.Items(reader.Item("PERSON")).Selected = True

Or at least read into a String variable first.

Dim strPERSON As String = reader.Item("PERSON").ToString()
ListView1.Items(strPERSON).Selected = True

I'm assuming that reader.Item("PERSON") contains a string of something that can be seen in the ListView.

Because as Poojavb said, SelectedItem is not a property of ListView.

Oxigen; you assume correct-reader.item("PERSON") is exact string as can be seen in the listview.

I have tried your code:

ListView1SelectedItem = reader.Item("PERSON")
MsgBox(ListView1SelectedItem)

Dim strPERSON As String = reader.Item("PERSON").ToString()
ListView1.Items(strPERSON).selected = True
ListView1.Items(reader.Item("PERSON")).selected = True

but all I'm getting is error on line

ListView1.Items(strPERSON).selected = True

NullReferenceExeption was unhandeled
Object reference is not set to an instance of an object.

find a cure for my problem called FindItemWithText

 Me.ListView1.FindItemWithText(reader.Item("PERSON")).Selected = True
 Me.ListView1.FindItemWithText(reader.Item("PERSON")).EnsureVisible()
 ListView1.Select()

Works like a charm, tnx for help everybody! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.