Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

<pre>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");
}



Sometimes, it happens that only images shuffle, sometimes both text and image but not correct positon.
I am sorry i dont know how to write question here

What I have tried:

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

<pre>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.
Posted
Updated 3-Sep-20 21:21pm

1 solution

For starters, don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app as a matter of very high priority.


The problem you have noticed though, there isn't a lot we can do - it depends on the DB and the data you pass to it - and we don't have access to them.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900