locked
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition. RRS feed

  • Question

  • User1395831461 posted

    I have a gridview defined in my aspx page having a DataSourceID="myDataSourceID".  I have a <asp:SqlDataSource ID="myDataSourceID ...</asp:SqlDataSource> on the same page.  The first time a user comes onto the page that is the datasource I want to use to load the gridview...I works no problems.

    I also have some check boxes on the page in a CheckBoxList with OnSelectedIndexChanged="myCodeBehind".  In myCodeBehind I build a query using the values of the checkboxes (thanks akhhttar) and try to use the query in to bind data to the gridview...

            Dim SqlDataSource1 As New SqlDataSource
            SqlDataSource1.ConnectionString = "$ ConnectionStrings:startpagegridview1ConnectionString"
            SqlDataSource1.ProviderName = "$ ConnectionStrings:startpagegridview1ConnectionString.ProviderName"
            SqlDataSource1.SelectCommand = Query
            GridView1.DataSource = SqlDataSource1
            GridView1.DataBind()

    Site builds and publishes ok, but at runtime...

     

    Server Error in '/' Application.

    Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace:

    [InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'.  Remove one definition.]
       System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +1779422
       System.Web.UI.WebControls.DataBoundControl.GetData() +4
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +59
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
       Default2.selecttowns(Object sender, EventArgs e) +887
       System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +118
       System.Web.UI.WebControls.CheckBoxList.RaisePostDataChangedEvent() +143
       System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +12
       System.Web.UI.Page.RaiseChangedEvents() +134
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5201
    

     


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

    I'm a newbie and obviously I'm missing a concept or several thousand...what should I be doing to bind a new dataset to the gridview?  Your time is greatly appreciated,

    Cheers, Roscoe

    <!-- [InvalidOperationException]: Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition. at System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() at System.Web.UI.WebControls.DataBoundControl.GetData() at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at Default2.selecttowns(Object sender, EventArgs e) at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) at System.Web.UI.WebControls.CheckBoxList.RaisePostDataChangedEvent() at System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() at System.Web.UI.Page.RaiseChangedEvents() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) [HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown. at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.startslf_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) -->

     

    Friday, May 13, 2011 9:52 AM

Answers

  • User-1360095595 posted

    instead of setting DataSource to SqlDataSource1, try setting DataSourceID instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 13, 2011 10:09 AM
  • User3866881 posted

    Hi:)

    Please change to this:

    GridView1.DataSourceId="startSLFSqlDataSource0";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2011 9:26 PM

All replies

  • User-1360095595 posted

    instead of setting DataSource to SqlDataSource1, try setting DataSourceID instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 13, 2011 10:09 AM
  • User3866881 posted

    Hi:)

    Try something like:

    GridView1.DataSourceId = "SqlDataSource1";

    Sunday, May 15, 2011 10:22 PM
  • User1395831461 posted

    Hi,

    Thanks for the quick reply...I tried setting the DataSourceID instead, got by the original exception but on to another one (my code is below the error paste)

    The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'SqlDataSource1' could not be found.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'SqlDataSource1' could not be found.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace:

    [HttpException (0x80004005): The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.  A control with ID 'SqlDataSource1' could not be found.]
       System.Web.UI.WebControls.DataBoundControl.GetDataSource() +1779500
       System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +159
       System.Web.UI.WebControls.DataBoundControl.GetData() +4
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +59
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
       Default2.selecttowns(Object sender, EventArgs e) +1326
       System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +118
       System.Web.UI.WebControls.CheckBoxList.RaisePostDataChangedEvent() +143
       System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +12
       System.Web.UI.Page.RaiseChangedEvents() +134
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5201
    

     


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

    My GridView1 in aspx...

     

     

     

            <asp:GridView ID="GridView1" runat="server" 
                AutoGenerateColumns="False"
                caption='<table  class="Freezing" ><tr><td>All Service Layout Forms</td></tr></table>'
                ToolTip="All Service Layout Forms"
                GridLines="None" AllowSorting="True" CaptionAlign="Left"
               DataKeyNames="SLFNO" OnSelectedIndexChanged="detail_Show"
               OnSelectedIndexChanging="detail_Show" SelectedRowStyle-BackColor="#CCFFCC" DataSourceID="startSLFSqlDataSource0">
                <AlternatingRowStyle BackColor="#CCCCFF" Wrap="False" />
                <Columns>
                    <asp:CommandField ShowSelectButton="True" ButtonType="Image" SelectImageUrl="linkimage.gif" />
                    <asp:BoundField DataField="SLFNO" HeaderText="SLF No."
                        SortExpression="SLFNO" />
                    <asp:BoundField DataField="SLFSTS" HeaderText="SLF Status"
                        SortExpression="SLFSTS" />
                    <asp:BoundField DataField="SLFSTSDSC" HeaderText="Status Desc."
                        SortExpression="SLFSTSDSC" />
                    <asp:BoundField DataField="SLFLOCNO" HeaderText="Location Code"
                        SortExpression="SLFLOCNO" />
                    <asp:BoundField DataField="SLFIFSWO#" HeaderText="IFS WO#"
                        SortExpression="SLFIFSWO#" />
                    <asp:BoundField DataField="SLFREQSTR" HeaderText="Requester"
                        SortExpression="SLFREQSTR" />
                    <asp:BoundField DataField="SLFREQNAM" HeaderText="Requester Name"
                        SortExpression="SLFREQNAM" />
                    <asp:BoundField DataField="SLFCUS" HeaderText="Customer Name"
                        SortExpression="SLFCUS" />
                    <asp:BoundField DataField="SLFSADD" HeaderText="Service Address"
                        SortExpression="SLFSADD" />
                    <asp:BoundField DataField="SLFSCITY" HeaderText="City"
                        SortExpression="SLFSCITY" />
                    <asp:BoundField DataField="SLFSPC" HeaderText="Postal Code"
                        SortExpression="SLFSPC" />
                    <asp:BoundField DataField="SLFBNAME" HeaderText="Bill Name"
                        SortExpression="SLFBNAME" />
                    <asp:BoundField DataField="SLFBADD1" HeaderText="Address Line1"
                        SortExpression="SLFBADD1" />
                    <asp:BoundField DataField="SLFBADD2" HeaderText="Address Line2"
                        SortExpression="SLFBADD2" />
                    <asp:BoundField DataField="SLFBADD3" HeaderText="Address Line3"
                        SortExpression="SLFBADD3" />
                    <asp:BoundField DataField="SLFBPC" HeaderText="Postal Code"
                        SortExpression="SLFBPC" />
                </Columns>
                <HeaderStyle CssClass="Freezing" />
            </asp:GridView>
            <asp:SqlDataSource ID="startSLFSqlDataSource0" runat="server"
                ConnectionString="<%$ ConnectionStrings:startpagegridview1ConnectionString %>"
                ProviderName="<%$ ConnectionStrings:startpagegridview1ConnectionString.ProviderName %>"
               
                SelectCommand="select slfno, t1.slfsts, slfstsdsc,
    SLFLOCNO,
    SLFIFSWO#,
    SLFREQSTR,
    SLFREQNAM,
    SLFCUS,  
    SLFSADD, 
    SLFSCITY,
    SLFSPC,  
    SLFBNAME,
    SLFBADD1,
    SLFBADD2,
    SLFBADD3,
    SLFBPC 
    from lib.file1 t1 join lib.file2 t2 on t1.slfsts = t2.slfsts
    order by t1.slfno">
                <SelectParameters>
                    <asp:ControlParameter ControlID="CheckBoxList1"
                        DefaultValue="CheckBoxList1.Items(0).Value.ToString()" Name="?"
                        PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>

    ...and my code in code behind...

        Protected Sub selecttowns(ByVal sender As Object, ByVal e As System.EventArgs)
            If CheckBoxList1.Items(0).Selected Then
                CheckBoxList1.Items(1).Selected = True
                CheckBoxList1.Items(2).Selected = True
                CheckBoxList1.Items(3).Selected = True
                CheckBoxList1.Items(4).Selected = True
                CheckBoxList1.Items(5).Selected = True
            End If
            If CheckBoxList1.Items(0).Selected = False Then
                CheckBoxList1.Items(1).Selected = False
                CheckBoxList1.Items(2).Selected = False
                CheckBoxList1.Items(3).Selected = False
                CheckBoxList1.Items(4).Selected = False
                CheckBoxList1.Items(5).Selected = False
            End If

            DetailsView1.Visible = False

            Dim Query As String
            Query = "select slfno, t1.slfsts, slfstsdsc,SLFLOCNO,SLFIFSWO#,SLFREQSTR,SLFREQNAM,SLFCUS,SLFSADD,SLFSCITY,SLFSPC,SLFBNAME,SLFBADD1,SLFBADD2,SLFBADD3,SLFBPC from lib.file1 t1 join lib.file2 t2 on t1.slfsts = t2.slfsts "
            Dim isFirstItemProccessed As New Boolean
            isFirstItemProccessed = False
            Dim MyItem As ListItem

            For Each MyItem In CheckBoxList1.Items


                If (MyItem.Selected = True) Then


                    If (isFirstItemProccessed = False) Then

                        Query += String.Format("where slfscity ='{0}'", MyItem.Value)
                        isFirstItemProccessed = True

                    Else


                        Query += String.Format(" or slfscity ='{0}'", MyItem.Value)

                    End If
                End If
            Next

     

            Dim SqlDataSource1 As New SqlDataSource
            SqlDataSource1.ConnectionString = "$ ConnectionStrings:startpagegridview1ConnectionString"
            SqlDataSource1.ProviderName = "$ ConnectionStrings:startpagegridview1ConnectionString.ProviderName"
            SqlDataSource1.SelectCommand = Query
            GridView1.DataSourceID = "SqlDataSource1"
            GridView1.DataBind()

        End Sub


     ...I'm still missing something.

    Your time greatly appreciated,

    Cheers, Roscoe

    Monday, May 16, 2011 11:57 AM
  • User3866881 posted

    Hi:)

    Please change to this:

    GridView1.DataSourceId="startSLFSqlDataSource0";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2011 9:26 PM
  • User1395831461 posted

    Thanks Metalasp and Decker for all your help, greatly appreciated

    Got it working by following both your advice, In the end rather than creating a new data source I just used datasourceidname.SelectCommand = thenewquerystring to change the select on the datasource originally named in the gridview in the aspx and then doing the DataBind().  Original intent was to create a new datasource set the query, connectionstring and providername for the new datasource and then change the datasource ID used by the gridview from the one originally specified in the aspx to the new datasourceID, but always seem to run into runtime exceptions...should I be able to make it work that way?

     

    Tuesday, May 17, 2011 11:41 AM
  • User3866881 posted

    Hi:)

    You mean that you use both DataSource and DataSourceId together?

    No you cannot do that.

    If I misunderstand yours, please elebrate your issue step by step (about what you've done), and what mistakes you have.

    Thx 

    Tuesday, May 17, 2011 8:57 PM