?
Solved

Populating ListView From Database

Posted on 2006-05-25
2
Medium Priority
?
250 Views
Last Modified: 2010-04-23
Hi Experts,

I have a listview with (2) columns (Offense ID) and (Offense Description) and the items for these columns come from a database based on a scalar query.  I have the following so far and the first record comes through fine (displays the "Offense ID" and the "Offense Description"; however the second record only show the "Offense ID" and the "Offense Description" is blank.  Any ideas?  I have attached my code below, thanks in advance for your assistance:

        Dim conn2 As New System.Data.SqlClient.SqlConnection("Persist Security Info=True;Server=xxx.xxx.xxx.xxx;Database=Reporting;uid=uid;network=dbmssocn;password=password")
        Dim da2 As New System.Data.SqlClient.SqlDataAdapter("Select * from tblOffense", conn2)
        Dim ds2 As New System.Data.DataSet
        Dim cmd As New System.Data.SqlClient.SqlCommand("Select offense_number, offensedesc from tblOffense where incident_ID='" & IncidentPeopleAdd.Incident_ID.Text & "' and ARN='" & IncidentPeopleAdd.ARN.Text & "'", conn2)

        Dim loggedOnUser As String
        loggedOnUser = My.User.Name
        Dim cmd2 As New System.Data.SqlClient.SqlCommand("select (authorid)as authorid from tblAuthor where username='" & loggedOnUser & "'", conn2)


        Try
            conn2.Open()
            Dim myReader As SqlDataReader = cmd.ExecuteReader()
            Do While myReader.Read()


                ListView1.Items.Add(myReader.Item("offense_number")).ToString()
                ListView1.Items(0).SubItems.Add(myReader.Item("offensedesc")).ToString()


            Loop
            myReader.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn2.Close()
        End Try


The above code returns:

Offense ID                  Offense Description
      1                               Grand Theft
      2

0
Comment
Question by:NCSO
2 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 16767407
What your code is doing is adding the subitem every time to the same item - that is Items(0).  The alternative I suggest for these two lines

                ListView1.Items.Add(myReader.Item("offense_number")).ToString()
                ListView1.Items(0).SubItems.Add(myReader.Item("offensedesc")).ToString()

is

                Dim lvi As New ListViewItem(myReader.Item("offense_number").ToString)
                lvi.SubItems.Add(myReader.Item("offensedesc").ToString())
                ListView1.Items.Add(lvi)

Roger
0
 

Author Comment

by:NCSO
ID: 16769107
Very Good, would you mind one last thing?  I am awarding you the points and will increase them by 100 if you could tell me how to make label22.text equal the checked value of the listview.  

Ie.

Offense Number                 Offense Description
       1                                     Grand Theft
       2                                        DWLSR


Label22.text would equal 1,2
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 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