Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9
This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Special Edition by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar; ISBN: 9780470041789
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 12th, 2008, 10:51 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 12-19 - MARS - "DBCon does not exist"

Since I'm using the code behind model there is no <script runat="server>

My problem is that the second event: gvOrders_RowDataBound does not recognize the DBCon that was made in the previous Page_Load event.

I guess this code is supposed to let the transfer happen:
Code:
        Command.CommandType = CommandType.Text;
        Command.Connection = DBCon;

        // Opening the connection and executing the SQL query. 
        DBCon.Open();
        OrdersReader = Command.ExecuteReader();

        // Binding the Data Reader to the GridView control
        gvOrders.DataSource = OrdersReader;
        gvOrders.DataBind();
Reply With Quote
  #2 (permalink)  
Old February 12th, 2008, 10:56 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 63 Times in 63 Posts
Send a message via AIM to dparsons
Default

I do not have the book in front of me, but if you declare DBCon in your page load, that variable will only be accessible to the Page Load event. Example:

Protected Sub Page_Load(sender as object, e as eventargs)
   Dim DBCon as New SqlConnection() 'This makes DBCon accessible only to the Page_Load method
   '...code
End Sub

Protected Sub gvOrders_RowDataBound(sender as object, e as GridViewRowEventArgs)
        Command.CommandType = CommandType.Text;
        Command.Connection = DBCon; 'Error

        // Opening the connection and executing the SQL query.
        DBCon.Open(); 'Error
        OrdersReader = Command.ExecuteReader();

        // Binding the Data Reader to the GridView control
        gvOrders.DataSource = OrdersReader;
        gvOrders.DataBind();
End Sub

The resolution would be to add
Dim DBCon as New SqlConnection()

to your DataBound event or to declare DBCon as a class level member, which I would advise against.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old February 12th, 2008, 11:12 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I thought about creating the object again in the next method.

However, and I might be wrong, I thought the whole point of Multiple Active Result Sets is that we use the same connection. This would mean that I wouldn't need to create a new connection object for the next method.

Here's what the book has:

Code:
protected void Page_Load(object sender, EventArgs e)
{
...
        SqlConnection DBCon;

        DBCon = new SqlConnection();
...
        Command.Connection = DBCon;

        // Opening the connection and executing the SQL query. 
        DBCon.Open();
        OrdersReader = Command.ExecuteReader();

        // Binding the Data Reader to the GridView control
        gvOrders.DataSource = OrdersReader;
        gvOrders.DataBind();

        // Closing connection after we are done processing all order records
        DBCon.Close();
}

protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
        // Reusing the same connection object that was used in retrieving 
        // allorder records from the Orders table
        Command.Connection = DBCon;

        // Executing SQL query without passing CommandBehavior.CloseConnection 
        // as parameter to ExecuteReader. We don't want the connection
        // to automatically close because we want to reuse it for more operations
        OrderDetailReader = Command.ExecuteReader();
...
}
Supposedly, the gvOrders_RowDataBound method should receive the data source before the connection is closed in the first method.

Reply With Quote
  #4 (permalink)  
Old February 12th, 2008, 11:29 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 63 Times in 63 Posts
Send a message via AIM to dparsons
Default

MARS is not my area of expertise but, it is my understanding, that MARS simply allows you to execute multiple batches per connection what you are doing is trying to access an object that is out of scope in the DataBound method which is a violation of VB and has nothing to do with MARS.

Nevertheless, the RowDataBound event is firing as the GridView is being bound to the datasource from within your Page_Load.

Someone more familiar with the book should weigh in on what the intent of the code in the DataBound method.

Not sure if this has helped you any.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #5 (permalink)  
Old February 12th, 2008, 11:47 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmmm, I'm not sure what the book was getting at then :)

I figured that
Code:
gvOrders.DataSource = OrdersReader;
somehow transported the connection object to the gvOrders. Maybe an object wasn't supposed to be transported. What you're saying makes sense, the object doesn't get transported. I suppose some underlying connection gets copied? Not sure.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DataReader error on Listing 12-19 cJeffreywang BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 January 3rd, 2008 11:36 AM
want to convert date from 01/12/07 to 12-Jan-2007 hurperl Perl 1 April 12th, 2007 05:47 AM
Listing 12 - 8 "Mars" Asp.Net BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 March 5th, 2007 06:33 AM
Errors on Chapter 12 example(12.8) sonnie ASP.NET 2.0 Professional 2 June 7th, 2006 10:55 AM
Chapter 19 johanyu BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 October 10th, 2004 11:30 PM



All times are GMT -4. The time now is 07:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.