How to set MyDataSource property of a Server Control?

Posted on 2007-10-05
Last Modified: 2013-12-17
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.
Question by:kosturdur
    1 Comment
    LVL 16

    Accepted 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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now