Click here to Skip to main content
15,909,645 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
problem in select statament between two date. (dd/MM/yyyy)
the error is :

data type mismatch in crietera expression. as the photo


http://postimg.org/image/pldnrnp7r/[^]


and the table in the database ( access) as photo

http://postimg.org/image/au7r3qdr3/[^]


http://postimg.org/image/ag6f44tnj/[^]


select statament :

C#
JSS_sql_01 = "SELECT SUM(sivd_Qnt) AS sivd_Qnt FROM sales_invoices_details where sivd_Sale_DATE >=
'" + dateTimePicker1.Value + "' and sivd_Sale_DATE<='" + dateTimePicker2.Value + "' ";
Posted

Use a Parameterized Query instead of string concatenation - the magic behind them will take care for the correct date format, and - also very important - will help you prevent SQL Injection Attacks.
C#
JSS_sql_01 = "SELECT SUM(sivd_Qnt) AS sivd_Qnt FROM sales_invoices_details where sivd_Sale_DATE >= @startDate and sivd_Sale_DATE<= @endDate ";
...
command.Parameters.AddwithValue("@startDate", dateTimePicker1.Value);
...
 
Share this answer
 
Comments
Golden Basim 19-Mar-14 6:39am    
thanks , the same error !!!
Bernhard Hiller 19-Mar-14 6:49am    
What? Is sivd_Sale_DATE a varchar instead of a DateTime? Don't do such nonsense!
First off, don't do it like that: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. It will probably also cure your problem.
There is also a BETWEEN clause in SQL:
SQL
SELECT * FROM MyTable WHERE myDate BETWEEN startDate AND endDate

So if you restructure your code to use a parameterised query and use BETWEEN is should solver you problem. I don't know how the rest of your code looks, but here's a simple version:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT SUM(sivd_Qnt) AS sivd_Qnt FROM sales_invoices_details WHERE sivd_Sale_DATE BETWEEN @SD AND @ED", con))
        {
        cmd.Parameters.AddWithValue("@SD", dateTimePicker1.Value);
        cmd.Parameters.AddWithValue("@ED", dateTimePicker2.Value);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            if (reader.Read())
                {
                int qnt = (int) reader["sivd_Qnt"];
                ...
                }
            }
        }
    }
 
Share this answer
 
Comments
Golden Basim 19-Mar-14 6:41am    
thank you

i used ur code :


string JSS_connetionString01 = null;
JSS_connetionString01 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";

using (OleDbConnection JSS_cnn01 = new OleDbConnection(JSS_connetionString01))
{
JSS_cnn01.Open();
using (OleDbCommand JSS_cmd01 = new OleDbCommand("SELECT SUM(sivd_Qnt) AS sivd_Qnt FROM sales_invoices_details WHERE sivd_Sale_DATE BETWEEN @SD AND @ED", JSS_cnn01))
{
JSS_cmd01.Parameters.AddWithValue("@SD", dateTimePicker1.Value);
JSS_cmd01.Parameters.AddWithValue("@ED", dateTimePicker2.Value);
using (OleDbDataReader JSS_reader01 = JSS_cmd01.ExecuteReader())
{
if (JSS_reader01.Read())
{
int JSS_qnt01 = (int) JSS_reader01["sivd_Qnt"];

JSS_cmd01.Dispose();
JSS_cnn01.Close();

textBox5.Text = JSS_qnt01.ToString("000"); ;
}


}

}
}




but this error appeared here (int JSS_qnt01 = (int) JSS_reader01["sivd_Qnt"];)

Specified cast is not valid.
and at ( JSS_reader01) this error appeared
Cannot evaluate expression because the code of the current method is optimized.
Bernhard Hiller 19-Mar-14 6:51am    
? perhaps you need a double instead of an int ?
Golden Basim 19-Mar-14 7:07am    
same error
OriginalGriff 19-Mar-14 6:54am    
And I'm supposed to know what is in your database? :laugh:
Golden Basim 19-Mar-14 7:06am    
:] the data : http://postimg.org/image/ren4g24mt/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900