using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb; //Database connectivity
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace StudentDatabase
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button ButtonAddStudent;
protected System.Web.UI.WebControls.Button ShowAll;
protected System.Web.UI.WebControls.ListBox ListBoxStudents;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBoxName;
protected System.Web.UI.WebControls.Button ButtonLoad;
protected System.Web.UI.WebControls.Button ButtonDeleteStudent;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//Create the connection while loading the form
OleDbConnection StudentConnection = new OleDbConnection();
StudentConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=";
StudentConnection.ConnectionString+=Server.MapPath("StudentDatabase.mdb");
//Add it to the application Object for future use
Application.Add("Connection",StudentConnection);
}
#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);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ButtonAddStudent.Click += new System.EventHandler(this.ButtonAddStudent_Click);
this.ButtonDeleteStudent.Click += new System.EventHandler(this.ButtonDeleteStudent_Click);
this.ShowAll.Click += new System.EventHandler(this.ShowAll_Click);
this.ButtonLoad.Click += new System.EventHandler(this.ButtonLoad_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ButtonAddStudent_Click(object sender, System.EventArgs e)
{
//Add the student directly to the list in the main page
//Insted of redirecting to another page
//Response.Redirect("FormAddStudent.aspx");
ListBoxStudents.Items.Add(TextBoxName.Text);
}
private void ButtonDeleteStudent_Click(object sender, System.EventArgs e)
{
ListBoxStudents.Items.Remove(ListBoxStudents.SelectedItem);
//Response.Redirect("FormDeleteStudent.aspx");
}
private void ShowAll_Click(object sender, System.EventArgs e)
{
//Saving all students to the database
AddStudentToTheDatabase();
}
private bool AddStudentToTheDatabase()
{
try
{
//Get the connection alreading stored..
OleDbConnection StudentConnection=(OleDbConnection)Application["Connection"];
//Open the connection now
StudentConnection.Open();
//Iterate the list and insert the names
foreach (ListItem StudentItem in ListBoxStudents.Items)
{
string myAddQuery = "INSERT INTO STUDENT_INFO (StudentName) VALUES('"+StudentItem.Text+"')";
OleDbCommand StudentCommand = new OleDbCommand(myAddQuery,StudentConnection);
//Ececute the Query
StudentCommand.ExecuteNonQuery();
}
StudentConnection.Close();
return true;
}
catch(Exception Ex)
{
Response.Write(Ex.Message);
return false;
}
}
private void ButtonLoad_Click(object sender, System.EventArgs e)
{
//Remove all items from the List
ListBoxStudents.Items.Clear();
//Get the connection
OleDbConnection StudentConnection=(OleDbConnection)Application["Connection"];
try
{
//Open the connection
StudentConnection.Open();
//Run the selecet SQL Query
string mySelectQuery = "SELECT * FROM STUDENT_INFO";
OleDbCommand StudentCommand = new OleDbCommand(mySelectQuery,StudentConnection);
//Execute the Query and get all datas in the data reader
OleDbDataReader StudentDescReader = StudentCommand.ExecuteReader();
//Clear the array first
//Iterate it and fill it to the list
while (StudentDescReader.Read())
{
ListBoxStudents.Items.Add(StudentDescReader.GetValue(StudentDescReader.GetOrdinal("StudentName")).ToString());
}
}
catch(Exception Ex)
{
Response.Write(Ex.Message);
return ;
}
}
}
}
FormDeleteStudent
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;
namespace StudentDatabase
{
/// <summary>
/// Summary description for FormDeleteStudent.
/// </summary>
public class FormDeleteStudent : System.Web.UI.Page
{
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);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}