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 jessica_phase1sample2 { /// /// Summary description for SpamForm. /// public class SpamForm : 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=coolStudent; pwd=imsocool; database=spam2"; protected System.Web.UI.WebControls.Label CountryLabel; protected System.Web.UI.WebControls.Label SpamQueryTitle; protected System.Web.UI.WebControls.Button GoButton; protected System.Web.UI.WebControls.Label SpamsSentLabel; protected System.Web.UI.WebControls.Label Query1Label; protected System.Web.UI.WebControls.TextBox CountryTB; protected System.Web.UI.WebControls.Label QueryLabel2; protected System.Web.UI.WebControls.Label LangLabel2; protected System.Web.UI.WebControls.Label LangLabel1; protected System.Web.UI.WebControls.TextBox Lang2TB; protected System.Web.UI.WebControls.TextBox Lang1TB; protected System.Web.UI.WebControls.Button LangButton; // The following is to display error messages, if any protected System.Web.UI.HtmlControls.HtmlGenericControl spanErrMsg; 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.GoButton.Click += new System.EventHandler(this.GoButton_Click); this.LangButton.Click += new System.EventHandler(this.LangButton_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void GoButton_Click(object sender, System.EventArgs e) { try { // 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 msgcount " + "FROM StatCountries " + "WHERE countryname = '" + this.CountryTB.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(); this.SpamsSentLabel.Text = "Number of Spams Sent: " + sqlRdr.GetInt32(0); } else { this.SpamsSentLabel.Text = "Invalid Country"; } // Close the sql connection at the end sqlConn.Close(); } catch (Exception e1) { spanErrMsg.InnerHtml = "" + e1.Message.ToString() + ""; } } private void LangButton_Click(object sender, System.EventArgs e) { // A Session is something that persists through HTTP requests to the server. // Since we want the next page to be able to retrieve what languages the // user requested, we will save the languages in the Session object. Session.Add("Lang1", Lang1TB.Text); Session.Add("Lang2", Lang2TB.Text); // Now we want to show the results in a new page so we transfer the control // to the next page we want to show. Server.Transfer("TwoLangSpammers.aspx"); } } }