I have the following code in C#, for a interface with a DB. I introduce values in TextBoxes on the interface. No error, but when executed, data is not stored in the DB created with Microsoft Access 2010. I'll give the complete code, here. Thanks for answers!
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;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\lumi\\Desktop\\Test_DataB.accdb");
OleDbDataAdapter ad = new OleDbDataAdapter();
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void indexBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.indexBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.test_DataBDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'test_DataBDataSet.Index' table. You can move, or remove it, as needed.
this.indexTableAdapter.Fill(this.test_DataBDataSet.Index);
}
private void button1_Click(object sender, EventArgs e)
{
try
{ con.Open();
ad.InsertCommand = new OleDbCommand("Insert @Birth_month, @Firstname, @Surname, @ID", con);
ad.InsertCommand.Parameters.Add("@Birth_month", OleDbType.Numeric).Value = int.Parse(textBox1.Text);
ad.InsertCommand.Parameters.Add("@Firstname", OleDbType.VarChar).Value = textBox2.Text.ToString();
ad.InsertCommand.Parameters.Add("@Surname", OleDbType.VarChar).Value = textBox3.Text.ToString();
ad.InsertCommand.Parameters.Add("@ID", OleDbType.Numeric).Value = int.Parse(textBox4.Text);
ad.InsertCommand.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}