Domanda

I am having problems retrieving values from the last inserted row in a Data-table. I have a login form and the values will be inserted in the table has the ID (int,an auto incremented value), userID (int), logintime (smalldatetime) and logouttime(smalldatetime). The code which is used to inside the login button,it thus inserts all values except the log out time

DateTime t1 = DateTime.Now;
objbal2.insertLoginTime(s, t1);

and in the log out button I am updating the table, so I have to retrieve the last row values. I am updating the table with reference to the ID value and userID. Can I use this query get the values? But I can't figure out how?

SELECT COLUMN FROM TABLE ORDER BY COLUMN DESC

Thanks in advance

È stato utile?

Soluzione

if you have to read the values from last row then

DataRow lastRow = yourTable.Rows[yourTable.Rows.Count-1];

will return you last row. and you can read the values from it.

My second guess is that by datatable you are referring to table in sql server.

Then with small modification your query is fine as well.

SELECT TOP 1 COLUMN FROM TABLE ORDER BY COLUMN DESC

Altri suggerimenti

var dt = new DataTable();
dt.AsEnumerable().Last();

dt.AsEnumerable() returns an IEnumerable<DataRow>

You can got the value from your session and then update your database as per your change

DateTime t2 = DateTime.Now;

DataRow Row = Mylagin_dataTable.Rows.Count-1

Row[0]["LogoutTime"] = t2 ;

if your ID in session then use below

DateTime t2 = DateTime.Now;

DataRow Row = Mylagin_dataTable.Select("LoginID='"+ HttpContext.Current.Session["loginID"] + "'");

Row[0]["LogoutTime"] = t2 ;

Execute following query on Application Exit event, this will do your work

string _query = string.Format("update top 1 SessionTable set LogOutTime = {0} where UserID = {1} order by ID desc", DateTime.Now, UserID);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top