This is a migrated thread and some comments may be shown as answers.

[Solved] error on programmatic addition of SqlDataSource

3 Answers 123 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 21 Aug 2009, 01:42 AM
I am using dynamic programmatic addition of a SqlDataSource in the page_init handler using Me.Controls.Add(sds) where sds is the SqlDataSource.

Then I set the trgrid.DataSourceID = sds.ID and trgrid.MasterTableView.DataSourceID = sds.ID and do a trgrid.Rebind().

And everything works fine on initial page access, postbacks, etc, as long as I do NOT put the code in a If Not Page.IsPostBack block.

If I do put it in a If Not Page.IsPostBack block then the initial page access works fine, but any postback bombs with the following error:

The DataSourceID of 'trgrid' must be the ID of a control of type IDataSource.  A control with ID 'sds' could not be found.

So how to fix this error? Should the If Not Page.IsPostBack block of code for programmatic addition of the SqlDataSource be put in a different event handler other than the Init event handler?

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Aug 2009, 06:52 AM
Hi Carl,

Try modifying your code as shown below and see whether it helps.

CS:
 
 protected void Page_Init(object sender, System.EventArgs e) 
    { 
        SqlDataSource sds = new SqlDataSource(); 
        sds.ID = "sds1"
        sds.ConnectionString = "Data Source=INC156;Initial Catalog=NorthWind;User ID=sa;Password=softinc"
        sds.SelectCommand = "SELECT [ProductID], [ProductName], [SupplierID] FROM [Products]"
        sds.DataBind(); 
        this.Page.Controls.Add(sds); 
 
        if (!IsPostBack) 
        { 
            RadGrid1.DataSourceID = sds.ID; 
        } 
    } 


Princy
0
Carl
Top achievements
Rank 1
answered on 21 Aug 2009, 06:47 PM
No that does not work.

I should also clarify that the SqlDataSource is added to a *.ascx user control. So the Init handler is the the Init handler on the *.ascx user control. So far everything I've tried does NOT work if placed in a If Not Page.IsPostBack code block. Somehow the ViewState is being ignored or nor accessed because there is data in the ViewState for the SqlDataSource following the initial page access but then it is not retrieved on the postbacks... So how to fix that problem?
0
Veli
Telerik team
answered on 25 Aug 2009, 08:42 AM
Hi Carl,

You should not use Not Page.IsPostBack with Page_Init. If you use Page_Init, your controls need to be recreated on every postback.

Regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Carl
Top achievements
Rank 1
Veli
Telerik team
Share this question
or