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
// 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);
objSqlConnection.Open();
object obj = objSqlCommand.ExecuteScalar();
objSqlConnection.Close();
txtPrdOpen.Text = obj.ToString();
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);
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Marked as answer by harsha bhat on Apr 11, 2012 04:58 AM
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 :)
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Harsha Bhat
Member
91 Points
57 Posts
SQL Last function is not working
Apr 11, 2012 03:13 AM|LINK
Hi,
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);
objSqlConnection.Open();
object obj = objSqlCommand.ExecuteScalar();
objSqlConnection.Close();
txtPrdOpen.Text = obj.ToString();
}
Regards
Harsha
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: SQL Last function is not working
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);
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Harsha Bhat
Member
91 Points
57 Posts
Re: SQL Last function is not working
Apr 11, 2012 04:11 AM|LINK
Hi Ashutosh,
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
Harsha Bhat
Member
91 Points
57 Posts
Re: SQL Last function is not working
Apr 11, 2012 04:59 AM|LINK
Hi,
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.
Regards
Harsha
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: SQL Last function is not working
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 :)
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
sanjayverma_...
Participant
1568 Points
361 Posts
Re: SQL Last function is not working
Apr 11, 2012 10:09 AM|LINK
Hi Harsha Bhat
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
Harsha Bhat
Member
91 Points
57 Posts
Re: SQL Last function is not working
Apr 11, 2012 01:36 PM|LINK
Thank you Ashutosh,
I guess it makes lot of sense. Let me cross verify that.