using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; // This is the library with the SQL types we're using below namespace cse444_ta { /// /// Summary description for results. /// public class results : System.Web.UI.Page { // To connect to the ISQL01 server, we need to specify the server name, // the userid, the password, and the name of the database public static string strSqlString = "server=IISQLSRV; uid=joedoe; pwd=joedoejoedoe; database=Test"; public SqlConnection sqlConn; // Declare a connection object public SqlCommand sqlCom; // Declare a command object public SqlDataReader sqlRdr; // Declare a reader to read the query result // This was auto-created when I added a datagrid in the design view protected System.Web.UI.WebControls.DataGrid DataGrid1; // The following is to display error messages, if any protected System.Web.UI.HtmlControls.HtmlGenericControl spanErrMsg; // This routine runs when the page loads private void Page_Load(object sender, System.EventArgs e) { // When the page loads, this function will run showStudentTable(); } private void showStudentTable() { try { // Start a new connection to the database specified in strSqlString sqlConn = new SqlConnection( strSqlString ); sqlConn.Open(); // open the connection // execute query string strSql = "select * from districts"; // Here you can do any query you want sqlCom = new SqlCommand(strSql, sqlConn); // The command needs to know the query and the connection sqlRdr = sqlCom.ExecuteReader(); // Execute the command // Fill query result into html table // DataGrid is a type to create a table so you don't have to do it in html // dgStudentTable needs to know the source of the content and then binds it to the table DataGrid1.DataSource = sqlRdr; DataGrid1.DataBind(); // Close the sql connection at the end sqlConn.Close(); } catch (Exception e1) { spanErrMsg.InnerHtml = "" + e1.Message.ToString() + ""; } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }