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

后端 未结 8 1776
死守一世寂寞
死守一世寂寞 2021-02-15 06:11

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

相关标签:
8条回答
  • 2021-02-15 07:05

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

    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)
提交回复
热议问题