Ms Access Database Inserting Incorrectly

Last post 11-02-2009 9:32 PM by joshmariam. 6 replies.

Sort Posts:

  • Ms Access Database Inserting Incorrectly

    11-01-2009, 4:16 PM
    • Member
      2 point Member
    • joshmariam
    • Member since 11-01-2009, 4:05 PM
    • Posts 4

    So for my project I need to insert into an msaccess database and the insert works but it puts the values in different columns.

    I'm using a hashtable atm which I think is the cause of my problems, but I'm not sure where to start making changes.



    I had to change the order of the column names in order for it to insert correctly but I don't think I should be keeping it that way.

    I tried using different lists but that didn't help and came with the same problem as with the hashtables.

    If anyone could point me in the right direction that'd be great

  • Re: Ms Access Database Inserting Incorrectly

    11-01-2009, 5:14 PM
    • Member
      23 point Member
    • steschu
    • Member since 10-29-2007, 11:02 AM
    • Posts 26

    Hi,

    When using Access, the order of the parameters has to be identical to the order of the columns. The '@' known from MS SQL is not supported with MS Access. SQL for MS Access simply uses the ? instead: ... VALUES (?, ?, ? ...

    Stephan


  • Re: Ms Access Database Inserting Incorrectly

    11-01-2009, 5:23 PM
    Answer

    OleDb parameters work on position, not name.  The SortedList is indeed your problem.  I'm guessing your DataLayer method iterates the items in the SortedList, creating parameters.  If you put a breakpoint on the line that calls the method, you will see that your SortedList has, well, sorted itself alphabetically.  If you want to pass an unknown number of parameter values to a generic method like this, just use simple arrays:

    string[] p = new[] {"@e", "@b", "@f", "@a", "@d", "@c"};
    object[] o = new[] (dv.e, dv.b, dv.f, dv.a, dv.d, dv.c };

    DataLayer.ExecuteNonQuery(qry, p, o);

    Then iterate the arrays to create an provide values ot the parameters.


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Ms Access Database Inserting Incorrectly

    11-01-2009, 5:26 PM

    steschu:
    The '@' known from MS SQL is not supported with MS Access.

    Yes it is. 

    I think what you mean is that the named parameters that SqlClient supports are not supported by OleDb or ODBC.



    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Ms Access Database Inserting Incorrectly

    11-02-2009, 1:27 AM
    • Member
      23 point Member
    • steschu
    • Member since 10-29-2007, 11:02 AM
    • Posts 26

    Can you tell more about using the @ with OleDb or provide a link to a documentation? Visual Studio always generates SQL Statements with '?'.

  • Re: Ms Access Database Inserting Incorrectly

    11-02-2009, 2:40 AM
    • Star
      9,008 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,551

    steschu:

    Can you tell more about using the @ with OleDb or provide a link to a documentation? Visual Studio always generates SQL Statements with '?'.

     

    There is no real difference in using ? or named parameters (with or without @). But sometimes it is easier to use named parameters, when you need to use the same parameter multiple times in a query, for example when you're working with optional parameters:

    http://www.mikesdotnetting.com/Article/68/An-ASP.NET-Search-Engine-with-MS-Access-for-optional-search-criteria

     

    By the way, next time you have a new question, start a new thread:

    http://www.mikesdotnetting.com/Article/69/How-to-get-your-forum-question-answered-avoid-thread-jacking

     

  • Re: Ms Access Database Inserting Incorrectly

    11-02-2009, 9:32 PM
    Answer
    • Member
      2 point Member
    • joshmariam
    • Member since 11-01-2009, 4:05 PM
    • Posts 4

    Nevermind I figured it out =D


    Thanks a lot

Page 1 of 1 (7 items)