0

I want to retrieve data from database using the following sql query but this is not working. I am trying to fetch data according to input date. In database the date is in format of 03/03/2013. Is following query correct for the problem.

 public DataSet OrderByDate(string date1, string date2)
        {


            //  string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Amrit\\Desktop\\Database.accdb ;Persist Security Info=False;";
            DataSet dataSet = new DataSet();
            OleDbConnection oleConn = new OleDbConnection(connString);

            try
            {
                oleConn.Open();
                string sql = "SELECT Order.OrderNumber, (Customer.Title +SPACE(2)+ Customer.CustomerName) as [Customer Name], Customer.CustomerEbayname, Customer.EmailAddress, Customer.PhoneNumber, (Customer.Address1 + SPACE(2) + Customer.Address2 + SPACE(2)+ Customer.City + SPACE(2) + Customer.PostCode + SPACE(2) + Customer.Country) as Address,  Order.ItemPurchased, Order.PurchasedDate, Order.TotalPrice FROM Customer INNER JOIN [Order] ON Customer.[CustomerID] = Order.[CustomerID] WHERE [PurchasedDate] BETWEEN #date1# AND #date2#";                    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                dataAdapter.Fill(dataSet, "Customer");
            }
            catch (Exception ex)
            {
                MessageBox.Show("An exception has been occured\n" + ex.ToString());
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                oleConn.Close();
            }
            if (dataSet.Tables.Count <= 0)
                return null;
            else
                return dataSet;
        }
1
  • Don't forget to accept if your question is answered. Commented Mar 3, 2013 at 8:51

1 Answer 1

1

since you are using Access, wrap the dates with # not by single quotes.

SELECT  [Order].[Order]Number, 
        (Customer.Title +SPACE(2)+ Customer.CustomerName) as [Customer Name],
        Customer.CustomerEbayname, 
        Customer.EmailAddress, 
        Customer.PhoneNumber, 
        (Customer.Address1 + SPACE(2) + Customer.Address2 + SPACE(2)+ Customer.City + SPACE(2) + Customer.PostCode + SPACE(2) + Customer.Country) as Address,  
        [Order].ItemPurchased, 
        [Order].PurchasedDate, 
        [Order].TotalPrice 
FROM    Customer 
        INNER JOIN [Order] 
            ON Customer.[CustomerID] = [Order].[CustomerID]  
WHERE   [PurchasedDate]= #" + date + "#
Sign up to request clarification or add additional context in comments.

4 Comments

use BETWEEN eg, WHERE [PurchasedDate] BETWEEN #date1# AND #date2#
try this, WHERE [PurchasedDate] >= #date1# AND [PurchasedDate] <= #date2#
Syntax error in date in query expression '[PurchasedDate] >= #date1# AND [PurchasedDate] <= #date2'.
are you sure between is not supported? en.allexperts.com/q/Using-MS-Access-1440/… what version of access you are using?

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.