Solved

Has anyone seen this error when assigning a datasource?

Posted on 2006-06-09
13
506 Views
Last Modified: 2010-05-18
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();
               
     
            }
        }
0
Comment
Question by:sutorius
  • 7
  • 6
13 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 16872349
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 ?
0
 
LVL 2

Author Comment

by:sutorius
ID: 16872455
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>
0
 
LVL 33

Expert Comment

by:raterus
ID: 16872520
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?
0
 
LVL 2

Author Comment

by:sutorius
ID: 16872579
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
0
 
LVL 33

Expert Comment

by:raterus
ID: 16872653
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?
0
 
LVL 2

Author Comment

by:sutorius
ID: 16872737
So how would I test that? Take out OnDataBound?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 33

Expert Comment

by:raterus
ID: 16872860
Yes, I'd start out by first removing that entire function, I don't think you really need it.
0
 
LVL 2

Author Comment

by:sutorius
ID: 16872945
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.
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 16872967
Well you shouldn't need it there if you are using the SqlDataSource control.  I'd have to guess you are doing something else wrong, but there is no reason to hook up to the DB manually like that if you are going to use the SqlDataSource controls.
0
 
LVL 2

Author Comment

by:sutorius
ID: 16872977
I forgot to say that I got the same results as before, a blank grid. Sorry.
0
 
LVL 2

Author Comment

by:sutorius
ID: 16873027
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?
0
 
LVL 33

Expert Comment

by:raterus
ID: 16873049
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.
0
 
LVL 2

Author Comment

by:sutorius
ID: 16873073
Ok, thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

20 Experts available now in Live!

Get 1:1 Help Now