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 demo { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { private SqlConnection sqlConn; // Declare a connection object private SqlCommand sqlCom; // Declare a command object private SqlDataReader sqlRdr; // Declare a reader to read the query result private string strSqlString = "server=IISQLSRV; uid=yourID; pwd=yourPassword; database=movies"; protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.TextBox yearTB; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.TextBox nameTB; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #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.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { // Start a new connection to the database specified in strSqlString sqlConn = new SqlConnection( strSqlString ); sqlConn.Open(); // open the connection // Execute query -- Here you could make 'strSql' any query you want string strSql = "SELECT * " + "FROM films " + "WHERE year > " + this.yearTB.Text; // The command needs to know the query and the connection sqlCom = new SqlCommand(strSql, sqlConn); // Execute the command sqlRdr = sqlCom.ExecuteReader(); // Get the answer from the reader and write it to the label. if (sqlRdr.HasRows) { DataGrid1.DataSource = sqlRdr; DataGrid1.DataBind(); DataGrid1.Visible = true; } // Close the sql connection at the end sqlConn.Close(); } private void Button2_Click(object sender, System.EventArgs e) { // Start a new connection to the database specified in strSqlString sqlConn = new SqlConnection( strSqlString ); sqlConn.Open(); // open the connection // Execute query -- Here you could make 'strSql' any query you want string strSql = "SELECT year " + "FROM films " + "WHERE filmname = '" + nameTB.Text + "'"; // The command needs to know the query and the connection sqlCom = new SqlCommand(strSql, sqlConn); // Execute the command sqlRdr = sqlCom.ExecuteReader(); // Get the answer from the reader and write it to the label. if (sqlRdr.HasRows) { sqlRdr.Read(); //advances to next row of results... Label3.Text = nameTB.Text + " was produced in " + sqlRdr.GetInt32(0); } else { Label3.Text = "requested film not found"; } Label3.Visible = true; // Close the sql connection at the end sqlConn.Close(); } } }