Solved

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

Posted on 2009-05-06
8
2,539 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Jquery datatables integration with ASP.NET MVC, bootstrap 3 22
Obtaining data on ASP 8 29
VBScript on Html 15 47
return false must be hit after calling certain command 10 33
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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