2
Answers

filter listview

Photo of Violeta Popa

Violeta Popa

12y
1.7k
1
hi :)

i want to display in a listview only the insurances that have the expiration day between two dates, here's my code...please tell me what's wrong :(

            DateTime date1 = dateTimePicker1.Value;
            DateTime date2 = dateTimePicker2.Value;

            listView1.Items.Clear();
            SqlCommand cmd = new SqlCommand("SELECT * FROM client JOIN obtine ON client.id_client=obtine.id_client JOIN polita ON obtine.cod_polita=polita.cod_polita", conn);
            SqlDataReader dataReader = cmd.ExecuteReader();
            
            while (dataReader.Read())
            {
                string[] array = dataReader["perioada_asigurare"].ToString().Split('-');
                DateTime dt1 = DateTime.Parse(array[1]);

                //int result1=DateTime.Compare(date1,dt1);     i tried this too
                //int result2 = DateTime.Compare(date2, dt1);

                if (dt1>=date1 && dt1 <= date2)
                {
                    ListViewItem item = new ListViewItem(dataReader["denumire"].ToString());
                    item.SubItems.Add(dataReader["societate"].ToString());
                    item.SubItems.Add(dataReader["tip_polita"].ToString());
                    item.SubItems.Add(dataReader["serie"].ToString());
                    

                    listView1.Items.Add(item);
                }
            }

Answers (2)

0
Photo of Violeta Popa
NA 137 110.7k 12y
actually i solved my problem..the problem was that when comparing the dates, it compared the time too, so i used

if (dt1.Date >= date1.Date && dt1.Date <= date2.Date)
0
Photo of Kush Muchaal
NA 597 88.2k 12y
NICE ANSWER. Its great & help me.