-4
   String start_cd;
   String end_cd;
   int time_start_int;
   int time_end_int;
    opencon();

     SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + end_cd + " FROM " + going + " WHERE " + start_cd + "!=0 or " + end_cd + "!=0 and " + start_cd + " >= " + time_start_int + " and " + start_cd + " <= " + time_end_int + "", con);
    SqlDataAdapter sda_res = new SqlDataAdapter(res);
    DataTable dt_res = new DataTable();
    sda_res.Fill(dt_res);

    listBox1.DataSource=dt_res;
    listBox1.DisplayMember="ID";

    listBox2.DataSource = dt_res;
    listBox2.DisplayMember = start_cd;

i getting no errors but listbox show unfiltered values(i want geting values time_start_int between time_end_int )

sudhara
  • 45
  • 10
  • 8
    People have already told you in your previous question, you concatenating strings to create queries is old fashioned and risky. – Itay Feb 16 '14 at 08:07
  • 1
    Even if you insist on writing code with SQL injection problems please take time and edit your sample so there is no scrolling necessary (it is unnecessarily hard to scroll through your sample to try to guess what is wrong). – Alexei Levenkov Feb 16 '14 at 08:26

3 Answers3

0

You'll need to compare time_start_int and time_end_int with start_cd in separate expressions like this

SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + 
    end_cd + " FROM " + going + 
   " WHERE " + start_cd + "!=0 or " + end_cd + "!=0 and " + 
   time_start_int + " <= " + start_cd + " and " +
   start_cd + " <= " + time_end_int + "", 
   con);

Keep in mind that using strings to concatenate SQL statements make your code vulnerable to SQL injection attacks. You may refer to Algorithm to avoid SQL injection on MSSQL Server from C# code? to get some hints on how to avoid SQL injection attacks.

Community
  • 1
  • 1
saintedlama
  • 6,838
  • 1
  • 28
  • 46
0

First of all i use parentheses for or, because and will calculate first and maybe it cause to remove all the filters and in second part i write time_start_int + " <= " + start_cd + " and " + start_cd + " <= " + time_end_int because we need start_cd being between time_start_int and time_end_int:

SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," + 
    end_cd + " FROM " + going + 
   " WHERE (" + start_cd + "!=0 or " + end_cd + "!=0 ) and " + 
   time_start_int + " <= " + start_cd + " and " + start_cd + " <= " + time_end_int + "", con);
Hamidreza
  • 3,038
  • 1
  • 18
  • 15
-1
SqlCommand res = new SqlCommand("SELECT ID,Available,Type,"'+ start_cd +'","' +
        end_cd +'" FROM going  
       WHERE "'+ start_cd +'"!=0 or "'+ end_cd +'"!=0 and " + 
       time_start_int + " <= "'+ start_cd +'" <= " + time_end_int + "", con);

You missed ' (single quote) for string variable.