This blog is intended for those who want to learn how to create a database driven windows application in .Net platform. Though I am not that expert but I just want to share what I had learned especially for those who wants to pursue a career in Software development. I require you to have Microsoft Visual Studio 2010 and MS Access installed in your PC.
Vitamin C#?
I know most of us are aware that Vitamin C is good for our immune system. But we're not talkin' about vitamins here. What I am trying to say is that let's take a look at the C# not as a guitar chord but as a coding standard. I am referring to Microsoft's C# programming language. Check wiki for the history and background of this language.
A very good to start is, to start from the very beginning. Yeah you're right I got it from a song but we'll apply it in software development. Let's say we want to have a Customer list data application. We need to retrieve the data from the database and display it in the datagrid. For this example, we will use MS Access database.
The Database
Open MS Access and create new database and call it Database1. Be sure to know the exact path of this database. In your Access DB, create new table and call it tblCustomer. Add ID, LastName, FirstName, Birthdate, Address and ContactNo datafields as shown below;
Now, let's add sample data on the database then close Access application.
The Project
Open Visual Studio and create new project. Select Visual C# Windows in the project template. Name the project as CustomerApp. Click browse to select the project path and click OK when done.
By default, the template will create a windows form and the Solution folder for the CustomerApp. Right click on the CustomerApp project and select add new folder then name it appdata. Again, right click on the CustomerApp project and select add Existing Item then browse to the access database that we just created.
Just cancel the Data Source configuration wizard. We will create our own data source configuration. Go to the form and add a datagrid then name it dgvCustomer;
The Codes
Double click on the form to view the code behind. By default, the following code should be seen;
Since we are using the Access Database, we need to declare OleDB by adding the following line;
using System.Data.OleDb;
private void LoadCustomerData()
{
DataSet DtSet = new DataSet();
//set the datasource
string conn= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=appdata/Database1.mdb;Persist Security Info=False;";
OleDbConnection mycon = new OleDbConnection(conn);
mycon.Open();
string strSql;
strSql = "SELECT * FROM tblCustomer ";
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(strSql, mycon);
myDataAdapter.Fill(DtSet);
dgvCustomer.DataSource = DtSet.Tables[0];
mycon.Close();
}
Then call LoadCustomerData in the form load event such as;
private void Form1_Load(object sender, EventArgs e)
{
LoadCustomerData();
}
Now, try running the project by pressing F5. You should see the following output;







No comments:
Post a Comment