In .aspx I have
<body>
<form id="form2" runat="server">
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="True" Width="100%" ViewStateMode="Enabled">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="id" HeaderText="iD" />
<asp:BoundField ItemStyle-Width="150px" DataField="nme" HeaderText="Name" />
</Columns>
</asp:GridView>
</form>
</body>
In .cs I have
private void Grid()
{
string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT id, nme FROM mytable"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (dt = new DataTable())
{
sda.Fill(dt);
GridView.DataSource = dt; //error occurs here
GridView.DataBind();
}
}
}
}
}
I receive the error CS0120 An object reference is required for the non-static field, method, or property 'BaseDataBoundControl.DataSource'
The .cs does not seem to recognize the GridView from the .aspx page. using .net 4.6.1