Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
cmd = new SqlCommand("SELECT (InvoiceNo) as [Invoice No],(InvoiceDate) as [Invoice Date],(Sales.CustomerID) as [Customer ID],(CustomerName) as [Customer Name],(GrandTotal) as [Grand Total],(TotalPayment) as [Total Payment],(PaymentDue) as [Payment Due] from Sales,Customer where Sales.CustomerID=Customer.CustomerID and InvoiceDate between #" + dtpInvoiceDateFrom.Text + "# And #" + dtpInvoiceDateTo.Text + "# order by InvoiceDate desc", con);
Posted
Comments
DamithSL 8-Jul-14 4:35am    
what is the column date type of InvoiceDate ?

use parameters like below
C#
cmd = new SqlCommand("SELECT (InvoiceNo) as [Invoice No],(InvoiceDate) as [Invoice Date],(Sales.CustomerID) as [Customer ID],(CustomerName) as [Customer Name],(GrandTotal) as [Grand Total],(TotalPayment) as [Total Payment],(PaymentDue) as [Payment Due] from Sales,Customer where Sales.CustomerID=Customer.CustomerID and InvoiceDate between @dtFrom And @dtTo order by InvoiceDate desc", con);
cmd.Parameters.AddWithValue("@dtFrom", fromDate);// convert your dtpInvoiceDateFrom.Text to DateTime and set here
cmd.Parameters.AddWithValue("@dtTo", toDate);// convert your dtpInvoiceDateTo.Text to DateTime and set here
 
Share this answer
 
Replace # with single quotation like ['] and try again.

cmd = new SqlCommand("SELECT (InvoiceNo) as [Invoice No],(InvoiceDate) as [Invoice Date],(Sales.CustomerID) as [Customer ID],(CustomerName) as [Customer Name],(GrandTotal) as [Grand Total],(TotalPayment) as [Total Payment],(PaymentDue) as [Payment Due] from Sales,Customer where Sales.CustomerID=Customer.CustomerID and InvoiceDate between '" + dtpInvoiceDateFrom.Text + "' And '" + dtpInvoiceDateTo.Text + "' order by InvoiceDate desc", con);


If you get invalid datetime error than you need to format your date into yyyy-mm-dd format and try again. By the way Solution 1 is more elegant.
 
Share this answer
 
v2
Try replacing # with single quotes and let us know if that works
 
Share this answer
 

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