Link to home
Start Free TrialLog in
Avatar of sutorius
sutoriusFlag for United States of America

asked on

Has anyone seen this error when assigning a datasource?

Error: Data properties on data control 'EditMgrGridView' such as DataSource, DataSourceID, and DataMember cannot be changed during the databinding phase of the control.

public void EditMgrGridView_DataBind(int nPageIndex, int intMaxRows, string strSortExpression)
        {
            bool bComplete = false;
            string strSortBy = "";
            string strSortOrder = "";

            while (!bComplete)
            {
                string strSPName = "sp_web_edit_manager";

                SqlConnection oConn = new SqlConnection(DBConnectionString);
                SqlCommand lCommand = new SqlCommand(strSPName, oConn);
                SqlParameter lParm;
                lCommand.CommandType = CommandType.StoredProcedure;
                lCommand.CommandTimeout = Convert.ToInt32(ConfigurationSettings.AppSettings["dbCommandTimeout"]);

                lParm = lCommand.Parameters.Add("@tabselected", SqlDbType.VarChar);
                lParm.Value = strRQStringR;
                lParm.Direction = ParameterDirection.Input;

                lParm = lCommand.Parameters.Add("@stateselected", SqlDbType.VarChar);
                lParm.Value = strRQStringS;
                lParm.Direction = ParameterDirection.Input;

                lParm = lCommand.Parameters.Add("@nbr_edits", SqlDbType.Int);
                lParm.Value = Convert.ToInt32(ddlNumOfRows.SelectedItem.Value);
                lParm.Direction = ParameterDirection.Input;

                lParm = lCommand.Parameters.Add("@page", SqlDbType.Int);
                lParm.Value = nPageIndex;
                lParm.Direction = ParameterDirection.Input;

                SqlParameter nbrPages = new SqlParameter("@nbr_pages", SqlDbType.Int);
                nbrPages.Direction = ParameterDirection.Output;
                lCommand.Parameters.Add(nbrPages);

                lParm = lCommand.Parameters.Add("@sort_option", SqlDbType.VarChar);
                if(EditMgrGridView.SortExpression == "")
                    lParm.Value = "edit_mast.edit_id";
                else
                    lParm.Value = EditMgrGridView.SortExpression;
                lParm.Direction = ParameterDirection.Input;

                lParm = lCommand.Parameters.Add("@desc", SqlDbType.Int);
                lParm.Value = EditMgrGridView.SortDirection;
                lParm.Direction = ParameterDirection.Input;

                SqlParameter totalEdits = new SqlParameter("@total_edits", SqlDbType.Int);
                totalEdits.Direction = ParameterDirection.Output;
                lCommand.Parameters.Add(totalEdits);

                lParm = lCommand.Parameters.Add("@user_id", SqlDbType.VarChar);
                lParm.Value = UserID;
                lParm.Direction = ParameterDirection.Input;

                oConn.Open();
                               
                 lReader = lCommand.ExecuteReader();

                DataTable dt = new DataTable();
               
                dt.Load(lReader);
               
                lReader.Close();

                int nNumberOfPages = Convert.ToInt32(nbrPages.Value.ToString());
               
                int nTotalNumEdits = Convert.ToInt32(totalEdits.Value.ToString());

                oConn.Close();

                EditMgrGridView.PageIndex = nPageIndex;
                //EditMgrGridView.PageCount = nNumberOfPages * this.EditMgrGridView.PageSize;

                EditMgrGridView.DataSource = dt;  ////// Getting error on this line


                EditMgrGridView.DataBind();
               
     
            }
        }
Avatar of raterus
raterus
Flag of United States of America image

Look at the function you are in, it appears to me this already a DataBind method for the EditMgrGridView.  This function is called whenever you DataBind the Grid.  However, in this function, it looks like you are reassigning the DataSource of the very same control again, and attempting to Bind again.  This would lead to all sorts of problems, so you can't do this.  What exactly are you trying to do ?
Avatar of sutorius

ASKER

Hey Raterus,

I am trying to enter the page for the very first time. I have a SqlDataSource hooked to a GridView. I have the select paramaters for the stored proc setup. Below is my aspx. I know that when the page loads the OnDataBound is getting loaded which is then calling the function above. I should turn on tracing.

<asp:SqlDataSource ID="sqldsEditManager" runat="server" ConnectionString="<%$ ConnectionStrings:cltg2w6431webdevConnectionString %>"
                    SelectCommand="sp_web_edit_manager" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="tabselected" QueryStringField="r" Type="String" />
                        <asp:QueryStringParameter Name="stateselected" QueryStringField="s" Type="String" />
                        <asp:ControlParameter ControlID="ddlNumOfRows" DefaultValue="10" Name="nbr_edits"
                            PropertyName="SelectedValue" Type="Int32" />
                        <asp:ControlParameter ControlID="EditMgrGridView" DefaultValue="1" Name="page" PropertyName="SelectedValue"
                            Type="Int32" />
                        <asp:Parameter Direction="InputOutput" Name="nbr_pages" Type="Int32" />
                        <asp:Parameter DefaultValue="edit_mast.edit_id" Name="sort_option" Type="String" />
                        <asp:Parameter DefaultValue="1" Name="desc" Type="Byte" />
                        <asp:Parameter Direction="InputOutput" Name="total_edits" Type="Int32" />
                        <asp:CookieParameter CookieName="UserID" DefaultValue="Install" Name="user_id" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
<anthem:GridView ID="EditMgrGridView" runat="server" Width="100%" AutoGenerateColumns="False"
                    AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" DataKeyNames="edit_id"
                    PagerSettings-Position="TopAndBottom" AlternatingRowStyle-BackColor="#f1f1f1"
                    UpdateAfterCallBack="False" DataSourceID="sqldsEditManager" OnDataBound="EditMgrGridView_DataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Error Number" SortExpression="edit_id">
                            <itemstyle width="5%" />
                            <itemtemplate>
                                <asp:Label ID="lblErrorNumber" runat="server" Text='<%# Bind("edit_id") %>'></asp:Label>
                            </itemtemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Override Allowed">
                            <itemstyle width="5%" />
                            <itemtemplate>
                                <asp:CheckBox ID="chkOverrideAllow" runat="server"/>
                            </itemtemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Biller Assigned">
                            <itemtemplate>
                                <asp:DropDownList ID="ddlBillerAssign" runat="server" DataSourceID="ObjectDataSource3" DataTextField="staff_name" DataValueField="signon_id">
                                </asp:DropDownList>
                            </itemtemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Include In Edit Stats">
                            <itemstyle width="5%" />
                            <itemtemplate>
                                <asp:CheckBox ID="chkEditStats" runat="server" />
                            </itemtemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Error Message Name" SortExpression="edit_name">
                            <itemtemplate>
                                <asp:TextBox Width="225" ReadOnly=true ID="txtErrorMsgName" runat="server" Height="40px" Text='<%# Bind("edit_name") %>' TextMode=multiLine></asp:TextBox>
                            </itemtemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Error Message" SortExpression="error_msg">
                            <itemtemplate>
                                <asp:TextBox ReadOnly=true ID="txtErrorMsg" runat="server" Height="40px" Text='<%# Bind("error_msg") %>'
                                    TextMode="MultiLine" Width="275"></asp:TextBox>
                            </itemtemplate>
                        </asp:TemplateField>
                    </Columns>
                    <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    <PagerSettings Position="TopAndBottom" />
                    <AlternatingRowStyle BackColor="#F1F1F1" />
                </anthem:GridView>
I guess I'm a bit confused why you need the DataBind code you first posted.  Is there something you need to do there that you can't do using the SqlDataSource?
Custom paging and sorting and the user selects the number of rows they want to display. The stored proc I have only returns back the number of rows needed and the page the user is on, etc.

Here is the stored proc header:
CREATE    procedure sp_web_edit_manager (
 @tabselected varchar(5) -- which tab was selected
,@stateselected varchar(5) = null  -- which state was selected, optional
,@nbr_edits int = 10 --Number of edits per page
,@page int = 1 --Which page should be returned
,@nbr_pages int =1 output --Let the app know how many pages there are
,@sort_option varchar(255) = 'edit_mast.edit_id' --How should we sort: 1=edit_id, 2=edit_name, 3=error_msg
,@desc tinyint = 0 --What is the sort order 0 = ascending, 1 = descending
,@total_edits int = 0 output --How many edits are there for this tab
,@user_id varchar(20) = 'Install'
) as
Ok, I understand you have that stored procedure, but I still don't see why you need that databind code like that.  From the looks of it, you already have the parameters set up in the SqlDataSource?  Are those not working?
So how would I test that? Take out OnDataBound?
Yes, I'd start out by first removing that entire function, I don't think you really need it.
While working with this project I thought I had done that and it gave a blank grid. I tried it again. Took out OnDataBound and Commented the OnDataBound method. I had tracing turned on and I had profiler on. Tracing shows _ctl0:maincontent:EditMgrGridView Anthem.GridView 69 56 28  and profiler does not show the stored procedure firing. That's why I had the OnDataBound in there.
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot to say that I got the same results as before, a blank grid. Sorry.
Even if I am doing custom paging??? I understand sortexpression and direction. But knowing what page the person selected and how many rows that want to display. In the pager row the gridview wouldn't what to display. I would need to create that myself, right?
I'm really not prepared to help you troubleshoot this,  I just wanted to let you know about the error!  I haven't used the DataSource controls that much, and would hate to give you bad advice/learn as I go advice!  You might want to look at asking another question just about the blank grid, and see what the other experts have to say about it.  Your first question has been answered I hope, as you why you were getting that error.
Ok, thanks.