ASP.NET Issue: The SqlDataSource control does not have a naming container…
I have received the following .NET error several times in the past when developing nested databound controls. I have found the solution does not have good coverage on the Internet. The curious thing about this error is that it does not appear until 2nd or 3rd postback on the page.
[box]The SqlDataSource control does not have a naming container. Ensure that the control is added to the page before calling DataBind[/box]
The cause is simple, however. It occurs when you use a ControlParameter on a DataSource that is within another data control. I ran into it again today on a Gridview within a DataList for example. The error occurs because the inner control cannot find the control containing the parameter for some reason.
The solution is simple as well. You must remove the ControlParameter from the DataSource that is inside the other control. This can be accomplished by using a normal <asp:Parameter> and assigning a DefaultValue to it when the outer control binds.
I followed your advice and changed the ControlParameter to a normal Parameter. This removed the error. Thank you for this.
When I now add a row to that nested GridView, and call BindData on that GridView, in code-behind I can see that a row is added to the GridView, but the row doesn’t show up in the browser. The GridView is in an asp:Panel.
Do you have any ideas about how to fix this?
It’s tough to say without knowing exactly what you are doing. You say you are adding a row, which sounds like you are doing it manually. If you then do a DataBind against a SQLDataSource, it will clear the row. Also, when working with nested GridViews you have to be very careful about when you bind them. I typically do the binding manually in the DataRowBound event of the parent control. Hope that helps.