Below is attached code, Code is working fine, But it is not inserting the Values into access database tables that I have created, Neither Code shows any error
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void submit_Click(object sender, EventArgs e)
{
OleDbConnection cnon = new OleDbConnection();
cnon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\visual_c\Database71.accdb";
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
cnon.Open();
command.Connection = cnon;
command.ExecuteNonQuery();
cnon.Close();
}
}
}
You are missing a space in here:
"(Asset_name,Asset_number)VALUES"
try to change your command string:
command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
And you should use parameterized queries to prevent Sql Injection attacks
command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES(@name,@number)";
command.Parameters.AddWithValue("@name", textBox1.Text);
command.Parameters.AddWithValue("@number", textBox2.Text);
Use parameteraized query Make sure your button OnClick event
command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES (@Assetname,@Assetnumber)",cnon;
command.Parameters.AddWithValue("@Assetname", textBox1.Text);
command.Parameters.AddWithValue("@Assetnumber", textBox2.Text);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.