Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...Trouble binding to my drop down list from a binding context such as GridView row.Trouble binding to my drop down list from a binding context such as GridView row.
Previous
 
Next
New Post
4/13/2006 12:27 AM
 

Hi all, sorry if this is the wrong forum to post this in. Please tell me what forum to post in if this is the wrong forum. Thanks.

I have a very simple drop down list:

1    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay), _
2    ToolboxData("<{0}:DDLPlantFamilies runat=server></{0}:DDLPlantFamilies>")> _
3    Public Class DDLPlantFamilies
4        Inherits WebControl
5        Implements INamingContainer
6    
7        Public Sub New()
8            MyBase.New()
9        End Sub
10   
11       <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
12       Public Property SelectedValue()
13           Get
14               Return DirectCast(Me.FindControl("ddl"), DropDownList).SelectedValue
15           End Get
16           Set(ByVal value)
17               DirectCast(Me.FindControl("ddl"), DropDownList).SelectedValue = value
18           End Set
19       End Property
20     
21       Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
22           MyBase.Render(writer)
23       End Sub
24   
25       Protected Overrides Sub CreateChildControls()
26           Dim ddl As New DropDownList
27           Me.Controls.Add(ddl)
28   
29           If Not Me.Page.IsPostBack Then
30               Dim listSource As New Bobs.DataObjects.AutoGen._bgd_PlantFamilies
31   
32               Dim rdr As SqlClient.SqlDataReader
33               rdr = listSource.LoadAllActiveDDLFromSqlReader
34   
35               With listSource
36                   ddl.ID = "ddl"
37                   ddl.DataSource = rdr
38                   ddl.DataValueField = listSource.ColumnNames.PlantFamilyID
39                   ddl.DataTextField = listSource.ColumnNames.ScientificFamilyName
40                   ddl.DataBind()
41               End With
42           End If
43       End Sub
44   End Class
45   

When I try to bind to the drop down list from a GridView row, I get the exception that follows. The list is being loaded fine, and works fine. I only get this error when I'm trying to bind to it from inside a binding context such as a GridView row. Please help. I really appreciate it.

-Andy

The Exception I'm getting:
System.InvalidOperationException was caught
  Message="Invalid attempt to FieldCount when reader is closed."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlDataReader.get_FieldCount()
       at System.Data.Common.DbEnumerator.BuildSchemaInfo()
       at System.Data.Common.DbEnumerator.MoveNext()
       at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
       at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
       at System.Web.UI.WebControls.ListControl.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at System.Web.UI.Control.DataBindChildren()
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.Control.DataBindChildren()
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.Control.DataBindChildren()
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
       at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
       at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
       at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
       at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
       at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
       at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at System.Web.UI.WebControls.GridView.DataBind()
       at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
       at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
       at System.Web.UI.Control.EnsureChildControls()
       at System.Web.UI.WebControls.CompositeDataBoundControl.get_Controls()
       at Utils.SetAllDeleteButtonConfirmationScripts(Control parent) in C:\Documents and Settings\Andy\My Documents\Visual Studio 2005\WebSites\BobsGreenhouseDelights\BobsWeb\App_Code\Utilities\Utilities.vb:line 98

 
New Post
4/13/2006 9:12 AM
 

Please post the constructor for Bobs.DataObjects.AutoGen._bgd_PlantFamilies and its method LoadAllActiveDDLFromSqlReader.  The problem seems to be that the reader is returning an empty recordset or that the reader has been closed before databinding.

I'm confused by your statement that "the list is being loaded fine and works fine." Is this in a test with the custom DDL placed outside of a GridView row or have you in stepping through the code execution found that listSource has been populated (rdr.HasRows == True) and that the reader is still open when the line ddl.DataBind() is reached?


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
4/13/2006 9:46 AM
 

Yes. The DDL loads fine except for when it is in the context of data binding inside a GridView row. Here's what I discovered last night. If I remove the "ddl.Databind" statement. Then it works in the GridView. However, then the DDL won't load automatically when it is not inside a databinding context. I've discovered that the GridView invokes the CreateChildControls method of WebControl automatically whether I want it to or not. When the GridView does this and then encounters the ddl.Databind statement is when  the error occurs. I believe that this is a problem that I should discuss with Microsoft--maybe not though, I'm not sure yet if it's me or them. However, this behavior is reproducable. I think it's a logical flaw. I nee a way around it though for now.

-Andy

 
New Post
4/13/2006 1:19 PM
 
I think I see what's happening and believe it is normal behavior.  Try moving all of your databinding code (including the IsPostback check) from the CreateChildControls method into a new Load event handler within the custom control.  The CreateChildControls method should only instantiate the DDL and add it to the control tree, not populate it with data. If the SQL query  to obtain the data that populates the DDL depends on what row is selected in the GridView you may have to place the databinding code in a public DataBind method of your custom control and explicitely call this method from the GridView's ItemDataBound event handler.

Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
4/13/2006 1:23 PM
 
As I was backing up over your first post I also noticed that you are assigning the ID of the DDL within the IsPostback block.  This should always be assigned - immediately after the DDL is instantiated and before the DDL is added to the control tree - not as part of your databinding code.

Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
Previous
 
Next
HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...Trouble binding to my drop down list from a binding context such as GridView row.Trouble binding to my drop down list from a binding context such as GridView row.


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out