0
votes

What I am trying to accomplish is pretty simple. I have a drop down list on a web form. The list shows the available job numbers. Under that, I have a grid view which shows the results of a query based on the current drop down list value.

Here is the form aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SHCInvoiceEntry
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           MasterDataDataContext db = new  MasterDataDataContext();

            var query = from m in db.MasterData2s 
                        select m;

            GridView1.DataSource = query;

        }
    }

Here is the error message:

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() +8575525
   System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +19
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5466; ASP.NET Version:2.0.50727.5456 

I am not sure how to remove the datasource or datasourceid. I am using the same datasource for drop down list and the grid view.

1
The error message says it all. Can you try to remove Datasource or DataSourceId from Gridview - DevelopmentIsMyPassion
In your Gridview code you have DataSourceID = query, you only need one datasource just remove either of it and if you are looking for refreshing it after some event then you need to use Gridview1.Databind - scc
Try below code GridView1.DataSourceID = string.Empty; - DevelopmentIsMyPassion

1 Answers

0
votes

Try below code

GridView1.DataSourceID = string.Empty;

Or else remove DatasourceId from property window.

Let me know if it helps you.