473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Find maximum id of a table created in access data source

I want to find the maximum id of Students table.
and that max id should be dispaly in a textBox1.

this is what i did.
Expand|Select|Wrap|Line Numbers
  1. ///////////////////////////////////
  2.  
  3. try {
  4.             com = new OleDbCommand("Select Max(sid) from Students", con);
  5.             reader = com.ExecuteReader();
  6.             while (reader.Read())
  7.             {
  8.  
  9.                 TextBox1.Text = reader["sid"].ToString();
  10.                 Label6.Text = "New Id Ok";
  11.  
  12.             }
  13.         }
  14.         catch(Exception ex){
  15.             Label6.Text = ex.ToString();
  16.         }
  17.  
  18.  
  19. //////////////////////////////////
but still i got a error like this.
System.IndexOutOfRangeException: sid at System.Data.ProviderBase.FieldNameLookup.GetOrdina l(String fieldName) at System.Data.OleDb.OleDbDataReader.GetOrdinal(Strin g name) at System.Data.OleDb.OleDbDataReader.get_Item(String name) at _Default.Button4_Click(Object sender, EventArgs e) in j:\Bank2\Default.aspx.cs:line 36
Error is

can anyone Help on this??
Sep 26 '10 #1
2 2702
Error is in reader["sid"].ToString()

use:
Expand|Select|Wrap|Line Numbers
  1. textBox1.Text = reader[0].ToString();
  2.  
Sep 26 '10 #2
Use ExecuteScalar() instead of ExecuteReader().
Expand|Select|Wrap|Line Numbers
  1.             try {
  2.                 com = new OleDbCommand("Select Max(sid) from Students", con);
  3.                 TextBox1.Text = com.ExecuteScalar().ToString();
  4.                 Label6.Text = "New Id Ok";  
  5.             }
  6.             catch(Exception ex){
  7.                 Label6.Text = ex.ToString();
  8.             }
Sep 26 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

41
by: Ruby Tuesday | last post by:
Hi, I was wondering if expert can give me some lite to convert my word table into access database. Note: within each cell of my word table(s), some has multi-line data in it. In addition, there...
1
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this...
0
by: shapper | last post by:
Hello, I have a Connection String defined in my Web.Config: <connectionStrings> <add name = "dbAccess_JaquelineRoxoAtelier" connectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data...
0
by: shapper | last post by:
Hello, I created an Asp.Net 2.0 Access Data Source. Is it possible to loop the loaded records and access each record field value? Thanks, Miguel
3
by: D | last post by:
Hello everyone I have developed a web application using .asp to access a data source on a different server than where my .asp files are stored. I've tried to determine how to establish this...
1
by: AudreyCole | last post by:
Hi. I have an Access front end .mdb file which in connected to Access tables in some locations and to SQL Server tables in others. It also is connected to an Access table for other data, regardless...
6
by: Chip Pearson | last post by:
I'm sure I'm missing something simple, but I've hit a brick wall. I'm creating an ASP.NET 2.0 web site in VS2005 (VB.NET) and need to read data from an Access database without attaching it to any...
3
by: =?Utf-8?B?QW50?= | last post by:
Hi, I'm new to ASP & the new ADO data model so please excuse the ignorance. I have created a data source by simply dragging a table onto the design view. Can I now pragmatically access columns &...
5
by: Ashutosh | last post by:
Hi, I have a un-typed table and 5 combo box on the form. All these combo box have this table as the data source. Now I want to be able to select different values using the 5 combo box. But when I...
0
by: TrevoriousD | last post by:
hi I have created a data link from access query to excel. I use excel pull data from access in order to do stats. the excel workbook always stays in the same folder as the access db. I want to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.