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
        Do While reader.Read()
        textbox1.text = reader.GetString(0))


      End Using


In my database, I have table name "customer", and only 1 field there name "ID", for example, assumed I have 4 records in the table, they are A, B, C, D

The return result are
A, B, C, D
B, C, D,
C, D

But, what I want the end result is


I know the problem probably is that when I use the DataReader, it runs the whole data base, with the loop, it goes back move to the next row, and runs it again. Not sure how to solve it, Is DataReader a right option here?


2/14/2007 6:12:27 AM 16182 articles. 0 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 49

Bellow I am giving you two examples. Which will solve your problem. Go throw the code line by line. You will get your answer.




public void BindData_dataSet()


        string strConStr = ConfigurationManager.ConnectionStrings["MyConTS"].ConnectionString;

        SqlConnection MyCon = new SqlConnection(strConStr);

        SqlCommand myCommand = new SqlCommand("select * from tUser", MyCon);       

        SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);

        DataSet ds = new DataSet();

        myAdapter.Fill(ds, "tUser");


        GridView1.DataSource = ds;



        foreach (DataRow myRow in ds.Tables["tUser"].Rows)


            TextBox1.Text = TextBox1.Text + myRow["cUserName"].ToString() + Environment.NewLine;





    protected void Button2_Click(object sender, EventArgs e)



        Label1.Text = "Data binding through code using Data Set";



    public void BindData_dataReader()


        string strConStr = ConfigurationManager.ConnectionStrings["MyConTS"].ConnectionString;

        SqlConnection MyCon = new SqlConnection(strConStr);

        SqlCommand myCommand = new SqlCommand("select * from tUser", MyCon);


        SqlDataReader myReader = myCommand.ExecuteReader();


        while (myReader.Read())


            TextBox1.Text = TextBox1.Text + myReader.GetName(0) + " : " + myReader["cUserName"] + " ";

            TextBox1.Text = TextBox1.Text + myReader.GetName(1) + " : " + myReader["cFullName"] + " ";

            TextBox1.Text = TextBox1.Text + myReader.GetName(3) + " : " + myReader["cEmail"] + Environment.NewLine;

            TextBox2.Text = TextBox2.Text + "Get string (0):"+ myReader.GetString(0) + Environment.NewLine;

            TextBox2.Text = TextBox2.Text + "Get string (1):" + myReader.GetString(1) + Environment.NewLine;

            TextBox2.Text = TextBox2.Text + "Get value (3):" + myReader.GetValue(3) + Environment.NewLine;

            TextBox2.Text = TextBox2.Text + "Get datatype (1):" + myReader.GetDataTypeName(1) + Environment.NewLine;



Md. Humayun Kabir Foysol
Software Engineer
Enosis Solutions
House # 9A, Road # 83,
Block NE(K), Gulshan-2
Tel: +88 02 8836411
Cell: +88 01712 982 687
2/14/2007 9:02:17 AM

This seems not the one I am looking for. I may not get myself clearly in the beginning. I don't just want the end result show as



Actually I want to get record from the column one at a time, do some operation with it, then move to the next row, do some operation with it, until the last row of the column. I want to read each record once at a time, then move down. When I use DataReader, it seems it always read the whole column before it stops, not one row by one row. How I can use DataReader to do it, or there is a better way. Thanks.

2/14/2007 5:30:46 PM

Similar Artilces:

Can't get dw_1.Object.Data[row] for a row > 32768
Is anyone aware of a problem with passing a row greater than MAX(integer) (32768) to dw_1.Object.Data[row]? I would appreciate a response. Jameel Abdo MCI Systemhouse. I understand that PB5 didn't support object notation for more than 32K rows. It was supposed to be fixed in PB6 (although I haven't confirmed it in PB6, myself). The object notation tended to be a tad flakey in PB5 too (poor null handling, etc.). Many developers decided to stick with the GetItem, SetItem in PB5 to avoid these problems. Jameel Stephen Abdo wrote: > Is anyone aware of a proble...

Why does it not let me type data in my database fields and gives me error "No row was updated, the data in row 1 was not committed?
Hi, I'm typing example records in my database fields for username, last name, firstname, city, state, etc directly into the database field but when I try to save it it says:  No row was updated. The data in row 1 was not committed.Error Source: .NetSqlClient Data Provider.Error Message: String or binary data would be truncated.The statement has been terminated.Correct the errors and retry or press ESC to cancel the change(s) Basically I'm just typing data into the fields but it won't allow me to save them.    The data types are all saved as...

read data from database row by row in WebForm.aspx
The following task is going to be coded in WebForm.aspx. I would like to read data from a database table row by row and execute some codes based on the data retrieved from each row. For example, there are 4 fields on a table called Image_Tbl. Those fields are Image ID, Image Path Name, X-Coordinate, and Y-Coordinate. Data stored on the table will be: Image ID Image Path Name X-Coordinate Y-Coordinate ImgBtnLight image\light.gif 192px 92px ImgBtnAC image\ac.gif 256px 152px How should I make a loop saying before en... problem: i can not view image data for the rest of my table rows except the image for the first row but i can read all the other non image fields for all rows.
Hi i have a table with the following fields imgdata........image imgtitle.........varchar(max) imgType........varchar(max) imgLength.....bigint i have no problem with uploading the image but viewing the uploaded image(s) in a gridview has caused me great pain. The probelm is i have two pages. page1 grabs the image to display and page2 has a gridview that displays the grabed image. I wanted to have an sql statement like SELECT * FROM mytable WHERE incident_id=someValue. IF i have 5 records in this table and i view page2 ...

How to get the data of a particular row in gridview without editing that row.
I have a gridview in a page in that ihave hyperlink as one column on click of that i want to go next page with the data present in that corresponding row. How shell i read the data from grid view for that pirticular row.On which event i can get the data. HI, the row that is selected should be passed into a string or an array or in sessionsthis can be done through this code  protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)    {        Label _lbl = new Label();      &...

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

Displaying two rows of database column data in one row of datagrid
Hello, I am trying to display two rows of an access column on one row of datagrid. Is that possible? Example of my intended datagrid: courseNo of course1courseNo of course2 courseNo of course3courseNo of course4 here's the HTML view of my aspx file: <Asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 40px"    runat="server" AutoGenerateColumns="False" Height="320px" Width="488px">    <Columns>     <asp:TemplateColumn>      &...

How to get record count or row count of rows in reporting service table object?
SSRS 2005.  Let's say I define ONE dataset.  In the page layout I drop in 2 tables.  table1 and table2.  In each of these 2 tables, I add a filter. Let's call the field Active Flag, and I filter on "Y" and "No" respectively.  Now, at the bottom of the report(and not inside the tables themselves) I want to drop 2 textboxes onto the page layout.  I want to get the record count from table1, and the record count in table 2, and place it in each textbox at the bottom of the page that shows the total records per table.  Sounds easy, bu...

Arranging data on multiple rows into a sigle row (converting rows into columns)
Hello, I have a survey (30 questions) application in a SQL server db. The application uses several relational tables. The results are arranged so that each answer is on a seperate row: user1   answer1user1   answer2user1   answer3user2   answer1user2   answer2user2   answer3 For statistical analysis I need to transfer the results to an Excel spreadsheet (for later use in SPSS). In the spreadsheet I need the results to appear so that each user will be on a single row with all of that user's answers on...

DetailsView and getting data from a row or field?! VB.NET
I am using Visual web developer and ASP.NET 2.0 using VB.NET Basically i have a detailsview which is bound to a database. It has two rows which are User and Status.. E.g. User   : Sean       Status: OnlineNow the trouble is that i wish to get the data of the User row and insert it back into another database, dont worry the SQL is not my problem..What i need help with is getting the data that it is in that row! I can get the selected page index but not what i need! Also if you know how to do this uding the grid view, form view or any data repr...

How to set the row number and get the row number for ("selected row) in Datagrid
I face a problem that i can't get the selected row of the datagrid in both server side script ( and client side script (javascript) As i can't use the button column / hyperlink column in the datagrid. Thus , i need to handle the change the background color of the selected row and also to get the selected row number for Update/Delete. Thanks in advance. This depends on how do you perform the selection. Do you want to implement chaning of the SelectedRow at the client side without any postback? If you have no issues with postback, then you can do s little trick here: 1. ...

(Fwd) How to loop through a database, row by row, and select and update one row at a time
----- Forwarded message from Troy Mulder <> ----- Date: Mon, 12 Apr 2010 17:48:37 -0400 From: Troy Mulder <> To: Subject: How to loop through a database, row by row, and select and update one row at a time Hello Tim (is it Dr. Bunce?), My name is Troy Mulder, and I am trying to get a perl script to interface with a PostgreSQL database. I am trying to step through each row of the database, and read one column of the row, and update another column of the row. When I follow the online tutorial a...

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 update Data of First row when I delete Data of Second row?
 assume my Table's Columns looks like below1  ,   Annie,   History, pass, 12  ,   Colin,   Math   , Pass, 17  ,   Annie,   History, Fail, 011 ,   Colin,   Math   , Fail, 0you can see the No1 and No7 with same people AnnieWhen I delete the No7's Data I want to update which last 2 columns "Fail" and "1" to No1so After I delete Row of No.7 the result will looks like below1  ,   Annie,   History, Fail, 0 ( update this row'...

Web resources about - How to get data from database row by row -

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

VTech Admits Lack of Database Security Opened Door to Hack
A SQL injection, a common software flaw, was found to be the root cause in the VTech breach.

MapR And Big Data In The World's Largest Biometric Database Project
India?s Aadhaar project is one of the world?s most ambitious big data projects, aiming to collect, store, and utilize biometric information from ...

Trump calls for surveillance of some mosques, attempts to clarify remarks on Syrian database
Trump calls for surveillance of some mosques, attempts to clarify remarks on Syrian database

Resources last updated: 12/3/2015 8:15:36 PM