-2
public static DataTable LoadGrid(string SelectedItem,string yearSelected)

{

 DataTable tbl;

string a = SelectedItem;

string b = yearSelected;

string query =" SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,DueDate, 
                            "ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,PurchaseOrderNumber,AccountNumber, CustomerID, SalesPersonID, st.Name AS TerritoryName,BillToAddressID, ShipToAddressID,ShipMethodID, CreditCardID, CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment, soh.rowguid, soh.ModifiedDate FROM Sales.SalesOrderHeader soh INNER JOIN Sales.SalesTerritory st ON soh.TerritoryID = st.TerritoryID"+
WHERE  st.Name = "+a+"AND Datepart(year,OrderDate) = "+b;

tbl=DataAccess.cmd(query);
return (tbl);

}

the data passed to a is a territory and to b is b is year from 2 different dropdown list but the query is giving error saying invalid column name canada (suppose if i choose canada from the dropdown list) and canada is an item of a table in territory table in Name column

shauryachats
  • 9,975
  • 4
  • 35
  • 48

4 Answers4

0

The reason for the error is because of missing quotes. The query is currently WHERE st.Name = Candada while it should be WHERE st.Name = 'Canada' instead.

Please consider using a stored procedure instead of using inline SQL queries though.

sudheeshix
  • 1,541
  • 2
  • 17
  • 28
0

well to be more specific you should use the following syntax

string query = string.Format(
            "SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,"
           +" DueDate,ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,"
        +"PurchaseOrderNumber,AccountNumber, CustomerID,SalesPersonID, st.Name AS TerritoryName,"
        +"BillToAddressID, ShipToAddressID,ShipMethodID,CreditCardID,"
        +"CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment,"
        +"soh.rowguid, soh.ModifiedDate FROM Sales.SalesOrderHeader soh "
        +"INNER JOIN Sales.SalesTerritory st ON soh.TerritoryID = st.TerritoryID "
       +" WHERE st.Name='{0}' AND Datepart(year,OrderDate) = '{1}'",a,b);

but use sp in these kind of work...:)

siride
  • 200,666
  • 4
  • 41
  • 62
Nitin Singh
  • 159
  • 2
  • 3
  • 14
0
public static DataTable LoadGrid(string selectedItem,string yearSelected)
{
    DataTable tbl;
    string query = string.Format( @" SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,DueDate,ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,PurchaseOrderNumber,AccountNumber, CustomerID, SalesPersonID, st.Name AS TerritoryName,BillToAddressID, ShipToAddressID,ShipMethodID, CreditCardID, CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment, soh.rowguid, soh.ModifiedDate 
                FROM Sales.SalesOrderHeader as soh 
                INNER JOIN Sales.SalesTerritory as st 
                    ON soh.TerritoryID = st.TerritoryID
                WHERE  st.Name = '{0}' AND Datepart(year,OrderDate) = '{1}'", selectedItem, yearSelected);

    tbl=DataAccess.cmd(query);
    return (tbl);
}

This might improve code readability thus makes writing queries more readable and less prone to syntax errors.

Jicking
  • 768
  • 6
  • 12
-1
SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,DueDate, 
                            "ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,PurchaseOrderNumber,AccountNumber, CustomerID, SalesPersonID, st.Name AS TerritoryName,BillToAddressID, ShipToAddressID,ShipMethodID, CreditCardID, CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment, soh.rowguid, soh.ModifiedDate FROM Sales.SalesOrderHeader soh INNER JOIN Sales.SalesTerritory st ON soh.TerritoryID = st.TerritoryID"+
WHERE  st.Name = "+a+"AND Datepart(year,OrderDate) = "+b;

why use + operator to compose the string again and again, very low efficient and hard to read , why not use "{0}".format()?

liuzhidong
  • 538
  • 3
  • 18
  • The `+` operator is going to be irrelevant here. It only really matters for large strings built with lots of operations. See: http://stackoverflow.com/questions/11133185/stringbuilder-versus-string-concat. – siride Jan 12 '15 at 04:25
  • Also, this snippet won't even compile as you have the quotes all wrong. – siride Jan 12 '15 at 04:26