hi. i just want to ask how can i populate a listview n vb.net that has a is limited by a date from and date to. i have a datetimepicker for start date and datetimepicker for end date and i want do list all the entry of my database in listview that is within the range of the start date and end date. thanks in advance

Are you having a problem with the SQL part, the ListView part, or both? My SQL is rusty so I'll leave that to the experts as I have no SQL implementation to play with, however, the ListView part is something I can help you with. What specifically are you having a problem with? Is it configuring the listview columns, adding the listview items, etc?

One of the ways is to create a SELECT command for your database with the range in the select clause.
With this command you can create a datareader to read row-by-row from your database and using each row, add a listview item (with subitems if needed) until no more data is returned from the database, then close the reader.

The select clause can be some thing like: SELECT * FROM YourTable WHERE TheDateField BETWEEN '2011-03-10' AND '2011-03-22' ORDER BY TheDateField ASC; In VB you can create the string for the command using:

Dim strCmd as String = "SELECT * FROM YourTable WHERE TheDateField BETWEEN '" & _
        Format(DatePickerFrom.SelectedDate,"yyyy-MM-dd") & "' AND '" & _
        Format(DatePickerTo.SelectedDate,"yyyy-MM-dd") & "' ORDER BY TheDateField ASC;

I let to you to verify that the selected to date is greater than the selected from date.

Hope this helps

Then if your recordset is returned in the variable "rec" you can do

do until rec.EOF
   dim item as new ListViewItem(rec(0).Value)
   item.SubItems.Add(rec(1).Value)
   item.SubItems.Add(rec(2).Value)
   .
   .
   .
   lvwListView.Items.Add(item)
   rec.MoveNext()
loop

rec.Close()

Or you can use the field names instead if a field index, that is, rec("FIRST_NAME").Value. And forgive me if my SQL syntax is a little rusty.

thanks for your replies:) reverend jim, and lolafuertes.these will be helpful! thank you

hi. i just want to ask how can i populate a listview n vb.net that has a is limited by a date from and date to. i have a datetimepicker for start date and datetimepicker for end date and i want do list all the entry of my database in listview that is within the range of the start date and end date. thanks in advance

Database is probably the cleanest way, but you could also load asll the data into an object derived from an IList, then use LINQ to limit the data, and bind that also?

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.