Hi
This is a tutorial for coding a form example a desktop application to send data from a form to an access database.Lets begin with the codes.This example is part of my final software project for developing a reservation system.
Let us Begin..by typing the namespace that we will be using for this form..Note that codes in blue are the codes you need to insert the red ones is generated by visual studio..
As you can see most of the namespace will be provided by Visual Studio but when you are trying to send data to an access database you it is important to include this namespace below as i have shown above:
using System.Data.OleDb;
The Oledb shows that we will be inserting data to an access database .
Now let`s move on to the body of the program that we will be coding but before we begin .Let me inform you that this form will have three textbox ,three labels, a submit button and a cancel button..That being said now let us continue...
namespace Form1
{
public partial class Form1 : Form
{
private OleDbConnection myCon; // type this code to declare or initializes the name of your Olebd connection
public Form1()
{
InitializeComponent();
myCon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Navutustars.accdb");//this code shows where the path to your database which is a connection string you can find in your app.config file in your solutions explorer.this is connection string example (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Navutustars.accdb
}
Below is the code of the submit button before you insert this code note that you must double click the submit button in your form to generate the code then below that code you can insert the line of code which is blue..
private void button1_Click(object sender, EventArgs e) {
OleDbCommand cmd = new OleDbCommand(); //cmd is the name of our OleDb command cmd.CommandType = CommandType.Text;// command type will be using is Text command
cmd.CommandText = "insert into customer (CustomerFirstName,CustomerLastName,CustomerPhoneNo,) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
cmd.Connection = myCon;//assigning our cmd connection as myCon since it is the OleDb connection which we have declare above
myCon.Open(); //now we are opening the connection
cmd.ExecuteNonQuery(); //query being execute
System.Windows.Forms.MessageBox.Show("Data Succefully Send to Database", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//if suceccful then message box pop out informing you that data has been inserted myCon.Close(); //now we are closing the connection
}
Let me explain about this codes below..
cmd.CommandText = "insert into customer (CustomerFirstName,CustomerLastName,CustomerPhoneNo,) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
In our access database we have to create a table named customer and three fields in that tables namely
CustomerFirstName,CustomerLastName,CustomerPhoneNo now code above is trying to tell the the machine or computer that we are inserting data from textBox1,textBox2,textBox3 into three fields in a table named customer which has three fields namely CustomerFirstName,CustomerLastName,CustomerPhoneNo...
Ok let us move on to the Cancel Button..For the Cancel button it is simple all you need to do is double click on your cancel button and insert this codes this.Close().
This is a tutorial for coding a form example a desktop application to send data from a form to an access database.Lets begin with the codes.This example is part of my final software project for developing a reservation system.
Let us Begin..by typing the namespace that we will be using for this form..Note that codes in blue are the codes you need to insert the red ones is generated by visual studio..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
As you can see most of the namespace will be provided by Visual Studio but when you are trying to send data to an access database you it is important to include this namespace below as i have shown above:
using System.Data.OleDb;
The Oledb shows that we will be inserting data to an access database .
Now let`s move on to the body of the program that we will be coding but before we begin .Let me inform you that this form will have three textbox ,three labels, a submit button and a cancel button..That being said now let us continue...
namespace Form1
{
public partial class Form1 : Form
{
private OleDbConnection myCon; // type this code to declare or initializes the name of your Olebd connection
public Form1()
{
InitializeComponent();
myCon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Navutustars.accdb");//this code shows where the path to your database which is a connection string you can find in your app.config file in your solutions explorer.this is connection string example (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Navutustars.accdb
}
Below is the code of the submit button before you insert this code note that you must double click the submit button in your form to generate the code then below that code you can insert the line of code which is blue..
private void button1_Click(object sender, EventArgs e) {
OleDbCommand cmd = new OleDbCommand(); //cmd is the name of our OleDb command cmd.CommandType = CommandType.Text;// command type will be using is Text command
cmd.CommandText = "insert into customer (CustomerFirstName,CustomerLastName,CustomerPhoneNo,) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
cmd.Connection = myCon;//assigning our cmd connection as myCon since it is the OleDb connection which we have declare above
myCon.Open(); //now we are opening the connection
cmd.ExecuteNonQuery(); //query being execute
System.Windows.Forms.MessageBox.Show("Data Succefully Send to Database", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//if suceccful then message box pop out informing you that data has been inserted myCon.Close(); //now we are closing the connection
}
Let me explain about this codes below..
cmd.CommandText = "insert into customer (CustomerFirstName,CustomerLastName,CustomerPhoneNo,) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
In our access database we have to create a table named customer and three fields in that tables namely
CustomerFirstName,CustomerLastName,CustomerPhoneNo now code above is trying to tell the the machine or computer that we are inserting data from textBox1,textBox2,textBox3 into three fields in a table named customer which has three fields namely CustomerFirstName,CustomerLastName,CustomerPhoneNo...
Ok let us move on to the Cancel Button..For the Cancel button it is simple all you need to do is double click on your cancel button and insert this codes this.Close().
if you want to know the final codes you can become a member of my blog and if you have any difficulties you comment on this blog so that i can help you...
ReplyDelete