C# INSERTING DATA FROM A WINDOWS FORM APPLICATION INTO MS ACCESS DATABASE USING VS2013 STEP BY STEP

in utopian-io •  7 years ago  (edited)

What Will I Learn?

Things we could learn from it

• Create a database using MS Access

• Create a Windows Form Project using Visual Studio 2013

• Connect the Windows Form Project to the database

• How to Insert data from Windows Form Project to database

Requirements

Simple materials to follow this tutorial

• Installed Visual Studio 2013 in to your desktop/laptop

• Installed Microsoft Office with MS Access in your desktop/laptop

Difficulty

  • Basic

Tutorial Contents

The contents of this tutorial are learning on how to create a database using MS Access, how to create a Windows Forms Application and connect to the database and inserting a data to the database.

Let's Start

CREATE A DATABASE USING MS ACCESS

Open Microsoft Office

step 1.JPG

  1. Click Blank Database to create a fresh database without any data
  2. Click Folder icon

step 2.jpg

3.Change file name into myDB and choose Microsoft Office Access Database (2002-2003 format) and click OK.

step 3.JPG

4.See file name if has .mdb as an extension of the file name you created and then click Create button.

step 4.jpg

5.Right click Table1 and choose Design View to add some entities to our table.

step 5.JPG

6.Change Table name Table1 into Person then click OK.

step 6.JPG

7.Add those Field Name with the corresponding Data Type and make sure to save it.
Ctrl + S shortcut for SAVE.

Field Name: ID

Data Type: Number

Field Name: Firstname

Data Type: Text

Field Name: Lastname

Data Type: Text

Field Name: Age

Data Type: Number

Field Name: Address

Data Type: Text

After saving the table you may now close the database you created for the mean time.

Next,

CREATE WINDOWS FORM PROJECT (VS2013)

Open Visual Studio 2013

step 7.JPG

1.Click New Project

step 8.JPG

2.Choose Visual C#, Windows and choose Windows Forms Application and click OK.

After this, we need to connect our Windows Form Application to our database we created earlier.
You need to locate your database you created earlier.
Basically it will be saved in My Documents in your Computer/Laptop.
After Locating it, COPY it and PASTE it in the Debug Folder of your Project.
To open the debug folder

Untitled.jpg

  • Right click Project File name and Click Open Folder in File Explorer.

go to bin\debug and Paste the database there. After this, let move to the next step.
we will add not the Connection string to our database and to do that we need to create
New Class.

step 9.jpg

3.Right click File name choose Add new New Item...

step 10.JPG

4.Change Class1.cs name into Connection.cs

5.after changing it click Add Button below.

step 15.JPG
See Code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Connection
    {
        public static string dbconnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\myDB.mdb";
        public static OleDbConnection con = new OleDbConnection(dbconnect);

        public static void DB()
        {
            try
            {
                con.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    
    }
}



After adding those code, lets go back to our main form to Add some items.

step 16.JPG

7.Add all these items to your Form also the labels.

to change the text of the labels Right Click the label and choose properties then look for the Text properties so that you can change the text of the label in your desired text. After that, we will now add the main code to save data to our database.

step 17.jpg

8.Double click SAVE button in your form.

step 18.JPG

9.Add this line of code above.

step 19.JPG

10.copy this line of codes here.

See the codes below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Connection.con.Open();
            string save = "Insert into Person(ID, Firstname, Lastname, Age, Address)VALUES("+textBox1.Text+", '"+textBox2.Text+"', '"+textBox3.Text+"', "+textBox4.Text+", '"+textBox5.Text+"')";
            OleDbCommand command = new OleDbCommand(save, Connection.con);
            command.ExecuteNonQuery();
            MessageBox.Show("Successfully saved");
            Connection.con.Close();

        }
    }
}

Now, we will run the codes.

step 20.JPG

11.Click start to run the codes.

finish.JPG

12.Add some data to your textbox after that click save button.

ID: 1

Firstname: roxy

Lastname: roxy

Age: 23

Address: Philippines

Hope you learn something with this turorial

Thank you!



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Related Rule:

  • Only contributions made to open source projects with a GitHub repository can be approved.

Clarifications and Suggestions:

  • There is no proper GitHub repository for C# or Visual Studio since they are not open source projects, so contributions on those projects cannot be approved. The repository you linked belongs to Visual Studio Code and it's not related with the tutorial or Visual Studio itself.

You can contact us on Discord.
[utopian-moderator]

Congratulations @roxy19! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Do not miss the last post from @steemitboard:

Carnival Challenge - Collect badge and win 5 STEEM
Vote for @Steemitboard as a witness and get one more award and increased upvotes!

Congratulations @roxy19! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Use your witness votes and get the Community Badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!