Solved

Datareader error

Posted on 2013-06-20
5
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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