Help anyone,
I am new to C# and using databases. I am trying to insert information into a database using some code from Youtube. I was able to successfully select data from the database, but for whatever reason, when I try to execute the insert query (bolded and underlined below) below, my try catch block throws an exception related to a syntax error and referring to my insert statement:
I am new to C# and using databases. I am trying to insert information into a database using some code from Youtube. I was able to successfully select data from the database, but for whatever reason, when I try to execute the insert query (bolded and underlined below) below, my try catch block throws an exception related to a syntax error and referring to my insert statement:
C#:
namespace Movies
{
public partial class Form2 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form2()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Graham\Desktop\School.accdb;
Persist Security Info=False;";
}
private void btn_save_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
//command.CommandText = "insert into students(Names, Age, Gpa) values('" + txt_name.Text +"','" + txt_age.Text + "','" + txt_gpa.Text + "')";
[U][B]command.CommandText = "INSERT INTO students (Names, Age, Gpa) VALUES ('Abeni', '20', '3.2')";[/B][/U]
int numRows = command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
}
catch(Exception excpt)
{
MessageBox.Show("Error" + excpt);
}
}
}
}
Last edited by a moderator: