Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 11, 2012 01:36 PM by Harsha Bhat
Apr 11, 2012 03:13 AM|LINK
I am using VS2008 with sql Server 2005. In my one of the program i wrote a query to find the last record but it shown a exception that Last is not any builtin function. i couldnt able to diognise it any help will be appreciated. Followin are my code
protected void ddlPrdID_SelectedIndexChanged(object sender, EventArgs e)
txtProd.Text = ddlPrdID.SelectedValue.ToString();
// Making the open stock equal to close stock of previous record
objSqlCommand = new SqlCommand("SELECT LAST(Close_stock)from Table_ISGMip where Product_id = '" + this.ddlPrdID.SelectedItem.ToString() + "'", objSqlConnection);
object obj = objSqlCommand.ExecuteScalar();
txtPrdOpen.Text = obj.ToString();
Apr 11, 2012 04:06 AM|LINK
I dont think there is any function in sql server called LAST...
if you are trying to fetch the last Close_Stock value of your table_ISGmip, then you can also write the query like below:
objSqlCommand = new SqlCommand("Select top 1(Close_Stock) from Table_ISGMip where Product_id = '" + this.ddlPrdID.SelectedItem.ToString() + "' order by Close_Stock desc", objSqlConnection);
Apr 11, 2012 04:11 AM|LINK
For the function last please referr the following link http://www.w3schools.com/sql/sql_func_last.asp. mean while i will referr to thecode suggested by you
Apr 11, 2012 04:59 AM|LINK
It did worked but please revert back on the sql last function. I will be awaiting for your help and thank you very much for answer.
Apr 11, 2012 07:54 AM|LINK
Harsha, that answer is for SQL, not for SQL Server.
SQL is a standerd of structured query language, but there are different kind of SQL providers, for example: PL/SQL, MySQL, TSQL, SQLite etc. may be LAST is getting suported by some other providers but Microsoft TSQL does not provide the LAST function, hence
your code was not working.
I hope its making sense :)
Apr 11, 2012 10:09 AM|LINK
if you want to find the last item inserted then you can do like this
SELECT TOP(1) * from Table_ISGMip where Product_id = '" + this.ddlPrdID.SelectedItem.ToString() +'" order by Primery_key DESC
You can use condition with it also
Apr 11, 2012 01:36 PM|LINK
Thank you Ashutosh,
I guess it makes lot of sense. Let me cross verify that.