How to read outside the dataReader

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    How to read outside the dataReader

    Hello

    Let me show you the code I'm trying to do and then I'll ask the question:

    Code:
    	  
    SqlDataReader rdr = null;
    SqlConnection conn = new SqlConnection(strConn);
    string strSQL = "SELECT * FROM View_ProductRange WHERE "+sqlSearch+"";     		
    SqlCommand cmd = new SqlCommand(strSQL, conn);
    
    conn.Open();
    
    rdr = cmd.ExecuteReader();
    		
    int rdrCount = 0;
    while (rdr.Read())
    	{
                	lbProducts.Text += "<img src='"+rdr["Location"] + "/images/"+rdr["PartNumber"]+".gif' alt='' class='thumb-outerbox' />
    				
    		rdrCount= rdrCount + 1;	
    	}
    		
    if (rdrCount == 1)
    		{
    		Response.Redirect("http://www.site.com/search.aspx?pn="+rdr["PartNumber"]);
    		}
    		
    conn.Close();
    So, query database and loop through the results and write all results to a label showing lots of pictures UNLESS there is only 1 result, where I want to forward to another page. The obvious error is when it tries to forward to another page it can't because it can't read the dataReader as it is outside the loop!

    Any ideas?

    My thoughts were either:
    a) requery database with a count function( eg, SELECT COUNT (*) WHERE xyz = 'xyz' etc) - not keen on this as I don't want to re-open a connection
    b) assign all the partnumbers the reader reads to a session variable
    c) is it possible to write a dataReader to a dataset and then query the dataset or do I need to use the dataAdapter for that?

    Thanks

    Dave
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Either re-query the database on the next page as you've already figured out.
    Or you could store the information that you want to access on the next page in Session. If you store something in Session it becomes available to every page in the site.

    -Frinny

    Comment

    • DaveRook
      New Member
      • Jul 2007
      • 147

      #3
      Thank you Frinny

      So, I assume it is not possible to write a dataReader to a dataset and then query the dataset?

      Thanks - I will go with the session variables!

      Dave

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Store the DataSet into Session
        Then query it in page 2
        ;)

        -Frinny

        Comment

        Working...