How To Develop A DataBase Application Using C#.Net And MS Access

Rate this article
0 out of 5
In this tutorial we will show you how to develop a data base application using C#.Net and ms access. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.




Source Code
HeaderControl.ascx.cs
namespace StudentDatabase
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for HeaderControl.
/// </summary>
public class HeaderControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Image Image1;

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
}
}

StudentDatabase

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
}
}




Source Code

Joined Date :14-Nov-2012
Articles
Posted
60
Messages
Posted
3

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.




   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud