1

I put this as a datasource in my gridView

var source = from p in allComments
         select new {p.Img, p.Name, p.Comment};
          GridView1.DataSource = source;
          GridView1.DataBind();

and i get this:

The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. 

A control with ID 'SqlDataSource1' could not be found.

My Gridview Markup:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        style="z-index: 1; left: 317px; top: 374px; position: absolute; height: 597px; width: 666px" 
       BackColor="#CCCCCC" BorderColor="#999999" 
        BorderWidth="0px" CellPadding="4" CellSpacing="2" 
        DataSourceID="SqlDataSource1" ForeColor="Black" AllowPaging="True" 
        onrowdatabound="GridView1_RowDataBound">

 <Columns>
      <asp:TemplateField HeaderText="#">
                        <HeaderStyle Width="500px" />
                         <ItemStyle Width="500px" />
                        <ItemTemplate>
                            <asp:Label ID="lblMessage" runat="server"  Text='<%# Bind("Comment") %>'></asp:Label>
                        </ItemTemplate>
       </asp:TemplateField>

           <asp:TemplateField HeaderText="#">
                        <HeaderStyle Width="100px" />
                          <ItemStyle Width="100px" />
                        <ItemTemplate>
                            <asp:Image ID="imgName" runat="server"  imageUrl='<%# Bind("Img") %>'></asp:Image><br />
                            <asp:Hyperlink ID="hyperLink" runat="server"  Text='<%# Bind("Name") %>' ></asp:Hyperlink>
                        </ItemTemplate>
       </asp:TemplateField>
   </Columns>

      </asp:GridView>
1
  • 1
    I guess you copy and paste code from other page and forget to remove DataSourceID="SqlDataSource1" :) Commented Jul 4, 2011 at 6:53

3 Answers 3

2

Remove DataSourceID="SqlDataSource1" from the Gridview, as you set the DataSource in Code behind..

var source = from p in allComments
select new {p.Img, p.Name, p.Comment};
GridView1.DataSource = source;
GridView1.DataBind();

You can either assign DataSourceID or DataSource, but can't do both.

Edit: Following your comments, you have a problem in Paging, to handle paging you have to to bind the data again.

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    var source = from p in allComments
                 select new { p.Img, p.Name, p.Comment };
    GridView1.DataSource = source;
    GridView1.DataBind();
}
Sign up to request clarification or add additional context in comments.

8 Comments

The data source does not support server-side data paging.---My var linq statement doesnt support paging :(
your problem is paging, I have update my answer. just try now.
Alright, it doesnt through an Exception.. But my datasource is not bound on the page load right no but on page index changing hmm... what if page index isnt changing?
You have to bind the data first time in page load, so the code goes in page load as well. Secondly when user change the page number, Page IndexChanging event fire, which assign new page index and you need to set the DataSource again here.
But if i put that code in the page load, it throws me the same exception saying that it doesnt support paging..May be i should use Group By statement to divide the whole collection into 10 groups
|
0

You've got the attribute DataSourceID="SqlDataSource1", the error message indicates that there are no SqlDataSource with the Id SqlDataSource1 in your code.

Therefore, you need to remove that attribute in order to bind it from code-behind.

Your Grid-View decleration should look like this instead:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    style="z-index: 1; left: 317px; top: 374px; position: absolute; height: 597px; width: 666px" 
    BackColor="#CCCCCC" BorderColor="#999999" 
    BorderWidth="0px" 
    CellPadding="4" CellSpacing="2" 
    ForeColor="Black" AllowPaging="True" 
    onrowdatabound="GridView1_RowDataBound">

You can now Bind the GridView without that issue, you can only have one of them set. Either you bind from code behind using the DataSource-attribute. Or you specify the Attribute DataSourceId.

Comments

0

Remove DataSourceID="SqlDataSource1"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.