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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

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

    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#.
    Code:
    string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Domains\xxx.com\App_Data\customers.accdb; Persist Security Info=False;";
    
    		
    string strInsert = "INSERT INTO Customers ([FirstName], [LastName], [EmailAddress]) VALUES ('FN', 'LN', 'Email@email.com')";
    
    
            OleDbConnection oleConn = new OleDbConnection();
            OleDbCommand oleCmd = new OleDbCommand();
            OleDbDataAdapter oleDA = new OleDbDataAdapter();
    
            oleCmd.CommandText = strInsert;
            oleCmd.CommandType = CommandType.Text;
            oleCmd.Connection = oleConn;
            oleConn.ConnectionString = strConn;
    
            oleDA.SelectCommand = oleCmd;
    
            oleConn.Open();
    
             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!
  • Teme64
    New Member
    • Sep 2008
    • 10

    #2
    I'm not a C# programmer but
    Code:
    oleDA.SelectCommand = oleCmd;
    seems a bit suspicious if you try to do insert. Shouldn't you use:
    Code:
    oleDA.InsertCommand = oleCmd;
    Teme64 @ windevblog.blog spot.com

    Comment

    • DaveRook
      New Member
      • Jul 2007
      • 147

      #3
      Hi

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

      Thank you again though

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        Originally posted by Teme64
        I'm not a C# programmer but
        Code:
        oleDA.SelectCommand = oleCmd;
        seems a bit suspicious if you try to do insert. Shouldn't you use:
        Code:
        oleDA.InsertCommand = oleCmd;
        Teme64 @ windevblog.blog spot.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.InsertCo mmand.ExecuteNo nQuery(); or something) and close the database again.

        That should do the trick :-)

        Comment

        • DaveRook
          New Member
          • Jul 2007
          • 147

          #5
          Thank you everyone

          The code I was missing was

          oleDA.InsertCom mand = oleCmd;
          oleCmd.ExecuteN onQuery();

          Thank you everyone for helping! Beers on me :)

          Comment

          Working...