I am working on winforms, where I have 2 codes, working for the same thing, and they are same just the query difference but one is working completely fine and other one has issues as it misplace the images. the problem is everytime I run code, images are at wrong place. the correct functionality code:
int c2 = -1;
List<string> searchpath =new List<string>();
List<string> searchtitle = new List<string>();
listView2.Clear();
homerecipe.Clear();
searchtitle.Clear();
searchpath.Clear();
imageList3.Images.Clear();
var text = textBox1.Text;
char[] separator = { ' ' };
string[] words = null;
words = text.Split(separator);
foreach (string word in words)
{
try
{
cmd = new SqlCommand($"select Title, Thumbnail,RecipeName from RecipeInfo where RecipeName like '%{word}%'", con);
con.Open();
SqlDataReader read = cmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
// if (homerecipe.Any(item => item == read[2].ToString())) continue;
searchtitle.Add(read[0].ToString());
searchpath.Add($@"{read[1].ToString()}");
homerecipe.Add(read[2].ToString());
}
read.Close();
//con.Close();
foreach (string ipath in searchpath)
{
ListViewItem img = listView2.FindItemWithText(ipath);
if (img == null)
{
imageList3.Images.Add(Image.FromFile(ipath));
}
}
listView2.LargeImageList = imageList3;
foreach (string hometitle in searchtitle)
{
ListViewItem list = listView2.FindItemWithText(hometitle);
if (list == null)
{
c2++;
listView2.Items.Add(hometitle, c2);
}
}
}
con.Close();
}
catch (SqlException)
{
MessageBox.Show("masla");
con.Close();
//continue;
}
The problematic code:
int ccc = -1;
hometitles.Clear();
homepaths.Clear();
homerecipe.Clear();
imageList2.Images.Clear();
try
{
cmd = new SqlCommand("select Title, Thumbnail,RecipeName from RecipeInfo order by newid()", con); //generating random from sql
con.Open();
SqlDataReader reader1 = cmd.ExecuteReader();
if (reader1.HasRows)
{
while (reader1.Read())
{
hometitles.Add(reader1[0].ToString());
homepaths.Add($@"{reader1[1].ToString()}");
homerecipe.Add(reader1[2].ToString());
}
}
reader1.Close();
//con.Close();
foreach (string imagepath in homepaths)
{
ListViewItem img = listView2.FindItemWithText(imagepath);
if (img == null)
{
imageList2.Images.Add(Image.FromFile(imagepath));
}
}
listView2.LargeImageList = imageList2;
foreach (string hometitle in hometitles)
{
ListViewItem list = listView2.FindItemWithText(hometitle);
if (list == null)
{
ccc++;
listView2.Items.Add(hometitle,ccc);
}
}
con.Close();
}
catch(SqlException)
{
MessageBox.Show("error");
}
I have tried using the homerecipe elements as image key as they are primary key but I don't know how to give a condition in foreach that if one name entered then the same name don't come twice. for this, i was trying this
foreach (string imagepath in homepaths)
{
foreach(string name in homerecipe) //name a primary key
{
ListViewItem img = listView2.FindItemWithText(imagepath);
if (img == null)
{
MessageBox.Show(name);
imageList2.Images.Add(name,Image.FromFile(imagepath));
}
}
}
listView2.LargeImageList = imageList2;
foreach (string hometitle in hometitles)
{
foreach (string name in homerecipe)
{
ListViewItem list = listView2.FindItemWithText(hometitle);
if (list == null)
{
ccc++;
listView2.Items.Add(hometitle, name);
}
}
}
I am trying this for the last 3 days, please help me correct it. please I am in the deadline for my project but this issue is not resolving. I am new to programming please resolve this issue.
Sometimes, it happens that only images shuffle, sometimes both text and image but not correct positon