Solved

Both DataSource and DataSourceID are defined on Gridview Remove one definition.

Posted on 2009-05-06
8
2,489 Views
Last Modified: 2012-05-06
I have just finished developing an application and once i moved it to production i started getting that error:

 Exception information:
    Exception type: System.InvalidOperationException
    Exception message: Both DataSource and DataSourceID are defined on 'grv_rslts'.  Remove one definition.

Request information:
    Request URL: ***
    Request path: ***
    User host address: ***
    User: ***
    Is authenticated: True
    Authentication Type: Forms
    Thread account name: NT AUTHORITY\NETWORK SERVICE

Thread information:
    Thread ID: 16
    Thread account name: NT AUTHORITY\NETWORK SERVICE
    Is impersonating: False
    Stack trace:    at System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView()
   at System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

 It doesnt happen all the time, 80% of the time it works fine and plus i will get the same error for different controls. I googled some stuff and i found
 that i need to remove the datasouce id from mark up ( which i never had) and try to set the datasourceid to null or String.Empty which also didnt help.

My site runs using Master pages, abit of Ajax, membership and role management. When i run in development or on my local pc it works great, only when in production.

Any idea on what might cause that?
0
Comment
Question by:urir10
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24321445
Sounds like at some point your code is setting the DataSourceID or DataSource property of the 'grv_rslts' grid when the other is already set.  If you want to post your html markup and code behind for the page/control where 'grv_rslts' is on, I will be happy to look at it.
0
 
LVL 7

Author Comment

by:urir10
ID: 24326111
Thanks wht1986

I only assign the datasource in code behind. I also searched the whole project for DataSourceID and didnt find anything.  Funny thing is 80% of the time it works fine, and i also get that issue with drop down lists and other gridviews. One thing i noticed is that this mostly happens when the server is working really slow (its an old server) could that have anything to do with it?
Also i took out anything that has to do with caching from the web.config file so if there is any other place i need to look for i will check that.

Here is my code for one of the pages that i  get that error from:
public partial class Pages_EventViewer : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        try

        {

            if (RadioButton1.Checked)

            {

               

                grv_list.DataSource = SqlHelper.LoadEvents(true);

                grv_list.DataBind();
 

            }

            else

            {

                

                grv_list.DataSource = SqlHelper.LoadEvents(false);

                grv_list.DataBind();
 

            }

            Label1.Text = "Total Count: " + grv_list.Rows.Count;
 

        }

        catch

        {
 

        }
 
 
 

    }

    protected void grv_list_RowEditing(object sender, GridViewEditEventArgs e)

    {

        Label lbl = (Label)grv_list.Rows[e.NewEditIndex].FindControl("lbl_id");
 

        DataSet ds = new DataSet();

        ds = SqlHelper.load_Error(lbl.Text);

        TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();

      

    }

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)

    {

        try

        {

            RadioButton2.Checked = false;

          

            grv_list.DataSource = SqlHelper.LoadEvents(true);

            grv_list.DataBind();

            Label1.Text = "Total Count: " + grv_list.Rows.Count;

        }            

        catch

        {
 

        }

    }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)

    {

        try

        {

            RadioButton1.Checked = false;

           

            grv_list.DataSource = SqlHelper.LoadEvents(false);

            grv_list.DataBind();

            Label1.Text = "Total Count: " + grv_list.Rows.Count;

        }

        catch

        {
 

        }

    }

}
 
 

<%@ Page Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="EventViewer.aspx.cs" Inherits="Pages_EventViewer" Title="Event Viewer" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

<script type='text/javascript'>

    function cancelClick() {

       

    }

</script>

     <font class='pageTitleB'>

         <asp:Label ID="lbl_MenuTitle" runat="server" Text="Event Viewer"></asp:Label></font><br />

     <hr />

    <asp:Label id="Label1" runat="server" Width="183px"></asp:Label>

    <asp:RadioButton id="RadioButton1" runat="server" AutoPostBack="True" Checked="True"

        OnCheckedChanged="RadioButton1_CheckedChanged" Text="Show Today's">

    </asp:RadioButton>

    <asp:RadioButton id="RadioButton2" runat="server" AutoPostBack="True" Text="Show All" OnCheckedChanged="RadioButton2_CheckedChanged">

    </asp:RadioButton>

    <br />

     <div  style='height: 250px; overflow: auto;' >

    <asp:GridView ID="grv_list" runat="server" CssClass="Grid" Width="99%" AutoGenerateColumns="False" OnRowEditing="grv_list_RowEditing">

        <HeaderStyle CssClass="GridHeader"></HeaderStyle>

        <AlternatingRowStyle CssClass="GridAtlItem"/>

        <RowStyle CssClass="GridItem"/>

        <Columns>

                 <asp:TemplateField HeaderText="ID" Visible="False">

                                                <EditItemTemplate>

                                                </EditItemTemplate>

                                                <ItemTemplate>

                                                    <asp:Label ID="lbl_id" runat="server" Text='<%# Eval("EventId") %>'></asp:Label>

                                                </ItemTemplate>

                                                <FooterTemplate>

                                                </FooterTemplate>

                                                <ItemStyle HorizontalAlign="Center" />

                                            </asp:TemplateField>

                                            

            <asp:TemplateField HeaderText="Date and Time" >

                <ItemTemplate >

                    <asp:LinkButton ID="lbl_ee" runat="Server" CommandName="Edit" Text='<%# DataBinder.Eval(Container.DataItem, "EventTime") %>'

                         />

                </ItemTemplate>

                <ItemStyle Width="150px" />

            </asp:TemplateField>

          <asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" SortExpression="RequestUrl" ></asp:BoundField>

       </Columns>

    </asp:GridView>

    </div>

      <hr />

       <font class='pageTitleB'>

         <asp:Label ID="Label2" runat="server" Text="Event Details"></asp:Label></font><br />

   

    <asp:TextBox id="TextBox1" runat="server" Height="500px" TextMode="MultiLine" Width="100%">

    </asp:TextBox>
 

 
 
 

</asp:Content>

Open in new window

0
 
LVL 12

Expert Comment

by:wht1986
ID: 24326254
on the code you posted in the initial question the error is happening in a control called "grv_rslts"
However, grv_rslts does not appear in the code you just posted.  Which page or control is grv_rslts defined in?
0
 
LVL 7

Author Comment

by:urir10
ID: 24326309
I have two pages that are almost the same, both give me the same problem. I just copied that one because it was shorter.
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 12

Accepted Solution

by:
wht1986 earned 125 total points
ID: 24326777
Well, I dont see any glaring issues other than it seems to be alot of needless bindings.  Might I suggest a RadioButtonList control like below.  This keeps the radio buttons working in a group so only 1 can be selected at a time. And since there is only 2 of them, i am using the value as a boolean to pass into your SqlHelper function.  In this manner, only 1 data bind is ever performed on a postback.
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" RepeatDirection="Horizontal">

            <asp:ListItem Text="Show Today's" Value="true" Selected="True" />

            <asp:ListItem Text="Show All" Value="false" />

        </asp:RadioButtonList>
 

================
 

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!this.IsPostBack)

        {

            grv_list.DataSource = SqlHelper.LoadEvents(true);

            grv_list.DataBind();

        }

    }
 

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        bool arg = bool.Parse(this.RadioButtonList1.SelectedValue);

        grv_list.DataSource = SqlHelper.LoadEvents(arg);

        grv_list.DataBind();

    }

Open in new window

0
 
LVL 7

Author Comment

by:urir10
ID: 24329487
Thanks wht1986 but i sort of figured that out.
The error message had nothing to do with the gridview lol. I was using static methods in my sqlhelper class and when the server was under a lot of load i started getting funny results. Once i removed the static methods it started working fine.
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24329819
Glad it is working, though take a look at how I did the RadioButtonList. It is a more elegant and less verbose way of handling the rebinding.  And if I was able to assist please rate my response in some manner, im racing a friend for points where the loser has to buy lunch tomorrow :)
0
 
LVL 7

Author Closing Comment

by:urir10
ID: 31578703
Thanks
0

Featured Post

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

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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