Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
private void GetLastReceivingPrice()
        {
            sqlCon.Open();
            strSQL = "select top 1 receiving from Delivery order by date desc where description=@desc and dosage=@dosage";
            SqlCommand cmd = new SqlCommand(strSQL, sqlCon);
            cmd.Parameters.AddWithValue("@desc", txtItemDescription.Text);
            cmd.Parameters.AddWithValue("@dosage", txtDosage.Text);
            txtPrevious.Text = cmd.ExecuteScalar().ToString();
            sqlCon.Close();
        }


ERROR IS " INCORRECT SYNTAX NEAR THE KEYWORD "WHERE"

I AM RETRIEVING THE LATEST PRICE RECEIVED FROM A SUPPLIER ORDER BY DATE DESCENDING

TO BE DISPLAYED IN TXTPREVIOUS.TEXT

THANKS IN ADVANCE
Posted
Comments
[no name] 9-May-14 11:02am    
Why are you screaming at us? "Date" is a reserved word.
Andrius Leonavicius 9-May-14 12:43pm    
Also, please read this:
http://www.hoax-slayer.com/do-not-use-all-capitals.html

Hi,

I can see two problems in your code:
1. As Wes Aday said, date is a reserved word. Put it in the square brackets: [date].
2. ORDER BY clause must be after the WHERE clause.

So try changing this:
SQL
strSQL = "select top 1 receiving from Delivery order by date desc where description=@desc and dosage=@dosage";

with this:
SQL
strSQL = "select top 1 receiving from Delivery where description = @desc and dosage = @dosage order by [date] desc";
 
Share this answer
 
Comments
Andrius Leonavicius 9-May-14 12:33pm    
Also, be careful with ExecuteScalar().ToString(). The ExecuteScalar() method can return null and your code would fail here.

For instance, change this:
cmd.ExecuteScalar().ToString()
with this (null values are handled):
Convert.ToString(cmd.ExecuteScalar())
Member 10717094 9-May-14 19:32pm    
Thanks a lot Andrius
Andrius Leonavicius 9-May-14 19:34pm    
You're welcome.
Your SQL is incorrect. The order by needs to be after the where clause.

SQL
select top 1 receiving 
from Delivery 
where description=@desc 
and dosage=@dosage
order by date desc 
 
Share this answer
 
C#
private void GetLastReceivingPrice()
{
    sqlCon.Open();
    strSQL = "select top 1 receiving from Delivery where description=@desc and dosage=@dosage order by date desc ";
    SqlCommand cmd = new SqlCommand(strSQL, sqlCon);
    cmd.Parameters.AddWithValue("@desc", txtItemDescription.Text);
    cmd.Parameters.AddWithValue("@dosage", txtDosage.Text);
    txtPrevious.Text = cmd.ExecuteScalar().ToString();
    sqlCon.Close();
}


JAFC
 
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