How to access the last row of data and find out how many rows are in that database?

I have a database with one column that has 3 rows with numbers 1, 2, 3. I want to know how to access the last row of the database (which stores the number 3) and find out what number it stores (in this case the number 3). But the goal is so that when I add a new row to the database from aspx page, I will know which number comes next after the last row that is already in the database. 

Also is there some way to find out how many rows in total the database currently stores. Because when the user is exiting my site I want to find out how many rows there are in total and then subtract 3 from that number and delete the rest. If there's a better way to do this, suggestions are welcome. Thanks

 

 


Using Visual Studio 2005. Language: C#
0
finittz
1/23/2008 9:54:17 PM
asp.net.access-datasource 4679 articles. 0 followers. Follow

3 Replies
592 Views

Similar Articles

[PageSpeed] 59

 

finittz:
I have a database with one column that has 3 rows with numbers 1, 2, 3. I want to know how to access the last row of the database
 

I assume you mean you want to access the last row of a table. If the records have some kind of indentity/auto increment column, then you can just order by desc and SELECT TOP 1.

finittz:
Also is there some way to find out how many rows in total the database currently stores.
 

I again assume you mean total rows in a table. You can do SELECT COUNT(*) FROM TABLE to get the number of records. 

0
jimmy
1/23/2008 9:59:44 PM

It sounds like your 1,2,3 columns should be an identity integer column. If you make the column an identity integer, then the database will put next number up in a new record.

You can use the MAX() function to et the max value in a column. Suppose the column is called Id, then:

DECLARE @MAXID INT
DECLARE @LOWID INT
DECLARE @KOUNT INT
SET @KOUNT = COUNT(*) FROM tableName
IF @KOUNT > 3 BEGIN
  SET @MAXID = MAX(ID) FROM tableName
  SET @LOWID = @MAXID - 3
  SET @KOUNT = COUNT(*) FROM tableName WHERE Id > @LOWID
  IF (@KOUNT = 3)
    DELETE tableName WHERE Id <= @LowId
END

Try the above!

 

 


Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
TATWORTH
1/23/2008 10:17:13 PM

Sorry I forgot to mention I was using Access database with C#. But I will try converting the above to C# and seeing if it works. Thanks a lot. The two things I used were the SELECT COUNT (*) FROM tablename -------> to get the total number of records in my table. I also used SELECT MAX (id_name) FROM tablename --------> to get the max value from my column. Thanks a lot.


Using Visual Studio 2005. Language: C#
0
finittz
1/24/2008 1:47:43 PM
Reply:

Similar Artilces:

Accessing GridView Data Row By Row
guys i have just started with asp.net i have a basic doubt, i have created a SQL datasource and a gridview associated with it now what i need to do is get all the data from my database n modify it further, for this i need to access each row of the gridview any ideas on how to do so? foreach(GridViewRow row in GridViewName.Rows) { you can interate like this..  }Better Post your code before asking some thing, some one. So that person helping u will understand u r problem ClearlyYahoo IM : dotnetrulerI am trying to keep Frequently Asked Questions in my blog  When u enable show...

accessing row by row
first of all sorry if i posted this in a wrong place but i'm new to powerbuilder and i have to develop a program quite quickly ( i only worked with acces until now) i have been provided a excel spreadsheet that contains all coloms and rows from all tables wich i have to insert in a database. The difficulty is that i can't use a datapipeline because i need to use a string function to check in wich table i have to put it: TABLENAME_COLUMNNAME -> i thought about using the string function to find out the right column in the right database. however i don't know how you ...

Access data in last row
How can I access the data in last row in datawindow painter? Example: The datawindow have col1, col2, col3 and I want to a field that I can access the col1[lastrow]. Please help! Last DataWindow painter and InfoMaker function Syntax Last ( column { FOR range { DISTINCTT { expres1 {, expres2 {, ... } } } } } ) Description Gets the value in the last row in the specified column. Lorenzo On Sun, 14 Jan 2001 20:10:42 -0500, in powersoft.public.powerbuilder.datawindow <Anonymous> wrote: >How can I access the data in last row in datawindow painter? > &g...

Access GridView Row Data without cell access
Hello,I have a gridview that I modify before rendering to add custom separators between different entities. I compare the value in a particular cell with the previous value, and take action accordingly. Everything works great. However, I no longer want to display the 'entity' column in the actual gridview rows - only as a separator. So, if I remove the column from the gridview, how can I access a particular value on each row without calling a cell? Keep in mind, this takes place during the render stage and not gridRowBound. My render code is as follows: 1 Protected Overri...

Problem accessing Ids of last row after dynamically adding row to GridView
I'm adding a "header" row to a gridview.  In the RowDataBound method I get the table of the gridview (e.Row.Parent), I then create a new row:Dim newRow As New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)Add some html to it and then add the row to the table:ParentTable.Rows.Add(newRow)This renders correctly.  However, in the gridview rows, I also have a link button.  For the most part this works, I click on the link button and am able to get the Id from the command argument.Funny thing is that if I click the link button in the las...

Retrieve individual data, row wise from access database ......
Hi, I want to retrieve individual data from access database in order to work with each data separately. plz can anyone help me by giving any related code. thanks. Select column(s) from table where unique_row_value = <some_value>Some good basic SQL tutorials can be found here: http://www.w3schools.com/sql/  Regards Mike [MVP - ASP/ASP.NET]My site...

First Row, Next Row, Prior Row, Last Row
I have a SQL Anywhere 7.0 Database, have a table Product_detail which contains Column Name Datatype width Null Default batch_id char(10) No None prod_id Integer No None employee_id Integer No None quantity Integer No None price Numeric (14, 2) No None Its primary key is batch_id I have a Freeform Datawindow with this table I want to provide the first row, next row, prior row and last row Ho...

How to get data from database row by row
I try to get data from a database row by row, I am using DataReader, the return result is not I expected, don't know how to solve it. Here is my sample code: --------------------------------------------Dim commandText As String = "select * from customer "Dim con As New OleDbConnection("Data Source=c:\company.mdb;Persist Security Info=False; Provider=Microsoft.Jet.OLEDB.4.0;")Dim Cmd As New OleDbCommand(commandText, con)Dim reader As OleDbDataReader = Cmd.ExecuteReader()             Using con       &nbs...

Deleted row information cannot be accessed through the row.
I'm getting an exception with the message "Deleted row information cannot be accessed through the row". I have processing the records through the data table. In some situation i have deleted the rows in the datatable and inserting the new rows. These situation i have get the above specified error. Regards, Vijay Hi Vijay, this error occours when u try to access the row which u have already deleted. So, try to check if the row exists or not and then retrieve the information. I hope this will help u..Please mark as Answer if it helps u. Thanks!Parth PatelTechsture Technol...

Deleted row information cannot be accessed through the row.
I have a DataSet in which I need to make multiple changes prior to saving. Here's the scenario where I'm having the problem:1. I delete a row and it disappears from the table.2. I attempt to add another row.3. I receive an error that "Deleted row information cannot be accessed through the row." when the the screen posts back and refreshes.Here's the code for deleting:<code>protected void dgEmail_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { Debug.WriteLine("dgEmail_DeleteCommand"); EnableSaveBtn(); dgEmail.EditItemIndex = -1; dsEmail.Tables["...

Access Database
I have an Access Database with two columns, date (xx/xx/xxxx) and a messages (255 characters). I would like to be able to display a message associated with a particular date, which is selected from a Calendar, in a Label outside the Calendar. And I would like to be able to do this using Visual Web Developer (Visual Basic code). I am a novice with ASP.NET and accessing a database. I can display a GridView of the enter date/message database. Just use an event that is wired to the calendar control.  Just search how to use events on asp.net pages if you dont know how to do this.  ...

Data Access using MS Access Database
I am writing an application I am writing in VS 2005 Web Developer.  I have been able to connect to my Access Database just fine, but I am having trouble pulling rows from the database.  I have a drop down list in order to choose an employee.  After the employee is picked I need to copy their address, city, state, and zip ifrom my table into text boxes on the form.  I have looked for a week now, but nothing goes beyond just the connection itself.  I can find plenty using sql, but not access.  Can anyone help? Most of the SQL examples you will find for use ...

Ms-Access 2007 could not find my access database
hi, i trying to export datagrid to excel and for that i created database using Ms Access 2007 and when i tried to connect to my database its not connecting instead it is throwing an error "Could not find file 'D:\Application\Test\Excel.mdb" My connection is OleDbConnection cn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=Excel.mdb;"); could any one help me out why i m not able to connect it ...   Thanks & Regards,Ramesh.S,Software Engineer,http://www.rameshsengamalai.info  you code will be  OleDbConnection cn = ...

access data... access...
Hi, I'm trying to handle a simple structured access db in c#... but I'm a bit confused on how to do it! How can I select, insert, edit and remove records from an access database? should I use datasets? Thanks I suggest reading microsoft's Documentation on ADO.Net. OleDb is the DataProvider you want to use if you're going against an Access DB. another thing: how can I get, having an opened oledbconnection, all the tables, and the fields in those tables, from the database?...

Web resources about - How to access the last row of data and find out how many rows are in that database? - asp.net.access-datasource

Database - Wikipedia, the free encyclopedia
... requiring information. For example, modelling the availability of rooms in hotels in a way that supports finding a hotel with vacancies. Database ...

Database - Wikipedia, the free encyclopedia
A database is an organized collection of data . The data are typically organized to model aspects of reality in a way that supports processes ...

Ben Carson calls for database of all foreigners in US
Boston Herald Ben Carson calls for database of all foreigners in US Boston Herald Republican presidential candidate, Dr, Ben Carson, center, ...

GOP rivals condemn Donald Trump for backing Muslim database
COLUMBIA, S.C. (AP) — Retired neurosurgeon Ben Carson said Saturday that he wants to expand the government's surveillance operations aimed at ...

Donald Trump wants "surveillance of certain mosques," database of refugees - Videos - CBS News
... rally in Birmingham, Alabama, Republican presidential candidate Donald Trump called for increased scrutiny over mosques, along with a database ...

Trump Wants A Database For All Syrian Refugees Who Enter America
Trump Wants A Database For All Syrian Refugees Who Enter America

US Republican rivals blast Donald Trump for Muslim database comments
US Republican rivals blast Donald Trump for Muslim database comments - Donald Trump said he would implement a database to keep track of Muslims ...

Animal Venom Database Could Be Boon To Drug Development
A new public database that catalogs animal toxins could spur the development of new drugs made with compounds found in animal venom.

WATCH 'TROJAN HORSE' Trump wants database for Syrian refugees in US
WATCH 'TROJAN HORSE' Trump wants database for Syrian refugees in US

Donald Trump is Under Media Assault for Suggesting ‘Muslim Database,’ But Here’s What He Says About It ...
"Donald Trump's plan for a Muslim database draws comparison to Nazi Germany"...

Resources last updated: 12/2/2015 2:01:40 PM