Hi - I am trying to insert data into an access 2010 table using c# and access 2010. For some reason I cannot get it to work. Please let me know what I might be doing wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void btSubmit_Click(object sender, EventArgs e)
{
OleDbConnection myDataConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString);
OleDbCommand nonqueryCommand = myDataConnection.CreateCommand();
// open the data connection.
myDataConnection.Open();
nonqueryCommand.CommandText = "INSERT INTO users (u_fname, u_lname) VALUES (@FirstName, @LastName)";
// Add Parameters to Command Parameters collection
Maybe we should add "@" as the prefix of parameter name:
No, it really doesn't matter how the parameters are called, because OleDb parameters are recognized by their position, NOT by their name! And when using SQL Server, the @ doesn't need to be added also....
geo0071
Member
29 Points
28 Posts
asp.net c# - Inserting data into access 2010
Aug 06, 2012 08:16 PM|LINK
Hi - I am trying to insert data into an access 2010 table using c# and access 2010. For some reason I cannot get it to work. Please let me know what I might be doing wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void btSubmit_Click(object sender, EventArgs e)
{
OleDbConnection myDataConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString);
OleDbCommand nonqueryCommand = myDataConnection.CreateCommand();
// open the data connection.
myDataConnection.Open();
nonqueryCommand.CommandText = "INSERT INTO users (u_fname, u_lname) VALUES (@FirstName, @LastName)";
// Add Parameters to Command Parameters collection
nonqueryCommand.Parameters["@FirstName"].Value = tbFname.Text;
nonqueryCommand.Parameters["@LastName"].Value = tbLname.Text;
nonqueryCommand.ExecuteNonQuery();
// close the data connection.
myDataConnection.Close();
// dispose the connection object to release the resources.
myDataConnection.Dispose();
}
}
;
hans_v
All-Star
35998 Points
6551 Posts
Re: asp.net c# - Inserting data into access 2010
Aug 06, 2012 10:28 PM|LINK
You're referring to parameters that don't exist....
Try
nonqueryCommand.Parameters.AddWithValue("FirstName", tbFname.Text);
nonqueryCommand.Parameters.AddWithValue("LastName", tbLname.Text);
http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: asp.net c# - Inserting data into access 2010
Aug 07, 2012 09:58 AM|LINK
Maybe we should add "@" as the prefix of parameter name:
nonqueryCommand.Parameters.AddWithValue("@FirstName", tbFname.Text);
If you are using:
nonqueryCommand.CommandText = "INSERT INTO users (u_fname, u_lname) VALUES (@FirstName, @LastName)";
Kindly correct me hans_v and OP.
Reguards!
geo0071
Member
29 Points
28 Posts
Re: asp.net c# - Inserting data into access 2010
Aug 07, 2012 01:51 PM|LINK
Great.. thanks. I appreciate the quick response from both of you!! Thanks again!!
hans_v
All-Star
35998 Points
6551 Posts
Re: asp.net c# - Inserting data into access 2010
Aug 08, 2012 10:33 PM|LINK
No, it really doesn't matter how the parameters are called, because OleDb parameters are recognized by their position, NOT by their name! And when using SQL Server, the @ doesn't need to be added also....