sutorius
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(i nt nPageIndex, int intMaxRows, string strSortExpression)
{
bool bComplete = false;
string strSortBy = "";
string strSortOrder = "";
while (!bComplete)
{
string strSPName = "sp_web_edit_manager";
SqlConnection oConn = new SqlConnection(DBConnection String);
SqlCommand lCommand = new SqlCommand(strSPName, oConn);
SqlParameter lParm;
lCommand.CommandType = CommandType.StoredProcedur e;
lCommand.CommandTimeout = Convert.ToInt32(Configurat ionSetting s.AppSetti ngs["dbCom mandTimeou t"]);
lParm = lCommand.Parameters.Add("@ tabselecte d", SqlDbType.VarChar);
lParm.Value = strRQStringR;
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@ stateselec ted", SqlDbType.VarChar);
lParm.Value = strRQStringS;
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@ nbr_edits" , SqlDbType.Int);
lParm.Value = Convert.ToInt32(ddlNumOfRo ws.Selecte dItem.Valu e);
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(nb rPages);
lParm = lCommand.Parameters.Add("@ sort_optio n", SqlDbType.VarChar);
if(EditMgrGridView.SortExp ression == "")
lParm.Value = "edit_mast.edit_id";
else
lParm.Value = EditMgrGridView.SortExpres sion;
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@ desc", SqlDbType.Int);
lParm.Value = EditMgrGridView.SortDirect ion;
lParm.Direction = ParameterDirection.Input;
SqlParameter totalEdits = new SqlParameter("@total_edits ", SqlDbType.Int);
totalEdits.Direction = ParameterDirection.Output;
lCommand.Parameters.Add(to talEdits);
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.V alue.ToStr ing());
int nTotalNumEdits = Convert.ToInt32(totalEdits .Value.ToS tring());
oConn.Close();
EditMgrGridView.PageIndex = nPageIndex;
//EditMgrGridView.PageCoun t = nNumberOfPages * this.EditMgrGridView.PageS ize;
EditMgrGridView.DataSource = dt; ////// Getting error on this line
EditMgrGridView.DataBind() ;
}
}
public void EditMgrGridView_DataBind(i
{
bool bComplete = false;
string strSortBy = "";
string strSortOrder = "";
while (!bComplete)
{
string strSPName = "sp_web_edit_manager";
SqlConnection oConn = new SqlConnection(DBConnection
SqlCommand lCommand = new SqlCommand(strSPName, oConn);
SqlParameter lParm;
lCommand.CommandType = CommandType.StoredProcedur
lCommand.CommandTimeout = Convert.ToInt32(Configurat
lParm = lCommand.Parameters.Add("@
lParm.Value = strRQStringR;
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@
lParm.Value = strRQStringS;
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@
lParm.Value = Convert.ToInt32(ddlNumOfRo
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@
lParm.Value = nPageIndex;
lParm.Direction = ParameterDirection.Input;
SqlParameter nbrPages = new SqlParameter("@nbr_pages",
nbrPages.Direction = ParameterDirection.Output;
lCommand.Parameters.Add(nb
lParm = lCommand.Parameters.Add("@
if(EditMgrGridView.SortExp
lParm.Value = "edit_mast.edit_id";
else
lParm.Value = EditMgrGridView.SortExpres
lParm.Direction = ParameterDirection.Input;
lParm = lCommand.Parameters.Add("@
lParm.Value = EditMgrGridView.SortDirect
lParm.Direction = ParameterDirection.Input;
SqlParameter totalEdits = new SqlParameter("@total_edits
totalEdits.Direction = ParameterDirection.Output;
lCommand.Parameters.Add(to
lParm = lCommand.Parameters.Add("@
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.V
int nTotalNumEdits = Convert.ToInt32(totalEdits
oConn.Close();
EditMgrGridView.PageIndex = nPageIndex;
//EditMgrGridView.PageCoun
EditMgrGridView.DataSource
EditMgrGridView.DataBind()
}
}
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 ?
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:cltg2w64 31webdevCo nnectionSt ring %>"
SelectCommand="sp_web_edit _manager" SelectCommandType="StoredP rocedure">
<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="SelectedValu e" Type="Int32" />
<asp:ControlParameter ControlID="EditMgrGridView " DefaultValue="1" Name="page" PropertyName="SelectedValu e"
Type="Int32" />
<asp:Parameter Direction="InputOutput" Name="nbr_pages" Type="Int32" />
<asp:Parameter DefaultValue="edit_mast.ed it_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="To pAndBottom " AlternatingRowStyle-BackCo lor="#f1f1 f1"
UpdateAfterCallBack="False " DataSourceID="sqldsEditMan ager" OnDataBound="EditMgrGridVi ew_DataBou nd">
<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="ObjectDataSo urce3" 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:T extBox>
</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 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:cltg2w64
SelectCommand="sp_web_edit
<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="SelectedValu
<asp:ControlParameter ControlID="EditMgrGridView
Type="Int32" />
<asp:Parameter Direction="InputOutput" Name="nbr_pages" Type="Int32" />
<asp:Parameter DefaultValue="edit_mast.ed
<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="To
UpdateAfterCallBack="False
<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="ObjectDataSo
</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:T
</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?
ASKER
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
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?
ASKER
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.
ASKER
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:EditMgrG ridView 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I forgot to say that I got the same results as before, a blank grid. Sorry.
ASKER
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.
ASKER
Ok, thanks.