9

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

I just got this error today, the code has been working until this afternoon I published the latest version to our server and it broke with that error both locally and on the server. I don't use "DataSourceID", the application reads database queries into a datatable and sets the datatable as the DataSource on the GridViews. I did a search in Visual Studio, searching the entire solution and the string "DataSourceID" does not appear in even 1 line of code in the entire solution. This is the first thing that freaked me out.

I figure it had been working yesterday, so I reverted the code to yesterday's build. The error was still there. I kept going back a build, and still the issue is there. I went back a month, I am still getting the same error. This application was working fine this morning? There has really been no code changes, and no where in the application is the DataSourceID EVER set on any of the gridviews. Has anyone ever seen anything like this at all??

How can I get that error if DataSourceID is never set... and the word "DataSourceID" is not in my solution? I just did a wingrep on the entire tree doing a case insensitive search on datasourceid.... pulled up absolutely nothing. That word is absolutely no where in the entire application.

    <asp:GridView ID="grdCommunication" runat="server" 
    Height="130px" Width="100%"
     AllowPaging="true" >
    ... standard grid view column setup here... 
    </asp:GridView>

// Code behind.. to set the datasource
  DataSet dsActivity = objCompany.GetActivityDetails();

  grdCommunication.DataSource = dsActivity;
  grdCommunication.DataBind();

// Updated: removed some confusing notes.

1
  • i wanted to thank you all for brainstorming with me and working around the freakin' Microsoft mislead. and thank you tsilb... Commented Nov 6, 2008 at 18:35

14 Answers 14

9

Try this:

DataSet dsActivity = objCompany.GetActivityDetails();
grdCommunication.DataSource = dsActivity.Tables[0];
grdCommunication.DataBind();
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry grdCommunication.DataMember was also set, I left that out. But I did try that way too. It was no good. I'm removing that grid view from the code right now, just to get the application up and running.
Okay, so do you get the same result when you bind to the Table instead of to the DataSet?
that was the issue, I was using "Activities" as the datasource name, and they changed it to "ACTIVITIES" same name, only upper case... now.. that shows me it is definitely case sensitive.. but the error given was a complete red herring.
4

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

3 Comments

As demonstrated, sometimes a table name change is more likely than the index changing... which would likely only happen if your query changes... in which case you'd instinctively double-check the code that runs it, right? :)
yeah i guess.. I just don't like change.. :(
Nobody likes things outside their control changing. That's why we have terms like "I'll take an action item to push back on the external dependency". In my world, this actually makes sense to people. It makes me sick.
3

Replace this code before this grdCommunication.DataSource = dsActivity;

grdCommunication.DataBind();
grdCommunication.DataSourceID="";

Comments

1

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().

1 Comment

I only just did that cuz I was freaking out.
0

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

Comments

0

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.

Comments

0

always bind dataset with table index to gridview...

ex. gridgrdCommunication.Table[0]; as metioned above by Tsilb

second way you intentionally write..

gridgrdCommunication.DataSourceID = String.Empty; gridgrdCommunication.DataSource=ds; gridgrdCommunication.DataBind();

Comments

0

Check you database structure.... if you are acceding your data throw a dbml file, the table structure in your database it's different of the dbml file structure

Comments

0

If you are using the Object Data Source and want to conditionally reload the grid in code behind you can successfully do this:

Dim datatable As DataTable = dataset.Tables(0)
Dim dataSourceID As String = gvImageFiles.DataSourceID
gvImageFiles.DataSourceID = Nothing
gvImageFiles.DataSource = datatable.DefaultView
gvImageFiles.DataBind()
gvImageFiles.DataSource = Nothing
gvImageFiles.DataSourceID = dataSourceID

Comments

0

You need to chose one way to bind the grid if it is from code behind means using c# code then remove the datasourceid property from grid view from design view of grid like this

//you have to make it like this

Comments

0

Please try this:

gvCustomerInvoiceList.DataSourceID = ""; gvCustomerInvoiceList.DataSource = ci_data; gvCustomerInvoiceList.DataBind();

Comments

0

I got this error today. It turns out that my stored procedure did not return neither any record nor a structure. This was because I had an empty try catch without a raiserror.

1 Comment

While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please edit to provide example code to show what you mean. Alternatively, consider writing this as a comment instead.
0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Page.DataBind()
End Sub
Function GetData()
    Dim dt As New DataTable
    Try

        dt.Columns.Add("ROOM_ID", GetType(String))
        dt.Columns.Add("SCHED_ID", GetType(String))
        dt.Columns.Add("TIME_START", GetType(Date))
        dt.Columns.Add("TIME_END", GetType(Date))


        Dim dr As DataRow = dt.NewRow

        dr("ROOM_ID") = "Indocin"
        dr("SCHED_ID") = "David"
        dr("TIME_START") = "2018-01-03 09:00:00.000"
        dr("TIME_END") = "2018-01-03 12:00:00.000"
        dt.Rows.Add(dr)


    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    Return dt
End Function

and add this to your item DataSource="<%# GetData() %>"

Comments

0

In my case the connection string to the database was not working. Fixing the connection string got rid of this error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.