• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

How to set MyDataSource property of a Server Control?

I have a server control inherited from CompositeControl. It has a grid and I want to set the grid's data source property from the Property Window. The datasource is one of the SqlDataSources on a page.
0f I use a syntax like;

        public virtual SqlDataSource DataSource
                return (SqlDataSource)ViewState["DataSource"];

                ViewState["DataSource"] = value;
I can set the DataSource but got that error at design time;
"Cannot create an object of type 'System.Web.UI.WebControls.SqlDataSource' from its string representation 'SqlDataSource1' for the 'Database' property"

I'm using ASP.Net with C# if necessary.
1 Solution
SqlDataSource is a control and I think it is not a good idea to save the reference of a control in a property.  Instead it is better to store the ID of the control and then get the reference to it when needed.  This is what you find in other standard controls like gridview.  You assign the ID of an exsiting SQLDataSource control in the gridview's datasourceID property but not the datasource property in the designer.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now