0
Answer

My application doesnt insert table info

Ask a question
flummer

flummer

19y
1.9k
1
Hi, I'm pretty new to programing and I'm very new to C#. I took some code of the internet to try and learn how to store information in a MS access database. I made a form with 2 buttons. 1 is supposed to insert some info into a table that already exists. the other one is supposed to create a new table inside the database, but _neither_ of these buttons seem to work. I'm hoping someone will notice what iv done wrong and can tell me what to correct to make it work. Thank you in advance. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace databastest { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btn_insert; private System.Windows.Forms.Button btn_create; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btn_insert = new System.Windows.Forms.Button(); this.btn_create = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btn_insert // this.btn_insert.Location = new System.Drawing.Point(200, 256); this.btn_insert.Name = "btn_insert"; this.btn_insert.Size = new System.Drawing.Size(120, 32); this.btn_insert.TabIndex = 0; this.btn_insert.Text = "Insert"; this.btn_insert.Click += new System.EventHandler(this.btn_insert_Click); // // btn_create // this.btn_create.Location = new System.Drawing.Point(48, 256); this.btn_create.Name = "btn_create"; this.btn_create.Size = new System.Drawing.Size(120, 32); this.btn_create.TabIndex = 1; this.btn_create.Text = "Create"; this.btn_create.Click += new System.EventHandler(this.btn_create_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(368, 317); this.Controls.Add(this.btn_create); this.Controls.Add(this.btn_insert); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { } private void btn_insert_Click(object sender, System.EventArgs e) { string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\databaser\test1.mdb"; OleDbConnection myConnection = new OleDbConnection( connectionString ); myConnection.Open(); string query = "insert into EMPLOYEE_TABLE (EmployeeID, Name, Address) VALUES (101, 'John', '3960 CliffValley Way')"; OleDbCommand myCommand = new OleDbCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; //myCommand.ExecuteNonQuery(); } private void btn_create_Click(object sender, System.EventArgs e) { string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\databaser\test1.mdb"; OleDbConnection myConnection = new OleDbConnection( connectionString ); myConnection.Open(); string query = "Create TEST_TABLE (ID, Namn, Alder) VALUES (1, 'Olle', '18')"; OleDbCommand myCommand = new OleDbCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; } } }