Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Fetch last record of table

Fetch last record of table

Scheduled Pinned Locked Moved C#
csharpdatabasehelptutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nekshan
    wrote on last edited by
    #1

    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 A V 3 Replies Last reply
    0
    • N 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.

      I Offline
      I Offline
      il_masacratore
      wrote on last edited by
      #2

      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...

      1 Reply Last reply
      0
      • N 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.

        A Offline
        A Offline
        althamda
        wrote on last edited by
        #3

        select top 1 * from tablename order by column asc/desc Obviously use either asc or desc

        1 Reply Last reply
        0
        • N 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.

          V Offline
          V Offline
          virendra patel
          wrote on last edited by
          #4

          ' 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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups