Connecting Tech Pros Worldwide Forums | Help | Site Map

Insert (SQL) won't work! Yet, I have no error message

Member
 
Join Date: Jul 2007
Posts: 70
#1: Nov 17 '08
Hi

I am trying to do a basic insert into an Access database (I have done this via MSSQL before and it was fine). I have done this with asp.old and no problems.

I am now using Access 2007 and C#.
Expand|Select|Wrap|Line Numbers
  1. string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Domains\xxx.com\App_Data\customers.accdb; Persist Security Info=False;";
  2.  
  3.  
  4. string strInsert = "INSERT INTO Customers ([FirstName], [LastName], [EmailAddress]) VALUES ('FN', 'LN', 'Email@email.com')";
  5.  
  6.  
  7.         OleDbConnection oleConn = new OleDbConnection();
  8.         OleDbCommand oleCmd = new OleDbCommand();
  9.         OleDbDataAdapter oleDA = new OleDbDataAdapter();
  10.  
  11.         oleCmd.CommandText = strInsert;
  12.         oleCmd.CommandType = CommandType.Text;
  13.         oleCmd.Connection = oleConn;
  14.         oleConn.ConnectionString = strConn;
  15.  
  16.         oleDA.SelectCommand = oleCmd;
  17.  
  18.         oleConn.Open();
  19.  
  20.          Response.Redirect("website.aspx");

I run the page and click the button which runs the code above! I know it's getting to the end as the redirect works!! My database does not alter!

It's almost as if it's ignored everything! What have I done wrong! I know I must have missed something!

Newbie
 
Join Date: Sep 2008
Posts: 10
#2: Nov 17 '08

re: Insert (SQL) won't work! Yet, I have no error message


I'm not a C# programmer but
Expand|Select|Wrap|Line Numbers
  1. oleDA.SelectCommand = oleCmd;
seems a bit suspicious if you try to do insert. Shouldn't you use:
Expand|Select|Wrap|Line Numbers
  1. oleDA.InsertCommand = oleCmd;
Teme64 @ windevblog.blogspot.com
Member
 
Join Date: Jul 2007
Posts: 70
#3: Nov 17 '08

re: Insert (SQL) won't work! Yet, I have no error message


Hi

Thank you for the suggestion, but sadly this did not solve it!

Thank you again though
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 283
#4: Nov 17 '08

re: Insert (SQL) won't work! Yet, I have no error message


Quote:

Originally Posted by Teme64

I'm not a C# programmer but

Expand|Select|Wrap|Line Numbers
  1. oleDA.SelectCommand = oleCmd;
seems a bit suspicious if you try to do insert. Shouldn't you use:
Expand|Select|Wrap|Line Numbers
  1. oleDA.InsertCommand = oleCmd;
Teme64 @ windevblog.blogspot.com

I agree with Teme64 :-)

Furthermore, you will have to execute the query. All you did now was telling oleDA what his command was. Open the database, execute the query (oleDA.InsertCommand.ExecuteNonQuery(); or something) and close the database again.

That should do the trick :-)
Member
 
Join Date: Jul 2007
Posts: 70
#5: Nov 17 '08

re: Insert (SQL) won't work! Yet, I have no error message


Thank you everyone

The code I was missing was

oleDA.InsertCommand = oleCmd;
oleCmd.ExecuteNonQuery();

Thank you everyone for helping! Beers on me :)
Reply


Similar .NET Framework bytes