1

Hello guys I have got this code Which I would like it to do simple thing:

SELECT * FROM klisluz WHERE IDzajsluz= id
SELECT * FROM klisluz WHERE subkey = vyberradek

But this simple thing makes this problém: At the moment its showing me rows where subkey isnt equal to vyberradek. How is that possible?

vyberradek is INT ID which i transfer via string

 for (int i = 0; i < dtg_ksluzby.Rows.Count;i++)
            {
                var row = dtg_ksluzby.Rows[i];
                int id = (int)row.Cells["ID"].Value;

                using (var novyprikaz3 = new SqlCommand("SELECT * from klisluz WHERE subkey= '" + vyberradek + "' AND IDzajsluz='" + id + "'", spojeni))
                {

                    spojeni.Open();
                    SqlDataReader precti3 = novyprikaz3.ExecuteReader();
                    if (precti3.HasRows)
                    {
                        row.Cells[4].Value = true;
                    }
                    spojeni.Close();
                }

Would somebody help me solve this thing out? Thanks in advance. enter image description here

I posted Picture which shows that it selects rows which it shouldnt.

Blaze M
  • 200
  • 4
  • 16

1 Answers1

3

Try to drop the quote from the IDzajsluz filter:

   for (int i = 0; i < dtg_ksluzby.Rows.Count;i++)
        {
            var row = dtg_ksluzby.Rows[i];
            int id = (int)row.Cells["ID"].Value;

            using (var novyprikaz3 = new SqlCommand("SELECT * from klisluz WHERE subkey= '" + vyberradek + "' AND IDzajsluz=" + id , spojeni))
            {

                spojeni.Open();
                SqlDataReader precti3 = novyprikaz3.ExecuteReader();
                if (precti3.HasRows)
                {
                    row.Cells[4].Value = true;
                }
                spojeni.Close();
            }
        }

Quote ('') have to be used only for varchar fields, not for numeric values.

EDIT: LIKE is not good in this case, you should use numeric types if they actually are numeric

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
  • Hi again, I tried both possibilities but its still selects values which arent equal to subkey = vyberradek :o – Blaze M Jul 18 '13 at 15:03
  • 2
    what is the type of subkey? text or varchar? – Tobia Zambon Jul 18 '13 at 15:04
  • subkey is data type char(3) i better change it right? Im even going to use only numbers in this subkey – Blaze M Jul 18 '13 at 15:05
  • Ahh, it came out that my problém is in update, its making duplicates, do you have tim to chat please? – Blaze M Jul 18 '13 at 15:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33709/discussion-between-trippino-and-blaze-m) – Tobia Zambon Jul 18 '13 at 15:11
  • please, if you have got some free time, have a look at this: http://stackoverflow.com/questions/17744031/group-by-issue-when-select-to-datagridview-from-2-tables – Blaze M Jul 19 '13 at 13:20