0

I'm creating a desktop chat app in which a user can search other users by name or email. When I search other users their images are not in a sequential manner.

// This is code from Home Form

    private void BtnSearch_Click(object sender, EventArgs e)
    {
        if (Friends.SelectedIndex == 0)
        {
            UsersClass.SearchContacts(listAllContacts, ImgListAllContacts, TxtSearch.Text.Trim());
        }
    }

// This is Code from UserClass

    public static void SearchContacts(ListView listview, ImageList imagelist, string searchkey)
    {
        DataTable dt = new DataTable();
        listview.Items.Clear();
        ListViewItem[] listviewitem = null;
        dt = DataBaseAccess.Retrive("select UID,FullName,DP from TKDBUsers WHERE ( FullName+' '+UserName ) Like '%" + searchkey + "%' AND UID != '" + LogInUser.UID + "'");
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                listviewitem = new ListViewItem[(dt.Rows.Count)];
                int LC = 0;        //List contacts
                foreach (DataRow item in dt.Rows)
                {
                    Image img = DataBaseAccess.Base64ToImage(Convert.ToString(item["DP"]));
                    imagelist.Images.Add(img);
                    listviewitem[LC] = new ListViewItem(new string[] { Convert.ToString(item["UID"]) + " - " + Convert.ToString(item["FullName"]) }, LC);
                    LC++;
                }
            }
        }
        if (listviewitem != null)
        {
            listview.Items.AddRange(listviewitem);
        }

enter image description here

enter image description here

Matt
  • 1,245
  • 2
  • 17
  • 32
  • What do you mean by "not in a sequential manner"? If you want them in a certain order (by name or whatever else), you can use an `order by` clause in your query. – Gabriel Negut Apr 23 '19 at 08:40
  • yes that's what I mean Order by " Name " – Muhammad Mansoor Ishaq Apr 23 '19 at 08:45
  • Did you try adding "Order by Name" in select query? – Matt Apr 23 '19 at 08:48
  • SQL Server doesn't not store data in any particular order or retrieve data in any particular order. The database is multi-threaded and performing same query can get different results every time. You must use an Order By in a query to make sure results come out in same order every time. – jdweng Apr 23 '19 at 08:49

2 Answers2

0

Don't you need to clear your imagelist ?

KiwiJaune
  • 530
  • 2
  • 16
0

Try changing the select query to (add "ORDER BY UserName"):

dt = DataBaseAccess.Retrive("
    SELECT UID, FullName, DP 
    FROM TKDBUsers 
    WHERE (FullName+' '+UserName ) LIKE '%" + searchkey + "%' 
    AND UID != '" + LogInUser.UID + "' 
    ORDER BY UserName");
Matt
  • 1,245
  • 2
  • 17
  • 32