Results 1 to 3 of 3

Thread: How to extract cell in database and save on variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    19

    How to extract cell in database and save on variable

    Hello there, I've been struggling with that for few days already and I can't seem to get things right...

    I've got a table with "id, name and phone" and I plan to copy the "last inserted id" into a variable, to do so, I thought about making a "SELECT MAX(id) FROM table" since id is increasing on every insert, so the greatest one will be the last one inserted, but then again, I don't know how to save that value returned from the select into a variable to use it later.

  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    56

    Re: How to extract cell in database and save on variable

    Well, couldnt you just declare a variable of the same datatype (in this case, an integer im assuming) and have it hold the value from that cell? Kind of like this? (Raw code)
    Code:
    Dim lastID As Integer = DataGridView1.Colums(0).Rows(0).Cell(0).Value ' Extracts the text from colum 0, row 0, cell 0
    So, if "ID" is lets say 15, then "lastID" is 15, and then, every time the ID cell is itterated (so to get the new "last id"), just call it again.

    Code:
    lastID = DataGridView1.Colums(0).Rows(id).Cell(0).Value ' Gets the text from the new last item added under "ID"
    Where "id" in this case, is assumed that you declared a method for "id" before, that gets the cell number of the newly added item as it is being added.

    Also assuming your "Database" is already loaded to the application on a "datagridview" control

    Sorry if this isnt exactly what your looking for, as this is more of a datagridview, then accessing say a Oracle or Navicat/SQL actual Database, but hopefully, without seeing how your getting this "data" and where your putting it, you can transfer the same logic to your exact issue.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,039

    Re: How to extract cell in database and save on variable

    If you are talking about querying the database directly to get the value, I'd create a command object, give it the SQL that you already wrote, then call the ExecuteScalar method on the command object. ExecuteScalar returns the first field from the first row of the query. In your case, your query only has one row and one field, so it's a perfect fit. ExecuteScalar is also about the fastest query you can run, too. What it returns is an Object, but with that query, it will ALWAYS return an integer, so, assuming you already opened a connection and created a command object called cmd:

    Code:
    cmd.CommandText = "SELECT MAX(id) FROM table"
    dim someInteger = CInt(cmd.ExecuteScalar)
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width