Solved

Datareader error

Posted on 2013-06-20
5
249 Views
Last Modified: 2013-06-30
Hi,

When I try to run the following code, I get an error

"No data exists for the row/column. "

 what is wrong with the code?

 protected void bindFiles()
    {
        OleDbConnection cn = new OleDbConnection(...
        OleDbCommand cmd2 = new OleDbCommand(...
        try
        {
            cn.Open();
           OleDbDataReader dr = cmd2.ExecuteReader();

           Context.Session["x"] = dr[2].ToString();
           Context.Session["y"] = dr[3].ToString();
           Context.Session["z"] = dr[4].ToString();

            gvSTLFiles.DataSource = dr;
            gvSTLFiles.DataBind();

            dr.Close();
        }
        catch
        {
            throw;  <----- error here
        }
        finally
        {
            cn.Close();
            cn.Dispose();
        }

Open in new window


Please help me to solve it.

thanks

ayha
0
Comment
Question by:ayha1999
5 Comments
 
LVL 27

Accepted Solution

by:
Sammy earned 84 total points
ID: 39265045
You need to call the read method after the execute reader call
See full example here
http://csharp-station.com/Tutorial/AdoDotNet/Lesson04
0
 
LVL 12

Assisted Solution

by:Jitendra Patil
Jitendra Patil earned 83 total points
ID: 39265096
try the below code
        protected void bindFiles()
    {
        OleDbConnection cn = new OleDbConnection("your connection string");
        OleDbCommand cmd2 = new OleDbCommand("your sql query");
        try
        {
            cn.Open();
            OleDbDataAdapter da= new OleDbDataAdapter(cmd2.ToString() ,cn);
            DataSet Ds= new DataSet();
            da.Fill(Ds); 
 
            foreach(DataRow dr in Ds.Tables[0].Rows)
            {
           Context.Session["x"] = dr[2].ToString();
           Context.Session["y"] = dr[3].ToString();
           Context.Session["z"] = dr[4].ToString();
            }

            gvSTLFiles.DataSource = Ds.Tables[0];
            gvSTLFiles.DataBind();

        }
        catch
        {
            throw;  
        }
        finally
        {
            cn.Close();
            cn.Dispose();
        }
            }

Open in new window

hope this helps.
0
 
LVL 4

Assisted Solution

by:AnuTiji
AnuTiji earned 83 total points
ID: 39265169
Hi

The datareader object cannot be set as datasource to gridview without reading. Below link details the code for binding data reader to gridview.

http://www.codeproject.com/Articles/29644/dataGridview-Using-DataReader
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39265362
hi.. you have to write code like this

protected void bindFiles()
    {
        OleDbConnection cn = new OleDbConnection(...
        OleDbCommand cmd2 = new OleDbCommand(...
        try
        {
            cn.Open();
           OleDbDataReader dr = cmd2.ExecuteReader();
while dr.read()
{
           Context.Session["x"] = dr[2].ToString();
           Context.Session["y"] = dr[3].ToString();
           Context.Session["z"] = dr[4].ToString();
}
end while
            gvSTLFiles.DataSource = dr;
            gvSTLFiles.DataBind();

            dr.Close();
        }
        catch
        {
            throw;  <----- error here
        }
        finally
        {
            cn.Close();
            cn.Dispose();
        }
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 39288394
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now