views:

75

answers:

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 =?";


using (OleDbCommand cmd = new OleDbCommand(queryString,database))                                    
{
    cmd.Parameters.Add("@datum", OleDbType.Char).Value = DateTime.Now.ToShortDateString();    
}

Why doesn't the parameter datum get the date value? (the value of at least one complex parameter has not been determined )

+4  A: 

From the docs for OleDbCommand.Parameters

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL Statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.

Try using the positional approach instead, basically.

Jon Skeet
Do you mean just like : Where datum=#"DateTime.Now()+"#";
Simon
@Simon: No, I mean using a "?" placeholder, as described in the quoted paragraph. Read the linked documentation for a fuller example.
Jon Skeet
Ok... But i can't make it work. How do i use that in my case (give the parameter the date value and compare it to the date in the database ?
Simon
@Simon: Just change the SQL statement to have "?" instead of @datum and I'd expect it to work. I don't know whether you need to remove the name from the Add call as well...
Jon Skeet
if it would be that easy... i did that before i asked. I still get the same error ( no value given to at least one parameter(smt. like that))
Simon
@Simon: Then please update your question with the modified code and the *exact* error message.
Jon Skeet
A: 

If that's cut straight from your code then it might be giving an error because it needs a space in your queryString before the WHERE clause.

Martin
nope. aint that
Simon