Solved

Datareader error

Posted on 2013-06-20
5
251 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 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

10 Experts available now in Live!

Get 1:1 Help Now