I would like to show data in a table in SQL server database to Gridview in ASP.NET project, but the gridview shows nothing. Could you please help me to find where is the problem, and how to fix it.
Thanks
Here is my code:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                string cs = ConfigurationManager.ConnectionStrings["SampleDBCS"].ConnectionString;
                SqlConnection con = new SqlConnection(cs);
                SqlCommand cmd = new SqlCommand("select * from newexample", con);

                DataTable dt = new DataTable();
                dt.Columns.Add("col0");
                dt.Columns.Add("col1");
                dt.Columns.Add("col2");

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    DataRow dr = dt.NewRow();
                    dr["col0"] = rdr["col0"];
                    dr["col1"] = rdr["col1"];
                    dr["col2"] = rdr["col2"];

                    dt.Rows.Add(dr);

                }
                con.Close();

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }

    }
}

Dani AI

Generated

Short diagnosis and what to check first: the GridView itself is fine — the usual causes for “shows nothing” are (a) the DataTable you bind is empty, (b) the GridView has no matching columns (AutoGenerateColumns is false and BoundField/DataField names don’t match), or (c) the query/connection returned no rows or failed silently. In the code posted by you build a DataTable manually and copy values from a reader; that works but is fragile if the column names from the SQL query don’t match the names you use in the DataTable, or if the query/connection is wrong. ’s INotifyPropertyChanged suggestion applies to desktop/WPF-style property notifications, not to server-side GridView binding — it won’t fix an empty DataTable in ASP.NET.

Practical quick checks (do these first)

  • Run the same SELECT in SQL Server Management Studio to confirm rows exist.
  • After filling the DataTable, inspect dt.Rows.Count (or log it) before calling DataBind.
  • Ensure the GridView either has AutoGenerateColumns="true" or that every BoundField/DataField uses the exact column names present in the DataTable.
  • Turn on debugging or catch/log exceptions so any IndexOutOfRange or connection errors aren’t suppressed.

A simpler, more reliable binding pattern (avoids manual NewRow loops)

using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleDBCS"].ConnectionString))
using (var da = new SqlDataAdapter("SELECT * FROM newexample", conn))
{
    var dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

Extra notes and cautions: use using to dispose connections/readers, prefer parameterized queries for user input, and if you need paging/sorting rebind on the appropriate events (or persist the DataTable between postbacks). If after these checks the table is still empty, post the exact SQL column names and the GridView markup (BoundFields or AutoGenerateColumns setting) so the mapping can be reviewed.

You probably need to implement the INotification interface in some way.
Google is your friend. Example

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.