#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Essex, ON
    Posts
    18
    Rep Power
    0

    populating a listview from a database


    I am trying to populate a ListView called ListView1 with data from my ttimeoff table. I have included some code. The summary click event is to bring up all of the days off from ttimeoff for the name selected in the combobox1. I got the listview1 form load event right because all of the proper columns show up. I can't seem to make any head way populating it. I just need to know if I am on the right track or if anyone can point me in the right direction.

    Private Sub ListView1Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'add columns

    ListView1.Columns.Add("USERID", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("SICK", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("LOA", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("SUSP", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("PPH", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("PAA", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("BER", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("AWOL", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("VAC", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("EMERG", 70, HorizontalAlignment.Center)
    ListView1.Columns.Add("MATERN", 70, HorizontalAlignment.Center)


    End Sub

    Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
    Dim userid As String
    Dim datesick As Date
    Dim datebereave As Date
    Dim datesusp As Date
    Dim datemat As Date
    Dim dateloa As Date
    Dim datepph As Date
    Dim datepaa As Date
    Dim dateemerg As Date
    Dim datevac As Date
    Dim dateabsent As Date

    Dim Item As String
    Dim ListViewItem As ListViewItem

    SQL = "Select * FROM ttimeoff WHERE USERID = '" & Trim(UCase("" & ComboBox1.SelectedItem())) & "'"

    Try
    ListView1.Items.Clear()
    SQL = "Select * FROM ttimeoff WHERE USERID = '" & Trim(UCase("" & ComboBox1.SelectedItem())) & "'"
    For Each Item In ComboBox1.Items
    ListViewItem = New ListViewItem(Item.ToString)
    ListViewItem.SubItems.Add(Rs.Fields(userid).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datesick).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datebereave).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datesusp).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datemat).Value)
    ListViewItem.SubItems.Add(Rs.Fields(dateloa).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datepph).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datepaa).Value)
    ListViewItem.SubItems.Add(Rs.Fields(dateemerg).Value)
    ListViewItem.SubItems.Add(Rs.Fields(datevac).Value)
    ListViewItem.SubItems.Add(Rs.Fields(dateabsent).Value)
    ListView1.Items.Add(ListViewItem)
    Next

    Catch ES As Exception
    MsgBox(ES.Message)
    End Try
    End Sub

    Thank you,
    SLCONSULT
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Montreal, Canada
    Posts
    486
    Rep Power
    12
    it's look good IMO

    but I just don't understand where is your problem and your code doesn't do that it is suppose to do.
    you want to make a header or something like that ?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Essex, ON
    Posts
    18
    Rep Power
    0

    populating a database


    Thanks for your reply. The problem lies that the event is not drawing the data from my table ttimeoff. The listview is simply listing all the members of my combobox1 and the date of 1/1/0000 in the columns. I can't figure out how to get it to populate from my database. I am using SQL as my database.

    thanks again,
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Montreal, Canada
    Posts
    486
    Rep Power
    12
    Has I can see you do not connect yourself to your database. I suggest you use ADO for it cause in .Net the dao is kinda slow and it doesn't automaticly save the modifications.

    It's kinda hard to explain exaclty how it's work. I used it a lof in vb6 but in .net things are a little bit different and I am still learning about it.

    I suggest you search about it on www.planetsourcecode.com and www.google.com/microsoft
    those 2 website helped me when I got some problems with the code I am correcting right now.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    Hehe, nice little bug :-)

    Code:
    Dim userid As String 
    Dim datesick As Date 
    Dim datebereave As Date 
    ...
    ListViewItem.SubItems.Add(Rs.Fields(userid).Value) 
    ListViewItem.SubItems.Add(Rs.Fields(datesick).Value) 
    ListViewItem.SubItems.Add(Rs.Fields(datebereave).Value) 
    ListViewItem.SubItems.Add(Rs.Fields(datesusp).Value) 
    ListViewItem.SubItems.Add(Rs.Fields(datemat).Value) 
    ListViewItem.SubItems.Add(Rs.Fields(dateloa).Value)
    This *can't* work since you never initialize those variables since the above "ListViewItem.SubItems..." statements evaluated always look like this:

    Code:
     
    ' "ListViewItem.SubItems.Add(Rs.Fields(userid).Value)" becomes
    ListViewItem.SubItems.Add(Rs.Fields("").Value)
    
    ' "ListViewItem.SubItems.Add(Rs.Fields(datesick).Value)" becomes 
    ListViewItem.SubItems.Add(Rs.Fields("").Value)
    
    ' and so on...
    I guess you meant:

    Code:
    ListViewItem.SubItems.Add(Rs.Fields("userid").Value) 
    ListViewItem.SubItems.Add(Rs.Fields("datesick").Value) 
    ListViewItem.SubItems.Add(Rs.Fields("datebereave").Value) 
    ListViewItem.SubItems.Add(Rs.Fields("datesusp").Value)

IMN logo majestic logo threadwatch logo seochat tools logo