Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code
C#
//cmd is already declared as string
cmd = "SELECT SUM(purchasePrice) AS price, COUNT(purchasePrice) AS num" +
"FROM ( SELECT TOP ("+quantity+") purchasePrice FROM store.dbo.tblSingleItems" +
"WHERE IsActive='1' AND proID='" + proID + "') AS profit" +
"UPDATE TOP ("+quantity+") tblSingleItems SET IsActive='1',salePrice='"
 + price +"' " +
"WHERE IsActive='1' AND proID='" + proID + "'";

SqlCommand sqlcmd = new SqlCommand(cmd, con);
SqlDataAdapter sda = new SqlDataAdapter(sqlcmd);
DataTable tbl = new DataTable();
sda.Fill(tbl);
con.Close();

when this executes in app, gives Syntax error near '='

What I have tried:

i tried this command in sql server 2014 that gives me output without any error. i had a very careful look but couldnot find any syntax error. if anyone can recognize
Posted
Updated 8-Dec-17 5:49am
Comments

First thing, this is not how you should be building a sql statement, you are open to sql injection attacks here.

Parameterizing sql queries c#

Next, simply look at your query. You've got no space after store.dbo.tblSingleItems and the following line of WHERE IsActive = '1'. So your query, when parsed, looks like this, to make it more obvious, here it is all on one line

SQL
SELECT SUM(purchasePrice) AS price, COUNT(purchasePrice) AS numFROM ( SELECT TOP ("+quantity+") purchasePrice FROM store.dbo.tblSingleItemsWHERE IsActive='1' AND proID='" + proID + "') AS profitUPDATE TOP(1) tblSingleItems SET IsActive='1',salePrice='1' WHERE IsActive='1' AND proID='1';


So you probably need to adjust your code to look like this

C#
cmd = "SELECT SUM(purchasePrice) AS price, COUNT(purchasePrice) AS num " +
"FROM ( SELECT TOP ("+quantity+") purchasePrice FROM store.dbo.tblSingleItems " +
"WHERE IsActive='1' AND proID='" + proID + "') AS profit " +
"UPDATE TOP ("+quantity+") tblSingleItems SET IsActive='1',salePrice='" + price +"' " + " WHERE IsActive='1' AND proID='" + proID + "'";


Notice the additional spaces. I think you may have issues in your SELECT statement as it doesn't make much sense to me but if that portion works then great. Just addressing the syntax error.
 
Share this answer
 
Comments
Arham Anees 8-Dec-17 11:23am    
actually i never studied database as first year programming so i got help from google. frankly i dont know how this is working but it works.
Arham Anees 8-Dec-17 11:26am    
this worked. thank you dude :)
If you debug your code and look at the final string, you'll see the problem: you're missing spaces across several lines.
C#
const string query = 
    "SELECT SUM(purchasePrice) AS price, COUNT(purchasePrice) AS num " + // <--Add a space here
    "FROM ( SELECT TOP (@quantity) purchasePrice FROM store.dbo.tblSingleItems " + // <-- And here
    "WHERE IsActive = '1' AND proID = @proID) AS profit; " + // <-- And here
    "UPDATE TOP (@quantity) tblSingleItems SET IsActive = '1', salePrice = @price "
    "WHERE IsActive='1' AND proID = @proID;";

using (SqlCommand sqlcmd = new SqlCommand(query, con))
{
    sqlcmd.Parameters.AddWithValue("@quantity", quantity);
    sqlcmd.Parameters.AddWithValue("@proID", proID);
    sqlcmd.Parameters.AddWithValue("@price", price);
    
    SqlDataAdapter sda = new SqlDataAdapter(sqlcmd);
    DataTable tbl = new DataTable();
    sda.Fill(tbl);
    ...
}
 
Share this answer
 
You need some spaces at the beginning and end of your strings to separate the sql statements so they don't run on to one another. i.e. +"WHERE should be + " WHERE" Also, you really should be using parameters in your query.
 
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