I'm trying to use DataReader in GridView
rather than using DataAdapter.
But I have this error in aspx :
Non-invocable member 'IDataItemContainer.DataItem' cannot be used like a method
I have the error in this line :
<%# Container.DataItem("Data1")%>
I have this code :
SqlConnection baglan=new SqlConnection("connectionstring text");
if (connection.State== ConnectionState.Closed)
{
baglan.Open();
}
SqlCommand cmd = new SqlCommand("Select Data1 from Table1",baglan);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItem("Data1")%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
</asp:TemplateField>
</Columns>
</GridView>
How can I fix this ? Thanks
You should be using Eval
. But the error you posted has nothing to do with using DataReader
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Data1") %>
</ItemTemplate>
</asp:TemplateField>