Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
if i used this code then it shown error like there is already an open datareader associated with this command.
please help me where i closed this datareader.
SqlConnection con = Database.GetConnection();
        SqlCommand com = new SqlCommand("SELECT * FROM category WHERE article_allow = 1", con);
        string article_visit = "0";
        SqlDataReader rs = com.ExecuteReader();
        while (rs.Read())
        {
            SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list WHERE cat_ID = @catID AND article_status=1", con);
            cmd.Parameters.AddWithValue("catID", rs["cat_ID"]);
            SqlDataReader rst = cmd.ExecuteReader();
            while (rst.Read())
            {
                article_visit = rst["article_visit"] + article_visit;
            }
            rst.Close();
            rst.Dispose();
            cmd.Dispose();
        }
        rs.Close();
        rs.Dispose();

please help how to solve this.
Thanks in advance
Posted
Updated 19-Jun-11 20:17pm
v7
Comments
Ra-one 21-Jun-11 2:42am    
Do not just copy paste. try to understand the code... Many typo errors... Are you using classic asp before if so than move on and learn latest concept.

Find your own path we are just the helpers in your way, its not our journey... :)

This is C# and the syntax has to be that of C# only. Use square brackets and '+' instead:
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" + rs["cat_ID"] + " and article_status=1", con);
in place of
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" & rs("cat_ID") & " and article_status=1", con);

and
article_visit = rst["article_visit"] + article_visit;
in place of
article_visit = rst("article_visit") + article_visit;

I believe you have copied it from somewhere!
 
Share this answer
 
v2
Replace your code with...

SqlConnection con = Database.GetConnection();
        SqlCommand com = new SqlCommand("select * from category where article_allow = 1", con);
        SqlDataReader rs = new SqlDataReader();
        SqlDataReader rst = new SqlDataReader();
        
        string article_visit = "0";
        rs = com.ExecuteReader();
        while (rs.Read())
        {
           
            SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" + rs["cat_ID"] + " and article_status=1", con);
            rst = cmd.ExecuteReader();
            while (rst.Read())
            {
               
                article_visit = rst["article_visit"] + article_visit;
            }
            
            rst.Close();
 
            
            cmd.Clone();
        }
        rs.Close();



Now whats the problem in your code : Notice that rs("cat_ID") is a method call, while it should be like this rs["cat_ID"]. Similarly for rst("article_visit") is rst["article_visit"]. also the + sign in place of &

Hope you got the point ....... :)
 
Share this answer
 
v3
Comments
rajendrabablu 13-Mar-13 2:37am    
Hi..my self rajendra..
I tried this code but i am not able to fetch the data from database....
How you create NEW instance of SqlDataReader ?
Do not create new instance of SQLDatareader, it will give you complie time error.

may be your older version of code running.
 
Share this answer
 
v2
In addition to the two postings above..

1. You have a typo in your first command string.. (seelct instead of select)
2. Do yourself a favor and use parameterized calls, always, even if your var is read from a database.
3. When I'm not wrong
SqlDataReader rs = new SqlDataReader()
should result in an error.

See code below...

SqlConnection con = Database.GetConnection();
SqlCommand com = new SqlCommand( "SELECT * FROM category WHERE article_allow = 1", con );
string article_visit = "0";
SqlDataReader rs = com.ExecuteReader();

 /* create the second connection here
  * SqlConnection secondCon = new SqlConnection ( connectionString );
  * or if your method Database.GetConnection() returns a new object use this instead
  * SqlConnection secondCon = Database.GetConnection();
  */
while ( rs.Read() )
{
    /* use second connection here */
    SqlCommand cmd = new SqlCommand( "SELECT article_visit FROM article_list WHERE cat_ID = @catID AND article_status=1", secondCon );
    cmd.Parameters.AddWithValue( "catID", rs[ "cat_ID" ] );
    SqlDataReader rst = cmd.ExecuteReader();
    while ( rst.Read() )
    {
        article_visit = rst[ "article_visit" ] + article_visit;
    }
    
    rst.Close();
    rst.Dispose();
    cmd.Dispose();
}

rs.Close();
rs.Dispose();


Edit.
Sorry, my fault.
It is not possible to use the same connection for the second reader while the first is still open.
You have to use another connection to your database for the second reader.

So create a new connection and modify the second SqlCommand to use the new connection..

Edit 2/3
Updated Code example
 
Share this answer
 
v4
Comments
rahul dev123 20-Jun-11 2:46am    
It display same value for every articles, i have a page where i can display articles category list and under this category display article visit but it display same value for every article category.
SvenMe 20-Jun-11 3:41am    
And probably it is a big number, am I right?
The code above concatenates all visits in one string.
Obviously that's not what you want.

Maybe you shouldn't just copy and paste code from somewhere and expect then someone else rewrite it for you to fit your needs.
Try to find the solution by your own. When you are stuck, come back and ask again. Maybe you also want to read a bit more about SQL.

Best regards

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