views:

83

answers:

3
string queryString = "SELECT SUM(skupaj_kalorij)as Skupaj_Kalorij  "
                + "FROM (obroki_save LEFT JOIN users ON obroki_save.ID_uporabnika=users.ID)"
                + "WHERE (users.ID= " + a.ToString() + ") AND (obroki_save.datum= @datum)";

            using (OleDbCommand cmd = new OleDbCommand(queryString,database))                                    
                {
                    DateTime datum = DateTime.Today;
                    cmd.Parameters.AddWithValue("@datum", datum);
                }
            loadDataGrid2(queryString);

I tried now with parameters. But i don't really know how to do it correctly. I tried like this, but the parameter datum doesn't get any value(according to c#).

+2  A: 

please try this :

database = new OleDbConnection(connectionString);
                database.Open();
                date = DateTime.Now.ToShortDateString();
                string queryString = "SELECT SUM(skupaj_kalorij)as Skupaj_Kalorij  "
                    + "FROM (obroki_save LEFT JOIN users ON obroki_save.ID_uporabnika=users.ID)" 
                    + "WHERE users.ID= " + a.ToString()+" AND obroki_save.datum= '" +DateTime.Today.ToShortDateString() + "'";
                loadDataGrid2(queryString);

when you use with Date, you must write like this

select * from table where date = '@date'

not like

select * from table where date = @date
masoud ramezani
that's it . thank you !!
Simon
but man. I don't get no results now.... Couse i changed the datatype from date to text in the database. Now i changed it back to date/time and i get an error that theres a wrong data type in the Conditional statement.
Simon
use DateTime.Now.ToString(); instead of DateTime.Now.ToShortDateString();
masoud ramezani
I changed the commas on the top (doesn't matter how they're called right now :D), so i get an messege that there's an syntax error in the number somewhere in the where statement
Simon
+1  A: 

While it's usually useful to post the error, I'd hazard a guess and say that you're getting a conversion error with your date.

You should really look at parameterising your queries...

You should read this: http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/

And if you can't be bothered reading that, then try changing your 'a' variable to '1; DROP TABLE obroki; --' (but only after you back up your database).

Paddy
IT's something with the date. Couse it works without it
Simon
Did you try the # delimiter for the date?
David-W-Fenton
BTW, Jet/ACE is not vulnerable to that particular SQL injection vulnerability, because it doesn't execute but one SQL statement at a time.
David-W-Fenton
A: 

Perhaps you need to write your SQL string in the SQL dialect of the database you're using. In Jet/ACE SQL (what's used by Access), the delimiter for date values is #, so you'd need this:

  obroki_save.datum= #" +DateTime.Today.ToShortDateString() + "#"

Of course, some data interface libraries translate these things for you, so that may not be the problem here.

David-W-Fenton
nope. Doesn't work.
Simon