Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi,

i need to print the last row of the table in the data grid form .i am using the sql server and c#.net.. i want to print the recently inserted row of the table.. can u tell me the answer..
Posted
Comments
[no name] 13-Apr-12 7:34am    
Tell you what answer? How to print? How to find the last row in a grid?
Pravinjas 13-Apr-12 7:49am    
give details about your database table column
Corporal Agarn 13-Apr-12 8:21am    
This is strictly a C# question as there is no order to how rows are stored in SQL tables. When you query the table you and do not use an ORDER then it uses the primary key to order the data.

1 solution

if your insert statement is in your code, you can use ;SELECT SCOPE_IDENTITY after your insert statement, this will return the last identity value that you inserted..

simple enough to run another select statement from this to print out the last record.

http://msdn.microsoft.com/en-us/library/ms190315.aspx[^]

C#
string sqlInsertActivity = "INSERT INTO activity (CodeID, Subject, ActivityPriorityID, CreatedByEmployeeID, AssignmentTaskStatusID) "
	+ "VALUES('82', 'New Document', '2', '" + createdBy + "', '2') "
	+ ";SELECT SCOPE_IDENTITY();";

string activityID = SqlHelper.ExecuteScalar(conn, CommandType.Text, sqlInsertActivity).ToString();


a quick example above of one I did which inserts a row into a table and returns the id of the record just inserted
 
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