Reputation: 17
I have 2 error messages with my c# webform, that i can not handle by myself.
Add.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Add.aspx.cs" Inherits="keszlet_management.Add" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<p>
<br />
</p>
<p>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</p>
</asp:Content>
Add.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
public object GridView1 { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM items"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
}
I am new in C#, so I know i have several mistakes, please focus on error messages:
1.: Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'DataSource' and no extension method 'DataSource' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
2.: The same message with 'DataBind'
Upvotes: 1
Views: 6995
Reputation: 460138
The problem is that you have declared your GridView
as
public object GridView1 { get; private set; }
It is a GridView
so don't declare it as object, otherwise you can't use the properties or methods of a GridView
but only of System.Object
, or you have to cast it to GridView
.
Since this is a codebehind file of an aspx file you don't need to declare it at all. It will be auto generated in the partial class that has the same name and ends with .designer.cs
.
Upvotes: 2
Reputation: 358
On this line: public object GridView1 { get; private set; }
. Shouldn't it be declared as GridView instead of just object? Check out the example usage here: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.gridview(v=vs.110).aspx
Upvotes: 1