Hey everyone can I get some help please. I read through a few threads with the same problem as me but i can't seem to get the answer i'm looking for. I'm doing a school project and I need to save a Access DataBase that I have created in C#! All the other threats are saving a Database that have already been created earlier. But I'm creating a DataBase in C#. Here is my code if anyone can help please...


[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;

namespace WindowsFormsApplication1
    public partial class New_Client : Form
        public New_Client()
            //skep veranderlike wat telling hou
            int count = 0;

            //skep datatabel veranderlike nl kliente
            DataTable kliente;

        private DataTable MaakKlientTabel()
            // Maak 'n nuwe tabel genaamd 'Kliente';
            DataTable kliente = new DataTable("Kliente");

            // Add column objekte in die tabel.
            DataColumn Naam = new DataColumn();
            Naam.DataType = System.Type.GetType("System.String");
            Naam.ColumnName = "Name";
            Naam.DefaultValue = "";

            DataColumn Van = new DataColumn();
            Van.DataType = System.Type.GetType("System.String");
            Van.ColumnName = "Surname";
            Van.DefaultValue = "";

            DataColumn ID = new DataColumn();
            ID.DataType = System.Type.GetType("System.String");
            ID.ColumnName = "ID";

            DataColumn Adres = new DataColumn();
            Adres.DataType = System.Type.GetType("System.String");
            Adres.ColumnName = "Adress";

            DataColumn Kontak = new DataColumn();
            Kontak.DataType = System.Type.GetType("System.String");
            Kontak.ColumnName = "Contact";

            DataColumn Provinsie = new DataColumn();
            Provinsie.DataType = System.Type.GetType("System.String");
            Provinsie.ColumnName = "Province";

            DataColumn CD = new DataColumn();
            CD.DataType = System.Type.GetType("System.Int32");
            CD.ColumnName = "CardNumber";

            DataColumn MOC = new DataColumn();
            MOC.DataType = System.Type.GetType("System.Decimal");
            MOC.ColumnName = "MoneyOnCard";

            // Maak 'n array van DataColumn objekte.
            DataColumn[] PK = new DataColumn[1];
            PK[0] = ID;
            kliente.PrimaryKey = PK;

            // Return nuwe tabel.
            return kliente;

        private void MaakNuweDataRy()


            // Sodra 'n tabel geskep is maak gebruik van NewRow om 'n nuwe ry te skep 
            DataRow ry;
            ry = kliente.NewRow();

            // Add die nuwe ry by die kolleksie
            ry["Name"] = textBox1.Text;
            ry["Surname"] = textBox2.Text;
            ry["ID"] = textBox3.Text;
            ry["Adress"] = textBox4.Text;
            ry["Contact"] = textBox8.Text;
            ry["Province"] = comboBox2.SelectedItem;
            ry["CardNumber"] = textBox5.Text;
            ry["MoneyOnCard"] = textBox6.Text;

            foreach (DataColumn kolom in kliente.Columns)
            dataGridView1.DataSource = kliente;


        private void button1_Click(object sender, EventArgs e)

                //Skep veranderlikes
            /*string IDSub = textBox3.Text.Substring(0, 6);

            string DOBDay = textBox7.Text.Substring(0, 2);
            string DOBMonth = textBox7.Text.Substring(3, 2);
            string DOBYear = textBox7.Text.Substring(8, 2);

            string DOBSub = (DOBYear + DOBMonth + DOBDay);

            int gesl = Convert.ToInt32(textBox3.Text.Substring(6, 1));

            bool IDtoets = false;*/
            bool NaamToets = false;
            bool VanToets = false;
            bool AdresToets = false;
            bool KontakToets = false;
            bool RasToets = false;
            bool ProvinsieToets = false;
            bool KaartNomToets = false;
            bool KaartGeldToets = false;

                //Toets die invoer
            /* if (IDSub == DOBSub && textBox3.TextLength == 13 && ((gesl < 5 && comboBox3.SelectedIndex == 1) || (gesl >= 5 && comboBox3.SelectedIndex == 0)))
                IDtoets = true;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with DOB, ID and Sex combinatition." + "\n" + "Make sure everything is typed in correctly");
                comboBox3.SelectedIndex = -1;
                IDtoets = false;
                } */
                if (textBox1.Text == "")

                NaamToets = false;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Name." + "\n" + "Make sure a name is typed in.");
                NaamToets = true;

                if (textBox2.Text != "")

                VanToets = true;
                VanToets = false;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Surname." + "\n" + "Make sure a Surname is typed in.");

                if (textBox4.Text != "")

                AdresToets = true;
                AdresToets = false;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Adress." + "\n" + "Make sure a Adress is typed in.");

                if (textBox8.Text != "")
                KontakToets = true;
                KontakToets = false;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Contact." + "\n" + "Make sure a Contact is typed in.");

                if (comboBox1.SelectedIndex != -1)

                RasToets = true;
                RasToets = false;
                comboBox1.SelectedIndex = -1;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Race." + "\n" + "Make sure a Race is selected.");

                if (comboBox2.SelectedIndex != -1)
                ProvinsieToets = true;
                ProvinsieToets = false;
                comboBox2.SelectedIndex = -1;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Province." + "\n" + "Make sure a Province is selected.");

                if (textBox6.Text != "")
                KaartGeldToets = true;
                KaartGeldToets = false;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Money on Card" + "\n" + "Make sure Money on Card is typed in.");

                if (textBox5.Text != "")

                KaartNomToets = true;

                KaartNomToets = false;
                MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Card Number" + "\n" + "Make sure a Card Number is typed in.");

                    //Toets of al die toetse korrek was
                if (/*(IDtoets == true) && */(NaamToets == true) && (VanToets == true) && (AdresToets == true) && (KontakToets == true) && (RasToets == true) && (ProvinsieToets == true) && (KaartGeldToets == true) && (KaartNomToets == true))
                    //Roep metode MaakNuweDataRy om 'n nuwe ry by te sit

                    //Update tabel

                    //Vertoon boodskap dat nuwe klient by geplaas is
                 MessageBox.Show("Client Added");
                    //Maak huidige form toe en vorige form weer oop
                 // Form2 kind = new Form2();
                 // kind.Show();

        private void New_Client_Load(object sender, EventArgs e)

            // Maak gebruik van die MaakKlientTabel funksie om 'n nuwe tabel te skep
            kliente = MaakKlientTabel();

            // TODO: This line of code loads data into the 'klientDataSet.Klient' table. You can move, or remove it, as needed.


        private void button2_Click(object sender, EventArgs e)
                //Maak huidige form toe en maak vorige form oop
            Form2 kind = new Form2();


        private void klientBindingNavigatorSaveItem_Click(object sender, EventArgs e)



        private void button3_Click(object sender, EventArgs e)

This code doesn't show how your created database fits in, can I assume you have selected the tableadapter and everything on the Windows Form design, if so we need to see these settings as well?
Which of these button clicks is used to instantiate filling the datagrid as its not done on loading the form?

button1_Click. After the testing is done it calls the method MaakNuweDataRy. (Sorry its in afrikaans, in english it means make new data row) See where I created the method

// As soon as a table is created in form load use NewRow to make new data rows             DataRow ry;            ry = kliente.NewRow();             // Add new row to collection            ry["Name"] = textBox1.Text;            ry["Surname"] = textBox2.Text;            ry["ID"] = textBox3.Text;            ry["Adress"] = textBox4.Text;            ry["Contact"] = textBox8.Text;            ry["Province"] = comboBox2.SelectedItem;            ry["CardNumber"] = textBox5.Text;            ry["MoneyOnCard"] = textBox6.Text;            kliente.Rows.Add(ry);             foreach (DataColumn kolom in kliente.Columns)            Console.WriteLine(kolom.ColumnName);            dataGridView1.DataSource = kliente;

This adds a new row to a local datatable but nothing is referencing an access database you created in code. If you want to save the rows added into a database then I would loop through the rows in the datatable and insert them manually.

can you send me the code for that please. thanks.

if you google for adoconnection and access you will get good code samples for saving information to the database.

and if you google for looping through items in a datagrid c# you will get this as well

I'm afraid I don't currently have the time to write all the code myself as I'm at work, if I get more time later I will do.

SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "pathof your database";
            SqlCommand cmd = new SqlCommand("create databse"+textBox1.Text);

this code is for create database at run time and u can spacify the database name in textbox.

I think u need this code.


The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference

this is the error i get with the code above and another thing is where "partof your database" stand what must come in there?

Dont you guys think it would be easier if i just create empty database and every time i click or do something it just fills up the existing one rather than creating a brand new one in c#. The only thing that bothers me of that idea is. I would only be able to have that one database. I want to have a Database for every day. Thats why i want to create a database everytime the page initializes because it initializes only once a day. Can yo guys please help.

I would appreciate it dearly when you have time to send me that code because its a school project and its almost time to give it in. thanks again

Before I put some code together can you give me an overview of what the project is? The code above is for an sql database not an access database so this won't help if you are using access databases.

Does it need to be an access database or any kind of database? If you are creating a new one every day you may be better for something more lightweight like SQLite.

The point of having databases is to persist data. Why do you want a DB if you don't want to persist data?

Have you thought about just creating an XML file?

When you are creating a DB, before you enter data, you have to create the schema. Before you create the schema, you have to create the DB. In order to do that, you have to have the JET engine create the access file. (I have no idea how to do that. As an exercise, I tried to do that. Never could get it to work.) There isn't anything in your code even attempting to create the connection to the JET engine, so you can work with Access.

Random comments about code, unrelated to your question:
DataTable kliente; // It is a good practice to declare "private", basically because it visually separates the object as a persisting instance of the class. (You aren't looking for the method wrapping the declaration.)
private DataTable MaakKlientTabel() // either declare the return type void or never reference kliente in the code. The point of this code should be either to exclusively create the kliente object (return void) or to create a Datatable image that matches kliente that could either be used to create the kliente object or a copy of the DataTable elsewhere. The way it is now, if you happened to ask for a copy of the image, you would wipe out the memory of kliente, and your "copy" IS kliente.

If you make it void, in New_Client_Load just execute MaakKlientTabel()

int gesl = Convert.ToInt32(textBox3.Text.Substring(6, 1));
//This would blow up if you put in a non-numeric character in the text box. Use:
int gesl;
if (!int.TryParse(textBox3.Text, out gesl))
{//Put whatever logic needed if the user didn't enter a number into textBox3}

Putting in the code you are attempting to execute is always a good idea, but not if it isn't germain to your main question.

Creating a DB structure in code is extremely difficult in any DB system. You haven't demonstrated that you understand there IS a DB structure in any DB. (Creating a memory copy of the DB structure is done by creating data tables and data sets. These are great tools to communicate between the DB structure and your code. They do nothing to create the DB stucture.)

You would be better off, using access to create an empty DB structure with the table(s) (and relationships) In code, COPY the mdb file to a new location, then connect to the new file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.