Hi friends,
I have a list view which contains an id and a name.... upon selecting one row i should be able to view the details of that particular row.. i.e name, age, salary etc...Im using Visual studio .net 2003 and sql server 2005..

Dani AI

Generated

For : instead of parsing the text shown in the ListView (which is fragile and breaks if formatting changes), keep the record identifier with the ListViewItem and use that ID to load full details from SQL Server. That avoids string parsing, keeps the UI simple, and makes the selection handler predictable. 's idea of extracting values from the displayed line can work for very small throwaway apps, but it is brittle for real data.

Populate each row with visible columns and store the primary key in the item Tag (or attach a small object). Example when filling the ListView:

' when adding rows
Dim it As New ListViewItem(employeeName)
it.Tag = employeeId         ' store the DB id (hidden)
it.SubItems.Add(department) ' visible columns
it.SubItems.Add(location)
ListView1.Items.Add(it)

On selection, read the Tag and run a parameterized query to fetch details (or pull a cached object if you preloaded rows). Example selection handler:

If ListView1.SelectedItems.Count = 0 Then Exit Sub
Dim id As Integer = CInt(ListView1.SelectedItems(0).Tag)

Using cn As New SqlConnection(connectionString)
    Using cmd As New SqlCommand("SELECT Name,Age,Salary FROM Employees WHERE Id = @id", cn)
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id
        cn.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader()
            If rdr.Read() Then
                txtName.Text = rdr("Name").ToString()
                txtAge.Text = rdr("Age").ToString()
                txtSalary.Text = rdr("Salary").ToString()
            End If
        End Using
    End Using
End Using

Notes: use parameterized SQL to avoid injection, check SelectedItems.Count before accessing, set MultiSelect = False if you need single selection, and dispose connections properly. For better responsiveness, load details on demand in a background worker and marshal results back to the UI. This approach works cleanly in VS.NET 2003 / .NET 1.1 with SQL Server 2005.

Recommended Answers

All 3 Replies

In the selected index change or one of the click events of the list you should put.

dim strLine as strin 
strLine = List1.SelectedItem.Text

That will get you the line, from there you will need to do string splits & string manipulation on your data.

What is string split and how do we do it?

String splits allow you to easily retrieve values in a string. Lets say that the line that was read from the listview has been formated by putting a tab between the values. You would get the data like this;

'declare an array to hold the vals
dim listVals as String() = Nothing

'get the line from the list
strLine = List1.SelectedItem.Text

'get the vals from the string
listVals = strLine.Split(vbTab)

If the line wasn't formatted with tabs as it was added to the listview then you may have to use left, mid and right string functions.

Microsoft.VisualBasic.Right(string, length)
Microsoft.VisualBasic.Left(string, length)
Microsoft.VisualBasic.Mid(string, start pos, length)
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.