Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datareader error

Posted on 2013-06-20
5
Medium Priority
?
259 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 Ageil earned 336 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 13

Assisted Solution

by:Jitendra Patil
Jitendra Patil earned 332 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 332 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

688 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