Solved

Datareader error

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

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

615 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