Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code:
C#
using (conn)
{
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        rptbio.DataSource = dr;
        rptbio.DataBind();
        dr.Read();
        int a = dr.GetInt32(1);
        dr.Dispose();
        var cmd1 = new SqlCommand("link", conn) { CommandType = CommandType.StoredProcedure };
        cmd1.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = a;
        SqlDataReader dr1 = cmd1.ExecuteReader();
        rptlink.DataSource = dr1;
        rptlink.DataBind();
        dr1.Dispose();
        conn.Close();
    }
}


I use dr.read() but I still get this error!

System.InvalidOperationException: Invalid attempt to read when no data is present

my sp work fine i test and i search the web! nothing found!
Posted

1 solution

This code makes no snese.

C#
rptbio.DataSource = dr;
rptbio.DataBind();
dr.Read();
int a = dr.GetInt32(1);
dr.Dispose();


The first two lines read ALL the data in to the control. The next line tries to read data, but it's ALL alrady been read, that's what DataBind does.
 
Share this answer
 
Comments
taha bahraminezhad Jooneghani 20-Aug-12 7:15am    
what is your suggestion?
taha bahraminezhad Jooneghani 20-Aug-12 7:17am    
I love you man!:)
I change the databind place and its work thanks!
Christian Graus 20-Aug-12 7:18am    
No worries, but, be careful. If you did the databind AFTER that read, it works, but it won't be reading the first row now, because you read it already.
Christian Graus 20-Aug-12 7:18am    
You need to read the data again, or read it from the control. The data reader has iterated over all the data in the collection already and is at the end.
taha bahraminezhad Jooneghani 20-Aug-12 7:23am    
can you give me a code that i cand use each both and don't mess up mith each other?
i want to read and i want to bind, how can i do this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900