using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; // Using Sql classes using System.Web.Configuration; // Using web.config file public partial class Lec7_Intro_ADONet_ReadingDatabase : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection connection; SqlCommand command; SqlDataReader reader; try { // Connect to the database using the connection string in the web.config file connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString); // Create an SELECT Sql statement command = new SqlCommand("SELECT * FROM Student", connection); using (connection) { // Open the connection connection.Open(); // Execute the command, the records are returned in an SqlDataReader object reader = command.ExecuteReader(); // Have an rows been returned? if (reader.HasRows) { DataTable table = new DataTable(); // Load the DataTable with the rows from // the SqlDataReader and put it into the // GridView to display it. table.Load(reader); gridView1.DataSource = table; gridView1.DataBind(); } else { statusLabel.Text = "No matching row found"; } // Close the connection connection.Close(); } } catch { statusLabel.Text = "An error occurred"; } } }