I used the following code to insert data into a database
public partial class Products : Form
{
private OleDbConnection connection = new OleDbConnection();
public Products()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Folder\Database.mdb;Persist Security Info=False;";
}
private void cmdAdd_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO Products (ProductName, Quantity, Weight(g)) VALUES ('" + txtName.Text + "', '" + txtQuantity.Text + "', '" + txtWeight.Text + "')";
command.ExecuteNonQuery();
MessageBox.Show("Data saved");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
And the last two variables don't seem to work. I tried entering a string for the first field and it works perfectly, but when I go to insert a number on the last two variables it throws an exception. I've already tried doing int.Parse(txtQuantity.Text) but it doesn't work either.