Monday, August 3, 2009

Simple Database Application using C#,VB.NET

This is my first article so if you are facing any problem or any other topic on which you want help then contact me my email address is
So here i made a simple database application for those who just started visual basic.net or C#.net
i provide the pic base tutorial as well as video based tutorial (soon inshallah)
SO first open your MS-Access and create new debase file its simple to create a database on MS-Access


click new button and chose the blank database from the left corner of your screen as shown below name the database and save it where you want so save it.



Now our database has been created so we have to put some data into so we can access it for database creation we must have know about the table rows entities or relationship so it will be better to do that you must have know about these terms however next step is to create a table in which we put some fields we suppose that our database is just a simple enough like student information :)


so,,


However, now double click on the create table in design view and create the table as shown below



here some thing that i would like to explain you first we have three fields

Roll_no Number

std_name Text

father_name Text

what is primary key its a unique value means the value which will never repeat it self for example if any student have a roll no of 13 then you can not assign any other student roll no 13 means duplication is not allowed in roll no since it is mention that roll no only posses a number.similarly std_name data type and father_name data type is also text.

Now your first table is created you have to save it and name it as you want here i just name the table std_info as shown below

Now we have to put some data in our table that we just created now double click on to the std_info table as show below



after double click on it you just have to put some data into it so you can access it in future with the help of vb or c#.net
here is the example data as show below


Now you just close the MS-Access now our real work has been started that how we can access this database file with the help of vb.net or c#.net

Remember that our MS-Access file extension is *.mbd.

Now open your visual studio click on file and then click on new project as shown below

and now select the language on which you would like to work here i am choosing the C#.net since the database application that i am gonna create here there is not so much different in code of vb.net or c# both having the similarities but i am also give you the code of vb.net i hope it will work out from your side

don't forget to select the path where you want to store it since we are working of GUI so we have to select the windows application it will be much easier for database and most uses

now we have to design the form on which we need three text boxes as we have three fields since we have three fields in our database and four button for

Add for insert new record

Search for search the existing record

Edit for update or edit the any stored record


Delete for delete any record


now our form design is almost complete jsut we are using next is label so it will be easy to understand information that will shown by our text boxes

so our final form looks like that

now the next step is more intresting we have to start to written the code here i am gonna uses the most easier code for creating the database appliaciton and it will be most poweful since mostly pepole done it by using wizard it will be easy but not as much poweful now

Double Click in your form main windiow and include the header file into it or go to the view and then click on to the code as show below






Now include the heder file onto the top of the your code.

C#.NET



using System.Data.OleDb;



VB.NET



Imports System.Data.OleDb
now go to view and select the designer from it and double click on to the form screen and the following code into it




C#.NET

public OleDbConnection conn;
public OleDbCommand comm;
private void Form1_Load(object sender, EventArgs e)
{
string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\db1.mdb";
conn = new OleDbConnection(connection);
}


For VB code will be as shown below



now go to the view then slect the designer again or double click on the search button and paste the following code into it..


C#.NET
//find out those record which havin the roll no written in to the textbox
string query = "select * from std_info where roll_no=" + textBox1.Text ;
//pass the query into the database and extract the data accordign to
comm = new OleDbCommand(query, conn);
//open connection
conn.Open();
//uses for data row
OleDbDataReader dr = comm.ExecuteReader();
//if record is found then
if (dr.Read() ==true )
{
//display it into the text boxes
textBox2.Text = dr["std_name"].ToString();
textBox3.Text = dr["father_name"].ToString();
}
else
{
MessageBox.Show("record is not found");
}

VB.NET



Public conn As OleDbConnection
Public comm As OleDbCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim query As String = "select * from std_info where roll_no=" & Val(TextBox1.Text) & ";"
comm = New OleDbCommand(query, conn)
conn.Open()
Dim dr As OleDbDataReader = comm.ExecuteReader()
If dr.Read() <> False Then
TextBox2.Text = dr("std_name")
TextBox3.Text = dr("father_name")
Else
MessageBox.Show("not found")
End If
conn.Close()

Now similarly we can update.delete or edit our record just we have to change the query in it so for your convenience i am written the code of it you just have to copy it and paste it :)

FOR DELETE

C#.NET

//delete record which havin the roll no written in to the textbox
string query = "Delete from std_info where roll_no=" + textBox1.Text ;
//pass the query into the database and extract the data accordign to
comm = new OleDbCommand(query, conn);
//open connection
conn.Open();
MessageBox.Show("record has been deleted");
conn.Close();

VB.NET

Dim query As String = "Delete from std_info where roll_no=" & Val(TextBox1.Text) & ";"
comm = New OleDbCommand(query, conn)
conn.Open()
MessageBox.Show("Record has been deleted ")
conn.Close()


FOR INSERT
C#.NET

//find out those record which havin the roll no written in to the textbox
string query = "INSERT INTO std_info (roll_no,std_name,father_name) values(" + textBox1.Text+",'"+textBox2.Text+"','"+textBox3.Text+"')";
//pass the query into the database and extract the data accordign to
comm = new OleDbCommand(query, conn);
//open connection
conn.Open();
int a = comm.ExecuteNonQuery();
if (a>0)
Messagebox.show("record has been insert");
VB.NET

Dim query As String = "INSERT INTO std_info (roll_no,std_name,father_name) values(" & TextBox1.Text & ",'" & TextBox2.Text + "','" & TextBox3.Text & "')"
comm = New OleDbCommand(query, conn)
conn.Open()
comm.ExecuteNonQuery()
MessageBox.Show("record has been entered")

FOR EDIT
C#.NET

string query = "UPDATE std_info set std_name='" + textBox2.Text + "',father_name='" + textBox3.Text + "'";
//pass the query into the database and extract the data accordign to
comm = new OleDbCommand(query, conn);
//open connection
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("record has been update");

VB.NET

Dim query As String = " UPDATE std_info set std_name='" & TextBox2.Text & "',father_name='" & TextBox3.Text & "'"
comm = New OleDbCommand(query, conn)
conn.Open()
comm.ExecuteNonQuery()
MessageBox.Show("record has been updated")














































































































1 comment:

  1. Well done osama ... iam proud to say tht ur r my one ov bestest frd :) ... hope to see more things from you ... ! :)

    Casper & hassaan...!

    ReplyDelete