-2

My problem in the title i have allcodes array and codes TextBox (kodTxtBox) i will split textbox like line per element and querying all elements with for loop then when i run it, it shows the query of only the last element of the allcodes array with the messagebox, but the others go into else and giving error message box

some turkish words in my codes so.
aciklama = description
birim = monad
birimFiyat = Price per 1 unit
ürünler = products
ürünler.sipariskod = products.ordercode etc.

i did a lot of ways for this i used foreach all variables type is string

    allCodes = kodTxtBox.Text.Split('\n');
    for (int i = 0; i < allCodes.Length; i++)
    {
        queryString = "SELECT ürünler.siparisKod, ürünler.aciklama, ürünler.birim, ürünler.fGrup, ürünler.birimfiyat FROM ürünler WHERE (((ürünler.siparisKod)=\"" + allCodes[i] + "\"));";
        using (OleDbCommand query = new OleDbCommand(queryString))
        {
            query.Connection = connection;
            reader = query.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show(allCodes[i] + " Succesful");
                var desc = reader["aciklama"].ToString();
                var monad = reader["birim"].ToString();
                var sellPrice = reader["birimFiyat"].ToString();
                MessageBox.Show("Açıklama: " + desc + " Birim: " + monad + " Satış Fiyatı: " + sellPrice);
                reader.Close();
            }
            else
            {
                MessageBox.Show("Hata");
            }
        }
    }
June7
  • 19,874
  • 8
  • 24
  • 34
Red Life
  • 1
  • 1
  • 1
    Could possibly use string from kodTxtBox and open filtered recordset then loop recordset instead of bothering with array. Edit question to show sample of data in kodTxtBox. Is this user input? – June7 Dec 26 '22 at 20:10
  • Might you please [edit] your question to share a [mcve]? You don't show a literal value for `allCodes` which makes it harder for us to guess where the problem is. Could you just add an compile-time array with two string values, one that works and one that doesn't? E.g. `var allCodes = new [] { "value that fails", "value that works" }`? Showing us a definition of your table schema + sample data would further increase your chances of getting help. See [ask]. – dbc Dec 28 '22 at 01:51
  • I don't know whether this is related to your specific problem, but you should **always** use parameterized queries to protect against SQL Injection attacks. See: [How do parameterized queries help against SQL injection?](https://stackoverflow.com/q/5468425/3744182) and [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/q/7505808/3744182). For an OLEDB example, see [OLEDB Parameterized Query](https://stackoverflow.com/q/12048152). Try rewriting your code to use parameterized queries, and see if the problem goes away. – dbc Dec 28 '22 at 01:54

1 Answers1

0

I solved the problem by making a single query instead of multiple queries. I saved the values ​​returned in each single query into a list and at the end I made the necessary for loop using the elements of the list

Red Life
  • 1
  • 1