Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Has anyone seen this error when assigning a datasource?

Posted on 2006-06-09
13
Medium Priority
?
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 1500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

610 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