“Both DataSource and DataSourceID are defined” error using ASP.NET GridView

后端 未结 7 2341
面向向阳花
面向向阳花 2021-02-15 06:38

\"Both DataSource and DataSourceID are defined on \'grdCommunication\'. Remove one definition.\"

I just got this error today, the code has been working until this after

相关标签:
7条回答
  • 2021-02-15 06:45

    Holy smoke batman. The Table name was changed causing my Datasource to be no good. But that error message doesn't make any sense in this situation. So technically tsilb's solution will work if I call the table by index instead of by name, so I'll mark his solution as correct.

    After reading his post, I tried dsActivity.Tables["Activities"] instead of passing the dataset to the Datasource and the table name to the Datamember, and obviously that didn't work, but If I pass the actual index, which I don't like doing because that index might change, then it is now working. But the messed up part, was that error.. That error was completely off base as to what the problem was. saying that I defined both and to remove one, when in reality, that was not the case. and another really messed up thing, was the table name was only changed to be all upper case... But hey, "Activities" is a different key than "ACTIVITIES".

    0 讨论(0)
  • 2021-02-15 06:46

    Try this:

    DataSet dsActivity = objCompany.GetActivityDetails();
    grdCommunication.DataSource = dsActivity.Tables[0];
    grdCommunication.DataBind();
    
    0 讨论(0)
  • 2021-02-15 06:54

    I got this error today, turns out that it had nothing to do with DataSourceID, and had everything to do with the DatasSource itself.

    I had a problem in my DatasSource , and instead of getting a DatasSource related error, I got this meaningless error.

    Make sure you're DatasSource is good, and this error should go away.

    0 讨论(0)
  • 2021-02-15 07:02

    tslib is right, don't do: grdCommunication.DataSourceID = null; or the string.Empty version. You only use the DataSourceID if you're using a SqlDataSource or ObjectDataSource control for your binding.

    It's called "declarative" binding because you're using "declared" controls from on your page. Binding to controls does not require a call to the DataBind() method.

    Because you're DataBinding manually (calling grd.DataBind()) you only set the DataSourrce and then call DataBind().

    0 讨论(0)
  • 2021-02-15 07:02

    I ran into the same error, but a totally different problem and solution. In my case, I'm using LINQ to SQL to populate some dropdown lists, then caching the results for further page views. Everything would load fine with a clear cache, and then would error out on subsequent page views.

    if (Cache["countries"] != null)
    {
        lbCountries.Items.Clear();
        lbCountries.DataValueField = "Code";
        lbCountries.DataTextField = "Name";
        lbCountries.DataSource = (Cache["countries"]);
        lbCountries.DataBind();}
    else
    {
        var lstCountries = from Countries in db_read.Countries orderby Countries.Name select Countries;
        lbCountries.Items.Clear();
        lbCountries.DataValueField = "Code";
        lbCountries.DataTextField = "Name";
        lbCountries.DataSource = lstCountries.ToList();
        lbCountries.DataBind();
    
        Cache.Add("countries", lstCountries, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
    }
    

    The issue came from: Cache.Add("countries", lstCountries, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);

    When it should have been: Cache.Add("countries", lstCountries.ToList(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);

    0 讨论(0)
  • 2021-02-15 07:05

    Replace this code before this grdCommunication.DataSource = dsActivity;

    grdCommunication.DataBind();
    grdCommunication.DataSourceID="";
    
    0 讨论(0)
提交回复
热议问题