1
Reply

problem in adding record to a database...

Hipo Hale

Hipo Hale

Jan 17 2010 9:53 AM
2.2k
Hello everyone...can anyone modify my code below....i have a database named "Database1.accdb"
with a table "Applicants"...The columns are ID(auto incremented),Name,and Age....
on this code, i can display the data from my database...
My problem is that how can i add a new record to my database...or delete a record...
I have read tutorials and code snippets here but i want a specific code...please help me guys...
On my designer form, i have dragged the Applicants node so in my form there is now the IDtextbox,
NameTextbox, and AgeTextbox...i have also added AddButton, DeleteButton, and UpdateButton...
How to write a code in which when i click the AddButtton, the values in the textboxes will
be added to my database...and when i delete then a record will be deleted....Please include
the namespace...or anything that needs to be declared....
By d way, im using MS Access 2007...Tanx guys...Any help will be greatly appreciated...


using System.Data.OleDb;

        private void button1_Click(object sender, EventArgs e)
        {
        const string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Bords\MyDocuments\Database1.accdb";
            string query1 = "Select * From Applicants";

            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(query1, conn))
                {
                    using (OleDbDataReader dr = cmd.ExecuteReader())
                    {
                        if (dt != null)
                        dt.Dispose();
                        dt = new DataTable();
                        dt.Load(dr);
                        dataGridView1.DataSource = dt;
                    }
                }
                conn.Close();
            }
        }

Answers (1)