Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
SQL
"SELECT (invoiceNo) as [Invoice No],(InvoiceDate) as [Invoice Date],(Sales.CustomerID) as [Customer ID],(CustomerName) as [Customer Name],SubTotal as [SubTotal],VATPercentage as [Vat+ST %],VATAmount as [VAT+ST Amount],(GrandTotal) as [Grand Total],(TotalPayment) as [Total Payment],(PaymentDue) as [Payment Due],Remarks from Sales,Customer where Sales.CustomerID=Customer.CustomerID and InvoiceDate between #" + dtpInvoiceDateFrom.Text + "# And #" + dtpInvoiceDateTo.Text + "# order by InvoiceDate desc", con);
Posted
Comments
[no name] 20-Sep-14 8:16am    
Write a stored procedure.
RAHUL(10217975) 20-Sep-14 8:17am    
Give us more information

Don't.
Wherever you copied that from, it's a dangerous way to do things: 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.

Exactly how you do that will depend on the language you are using to access SQL server - and that fragment looks like it probably C#.
So it'll be something like:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT (invoiceNo) as [Invoice No],(InvoiceDate) as [Invoice Date],(Sales.CustomerID) as [Customer ID],(CustomerName) as [Customer Name],SubTotal as [SubTotal],VATPercentage as [Vat+ST %],VATAmount as [VAT+ST Amount],(GrandTotal) as [Grand Total],(TotalPayment) as [Total Payment],(PaymentDue) as [Payment Due],Remarks from Sales,Customer where Sales.CustomerID=Customer.CustomerID and InvoiceDate between @FROM And @TO", con))
        {
        cmd.Parameters.AddWithValue("@FROM", dtpInvoiceDateFrom.Value);
        cmd.Parameters.AddWithValue("@TO", dtpInvoiceDateTo.Value);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                ...
                }
            }
        }
    }
 
Share this answer
 
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
StringBuilder sb = new StringBuilder();
            sb.Append("SELECT invoiceNo as [Invoice No],InvoiceDate as [Invoice Date] ");
            sb.Append(" ,Sales.CustomerID as [Customer ID],CustomerName as [Customer Name] ");
            sb.Append(" ,SubTotal as [SubTotal],VATPercentage as [Vat+ST %],VATAmount as [VAT+ST Amount] ");
			sb.Append(" ,GrandTotal as [Grand Total],TotalPayment as [Total Payment] ");
            sb.Append(" ,PaymentDue as [Payment Due],Remarks ");
			sb.Append(" from Sales,Customer ");
            sb.AppendFormat(" Sales.CustomerID= {0}", CustomerID);
            sb.AppendFormat(" AND InvoiceDate BETWEEN '{0}' AND '{1}' ",startDate,endDate ); 

    sqlCommand command = new sqlCommand (sb, con );
                sqlAdapter dataAdapter = new sqlAdapter (command);

                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet, "t");

                connection.Close();

                return dataSet;
    }
 
Share this answer
 
v2

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