Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. oleDb Insert problem

oleDb Insert problem

Scheduled Pinned Locked Moved C#
helpdatabase
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gmeii
    wrote on last edited by
    #1

    I am getting errors when I try to insert data into my database. I dont have any problems viewing the database. I think it is a problem with my insert statement: oleDbInsertCommand1.CommandText="INSERT INTO UserTable(Username,Password) VALUES('@NewUserTextBox.Text','@NewPasswordTextBox.Text')"; oleDbInsertCommand1.ExecuteNonQuery(); I have even tried it without the @ and other ways. This is the error I get below. System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()

    K 1 Reply Last reply
    0
    • G gmeii

      I am getting errors when I try to insert data into my database. I dont have any problems viewing the database. I think it is a problem with my insert statement: oleDbInsertCommand1.CommandText="INSERT INTO UserTable(Username,Password) VALUES('@NewUserTextBox.Text','@NewPasswordTextBox.Text')"; oleDbInsertCommand1.ExecuteNonQuery(); I have even tried it without the @ and other ways. This is the error I get below. System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()

      K Offline
      K Offline
      Ken Haley
      wrote on last edited by
      #2

      You can do it this way, but it's susceptible to SQL injection attacks (a common way that malicious users can attack a web site and destroy databases): oleDbInsertCommand1.CommandText="INSERT INTO UserTable(Username,Password) VALUES('" + NewUserTextBox.Text + "','" + NewPasswordTextBox.Text + "')"; Instead you should set up an oleDbParameter collection and attach it to the oleDBInsertCommand1 object. You'd populate the paramters from the two textboxes (NewUserTextBox and NewPasswordTextBox). If you named these parameters '@NewUser' and '@NewPassword' respectively, then you'd code the CommandText as follows (notice that there are no single quotes here): oleDbInsertCommand1.CommandText="INSERT INTO UserTable(Username,Password) VALUES(@NewUser, @NewPassword)"; This is the secure, recommended best-practice solution. There is more help on parameter collections in the Visual Studio on-line help.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups