2
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#).

3
  • 1
    what error? also where are you comparing a date, I can see you create date but not use it? Commented May 20, 2010 at 13:08
  • 1
    The error may be that ToShortDateString returns the date in a format that SQL does not recognizes. (It is generally better to use parameters to pass parameters in a query.) But without indication of what error actually occurred, it's hard to tell if the cause of the error is this or something entirely different. Commented May 20, 2010 at 13:10
  • numbers syntax error in query command in the whole where statement(users.id=1 AND obroki_save.datum=20.5.2010) Commented May 20, 2010 at 13:15

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
use DateTime.Now.ToString(); instead of DateTime.Now.ToShortDateString();
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
1

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).

2 Comments

Did you try the # delimiter for the date?
BTW, Jet/ACE is not vulnerable to that particular SQL injection vulnerability, because it doesn't execute but one SQL statement at a time.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.