Inserting records into access database

Last post 07-01-2009 12:00 PM by hans_v. 5 replies.

Sort Posts:

  • Inserting records into access database

    07-01-2009, 8:49 AM
    • Member
      6 point Member
    • FrankEnem
    • Member since 06-24-2009, 11:58 AM
    • Posts 13

     Please I have a web form with two textbox Account Id and Account Name. I have another button submit. I want to insert the values in the textbox in the access table when the submit button is click.

    ------------

    I have created an sqldatasource; sqldatasource1 on the webform and written this code on the button_click

    SqlDataSource1.InsertCommand = "Insert INTO USERS (ACCOUNTID, ACCOUNTNAME)" & _
      "Values(01011,Accounts Payable)"
      SqlDataSource1.UpdateParameters.Clear()
      SqlDataSource1.InsertParameters.Add("ACCOUNTID", Me.Account.Text)
      SqlDataSource1.InsertParameters.Add("ACCOUNTNAME", Me.AccountName.Text)
      SqlDataSource1.Insert()

    This gave me an error message " Syntax Error in INsert"

    ------------

    If you are responding to this thread, pls bear in mind that I an new and started this just a few days ago. I will like everything to be very simple.


    Thanks 


     

     

  • Re: Inserting records into access database

    07-01-2009, 9:30 AM
    Answer
    • Contributor
      5,138 point Contributor
    • papabear
    • Member since 08-08-2005, 3:49 PM
    • Posts 851

    Your insert statement should be more like this:

    SqlDataSource1.InsertCommand="Insert INTO USERS (ACCOUNTID, ACCOUNTNAME) " & _
            " VALUES(@AccountID, @AccountName)"
    SqlDataSource1.InsertParameters.Add("@AccountId", Me.Account.Text)
    SqlDataSource1.InsertParameters.Add("@AccountName", Me.AccountName.Text)

    Basically you need to specify the parameters using the @ and then refer to them on the InsertParameters section...

    See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insert(VS.80).aspx for more...

    hope this helps,

    m

    give me suggestions for what to blog... http://www.myfriedmind.com/techblog -> thx

    Mark as "Answered" if this solves that wee old problem...
  • Re: Inserting records into access database

    07-01-2009, 9:31 AM
    • Member
      549 point Member
    • adeelehsan
    • Member since 07-18-2005, 10:56 AM
    • UAE
    • Posts 94

    Hello

    Try using AccessDataSource instead of SqlDataSource. Also enclose the table name "USERS" in square braces like:

    insert into [USERS]........................

    Because USERS may be a reserve word or reserve name of an existing object. 

    Please Mark AS Answer if it helped.
    Regards
    ADEEL EHSAN
    Filed under:
  • Re: Inserting records into access database

    07-01-2009, 10:56 AM
    Answer
    • Star
      8,280 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,423

    adeelehsan:
    Because USERS may be a reserve word or reserve name of an existing object.
     

    No it is not (USER is). The complete list of Jet (in an ASP.NET environment we're dealing with a Jet Database Engine) can be found here

    papabear:
    Basically you need to specify the parameters using the @
     

    No, that's not required. Jet will thread any unknown string as a parameter. Off course, it is a good habit using @

    papabear:
    then refer to them on the InsertParameters section...

    Be aware that when using OleDB, parameters are not recognized by their name, but by their position. So the parameters must be addes in the parametercollection in the same order they appear in the sql string.

    SqlDataSource1.InsertParameters.Add("Paremeter1", Me.Account.Text)
    SqlDataSource1.InsertParameters.Add("Paremeter2 Or any other name, even an empty string will do", Me.AccountName.Text)

    is OK

    SqlDataSource1.InsertParameters.Add("@ACCOUNTNAME", Me.AccountName.Text)
    SqlDataSource1.InsertParameters.Add("@ACCOUNTID", Me.Account.Text)

    is NOT OK

    FrankEnem:
    SqlDataSource1.InsertCommand = "Insert INTO USERS (ACCOUNTID, ACCOUNTNAME)" & _
      "Values(01011,Accounts Payable)"
      SqlDataSource1.UpdateParameters.Clear()
      SqlDataSource1.InsertParameters.Add("ACCOUNTID", Me.Account.Text)
      SqlDataSource1.InsertParameters.Add("ACCOUNTNAME", Me.AccountName.Text)
     

    In this case the first 'parameter' isn't threaded as a parameter, because it is a numeric value. Furthurmore, parameters may not have spaces in them. If you have parameters (or field or tablenames) with spaces, or you're using Reserved words, you should enclose them in brackets, like [Accounts Payable]. Change the code as papabear showed you and it should work

     

  • Re: Inserting records into access database

    07-01-2009, 11:37 AM
    • Star
      7,932 point Star
    • amit.jain
    • Member since 10-06-2008, 5:09 AM
    • Delhi India
    • Posts 1,264

    FrankEnem:
    pls I am using access database.

     

    than use olebd insted of SQL , and use accessDataSource instead of SqlDataSOurce

    refer link below

    http://forums.asp.net/p/1364640/2829655.aspx

    Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
    amiT jaiN

    ASP.NET C#.NET Articles
  • Re: Inserting records into access database

    07-01-2009, 12:00 PM
    Answer
    • Star
      8,280 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,423

    amit.jain:
    than use olebd insted of SQL
     

    So with OleDb you're not required to use SQL??????

    amit.jain:
    and use accessDataSource instead of SqlDataSOurce

    Quess what? The AccessDataSource control inherits the SqlDataSource class and replaces the ConnectionString property with a DataFile property to make it more convenient to connect to a Microsoft Access database

    http://msdn.microsoft.com/en-us/library/8e5545e1.aspx

    So everything the accessDatasource can do, the sqlDatasource control can do that also. The only difference is the connectionstring, I think it's better to use the sqlDatasource so you can store the connectionsting in the web.config, and more important, when you've a password protected mdb you cannot use the accessDataSource anyway....

Page 1 of 1 (6 items)