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 questionaire { /// /// Summary description for WebForm1. /// public class WebForm1 : 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=ISQL01;uid=likhan03; pwd=foo; database=likhan03"; 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 // Declare a DataGrid to show the results of the query protected System.Web.UI.WebControls.DataGrid dgStudentTable; // 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 Student"; // 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 dgStudentTable.DataSource = sqlRdr; dgStudentTable.DataBind(); // Close the sql connection at the end sqlConn.Close(); } catch (Exception e1) { spanErrMsg.InnerHtml = "" + e1.Message.ToString() + ""; } } }