Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (2 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 #" + DateTimePicker2.Text + "# And #" + DateTimePicker1.Text + "# and PaymentDue > 0 order by InvoiceDate desc", con);
Posted
Comments
Dilan Shaminda 8-Jul-14 5:13am    
what is the error you are getting?
pagal panti 8-Jul-14 5:28am    
no error is syntax error Near "#"
_Asif_ 8-Jul-14 5:36am    
Please do not post questions that you already have posted

You can use a more formatted string build up to make it easier to see the error.
C#
    StringBuilder cmdString = new StringBuilder();
    cmdString.Append(@"SELECT (invoiceNo) as [Invoice No]");
    cmdString.Append(@",(InvoiceDate) as [Invoice Date]");
    cmdString.Append(@",(Sales.CustomerID) as [Customer ID]");
    cmdString.Append(@",(CustomerName) as [Customer Name]");
    cmdString.Append(@",(GrandTotal) as [Grand Total]");
    cmdString.Append(@",(TotalPayment) as [Total Payment]");
    cmdString.Append(@",(PaymentDue) as [Payment Due]");
->  cmdString.Append(@"from Sales, Customer");
    cmdString.Append(@"where Sales.CustomerID = Customer.CustomerID");
    cmdString.Append(@"and InvoiceDate between #");
    cmdString.Append(DateTimePicker2.Text);
    cmdString.Append(@"# And #");
    cmdString.Append(DateTimePicker1.Text);
    cmdString.Append(@"# and PaymentDue > 0");
    cmdString.Append(@"order by InvoiceDate desc");


Have a look at the line pointed out by ->.
Is that syntax correct?
 
Share this answer
 
Comments
pagal panti 8-Jul-14 5:28am    
no error is syntax error Near "#"
First thing I would do is restructure the query to put the various AND statements in brackets;

e.g.

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 #" + DateTimePicker2.Text + "# 
                                             AND #" + DateTimePicker1.Text + "#)
                      AND   (PaymentDue > 0)
                      ORDER BY InvoiceDate desc"
               , con);


Check the date fields to make sure you haven't got the later one before the early one.

Also, Format the DateFields to make sure they are appearing correctly to SQL. i always format to YYYY-MMM-DD so there is always no ambiguity.

Did you test the two seperate parts? i.e. the Sales data and the Customer data using known variables? then join the two parts together to test as a complete query.
 
Share this answer
 
v3
Hi ,

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 '" + DateTimePicker2.Text + "' And '" + DateTimePicker1.Text + "' and PaymentDue > 0 order by InvoiceDate desc", con);

Try to use instead # use ' this one
 
Share this answer
 
As far as I know, the # character works for microsoft access database. For SQL database replace it with a single quote and try doing it.

Please let me also know if that works for you
 
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