Fetch last record of table
-
i m using sqlserver with c#.net making a window application. i have a table 'dealerlogin', from which i want to fetch the last row of the table which is inserted through a query. I want to fetch the last row to get one of its field 'logindatetime' to use it in my update query in 'where' clause. i want to know how can i fetch the last row of the table. i.e -- Counting rows of 'dealerlogin' table one by one till last row is found and putting 'logindatetime' field's value in a variable. But i dont know how to implement it. Hope for some help. Thank you. Nekshan.
-
i m using sqlserver with c#.net making a window application. i have a table 'dealerlogin', from which i want to fetch the last row of the table which is inserted through a query. I want to fetch the last row to get one of its field 'logindatetime' to use it in my update query in 'where' clause. i want to know how can i fetch the last row of the table. i.e -- Counting rows of 'dealerlogin' table one by one till last row is found and putting 'logindatetime' field's value in a variable. But i dont know how to implement it. Hope for some help. Thank you. Nekshan.
Hi, Which are the fields of the table? You can try to fill a dataset with a query where you order the rows of the table desc... For example, using an autonumeric field(if you have one)... Or better than this, getting only the field using the same query and ExecuteScalar method...
-
i m using sqlserver with c#.net making a window application. i have a table 'dealerlogin', from which i want to fetch the last row of the table which is inserted through a query. I want to fetch the last row to get one of its field 'logindatetime' to use it in my update query in 'where' clause. i want to know how can i fetch the last row of the table. i.e -- Counting rows of 'dealerlogin' table one by one till last row is found and putting 'logindatetime' field's value in a variable. But i dont know how to implement it. Hope for some help. Thank you. Nekshan.
-
i m using sqlserver with c#.net making a window application. i have a table 'dealerlogin', from which i want to fetch the last row of the table which is inserted through a query. I want to fetch the last row to get one of its field 'logindatetime' to use it in my update query in 'where' clause. i want to know how can i fetch the last row of the table. i.e -- Counting rows of 'dealerlogin' table one by one till last row is found and putting 'logindatetime' field's value in a variable. But i dont know how to implement it. Hope for some help. Thank you. Nekshan.
' convert it onto c# this code is i writen in vb ' this answer is the question u ask how to get value in dataset through function ' now u bind that as Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'write this code place at which u want data. Dim ds As New DataSet ds = RmaSearchByDays("0065xyz", 10) ' if u want to access ds data then write For i = 0 To ds.Tables(0).Rows.Count Dim str As String = ds.Tables(0).Rows(0).Item(0) ' str contain value item(0) means column 0, item(1) means column 1 ok. Next i End Sub Public Function RmaSearchByDays(ByVal ACCOUNT_REF As String, ByVal TotalDays As Integer) As DataSet Dim cn As SqlConnection = New SqlConnection("write here connection strings") Dim cmd As SqlCommand = New SqlCommand() cmd.CommandText = "write here select statement where account_ref='" + ACCOUNT_REF + "'" + "and tdays =" + TotalDays cmd.Connection = cn Dim da As SqlDataAdapter = New SqlDataAdapter() Dim dsResult As New DataSet da.SelectCommand = cmd da.Fill(dsResult) ' Return the dataset result Return dsResult End Function End Class ' this question answer is select top 1 * from tablename order by column_name desc 'column name is column which is unique number columnname ok ' if u not able to understand then replay me.