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...
Thanks
[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()
{
InitializeComponent();
}
//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 = "";
kliente.Columns.Add(Naam);
DataColumn Van = new DataColumn();
Van.DataType = System.Type.GetType("System.String");
Van.ColumnName = "Surname";
Van.DefaultValue = "";
kliente.Columns.Add(Van);
DataColumn ID = new DataColumn();
ID.DataType = System.Type.GetType("System.String");
ID.ColumnName = "ID";
kliente.Columns.Add(ID);
DataColumn Adres = new DataColumn();
Adres.DataType = System.Type.GetType("System.String");
Adres.ColumnName = "Adress";
kliente.Columns.Add(Adres);
DataColumn Kontak = new DataColumn();
Kontak.DataType = System.Type.GetType("System.String");
Kontak.ColumnName = "Contact";
kliente.Columns.Add(Kontak);
DataColumn Provinsie = new DataColumn();
Provinsie.DataType = System.Type.GetType("System.String");
Provinsie.ColumnName = "Province";
kliente.Columns.Add(Provinsie);
DataColumn CD = new DataColumn();
CD.DataType = System.Type.GetType("System.Int32");
CD.ColumnName = "CardNumber";
kliente.Columns.Add(CD);
DataColumn MOC = new DataColumn();
MOC.DataType = System.Type.GetType("System.Decimal");
MOC.ColumnName = "MoneyOnCard";
kliente.Columns.Add(MOC);
// 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;
kliente.Rows.Add(ry);
foreach (DataColumn kolom in kliente.Columns)
Console.WriteLine(kolom.ColumnName);
dataGridView1.DataSource = kliente;
}
private void button1_Click(object sender, EventArgs e)
{
this.klientTableAdapter.Fill(this.klientDataSet.Klient);
//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;
}
else
{
MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with DOB, ID and Sex combinatition." + "\n" + "Make sure everything is typed in correctly");
textBox3.Clear();
textBox7.Clear();
comboBox3.SelectedIndex = -1;
IDtoets = false;
} */
if (textBox1.Text == "")
{
NaamToets = false;
textBox1.Clear();
MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Name." + "\n" + "Make sure a name is typed in.");
}
else
{
NaamToets = true;
}
if (textBox2.Text != "")
{
VanToets = true;
}
else
{
VanToets = false;
textBox2.Clear();
MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Surname." + "\n" + "Make sure a Surname is typed in.");
}
if (textBox4.Text != "")
{
AdresToets = true;
}
else
{
AdresToets = false;
textBox4.Clear();
MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Adress." + "\n" + "Make sure a Adress is typed in.");
}
if (textBox8.Text != "")
{
KontakToets = true;
}
else
{
KontakToets = false;
textBox8.Clear();
MessageBox.Show("Client not added." + "\n" + "\n" + "Problem with Contact." + "\n" + "Make sure a Contact is typed in.");
}
if (comboBox1.SelectedIndex != -1)
{
RasToets = true;
}
else
{
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;
}
else
{
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;
}
else
{
KaartGeldToets = false;
textBox6.Clear();
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;
}
else
{
KaartNomToets = false;
textBox5.Clear();
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
MaakNuweDataRy();
//Update tabel
this.Validate();
this.klientBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.klientDataSet);
//Vertoon boodskap dat nuwe klient by geplaas is
MessageBox.Show("Client Added");
//Maak huidige form toe en vorige form weer oop
//New_Client.ActiveForm.Close();
// 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.
this.klientTableAdapter.Fill(this.klientDataSet.Klient);
}
private void button2_Click(object sender, EventArgs e)
{
//Maak huidige form toe en maak vorige form oop
ActiveForm.Close();
Form2 kind = new Form2();
kind.Show();
}
private void klientBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.klientBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.klientDataSet);
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}]