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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Error while insert DB

Error while insert DB

Scheduled Pinned Locked Moved C#
databasehelpquestion
6 Posts 4 Posters 1 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.
  • R Offline
    R Offline
    Ronen Kfir
    wrote on last edited by
    #1

    I'm trying to open a user with insert SQL query to Access. This is the statements: string sqlCreateNewUser = "INSERT INTO usersTbl ( UserName, Password) " + "VALUES (" + username.UserName+ ", " + username.Password + ")"; Think the eror derived from the class I wrote to user. This is the class: public class user { private string username; private long password; public user(string username, long password) { this.username = username; this.password = password; } /// /// Gets, Sets username. /// public string UserName { get { return this.username; } set { this.username = value; } } /// /// Gets password. /// public long Password { get { return this.password; } set { this.password = value; } } } Please help me find the false. BTW: sorry I don’t use indentation. Just couldn’t understand the tags system here... Where would I find some info on it? TIA, Ronen

    G R 2 Replies Last reply
    0
    • R Ronen Kfir

      I'm trying to open a user with insert SQL query to Access. This is the statements: string sqlCreateNewUser = "INSERT INTO usersTbl ( UserName, Password) " + "VALUES (" + username.UserName+ ", " + username.Password + ")"; Think the eror derived from the class I wrote to user. This is the class: public class user { private string username; private long password; public user(string username, long password) { this.username = username; this.password = password; } /// /// Gets, Sets username. /// public string UserName { get { return this.username; } set { this.username = value; } } /// /// Gets password. /// public long Password { get { return this.password; } set { this.password = value; } } } Please help me find the false. BTW: sorry I don’t use indentation. Just couldn’t understand the tags system here... Where would I find some info on it? TIA, Ronen

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Standard question #2: What error message do you get? --- b { font-weight: normal; }

      R 1 Reply Last reply
      0
      • R Ronen Kfir

        I'm trying to open a user with insert SQL query to Access. This is the statements: string sqlCreateNewUser = "INSERT INTO usersTbl ( UserName, Password) " + "VALUES (" + username.UserName+ ", " + username.Password + ")"; Think the eror derived from the class I wrote to user. This is the class: public class user { private string username; private long password; public user(string username, long password) { this.username = username; this.password = password; } /// /// Gets, Sets username. /// public string UserName { get { return this.username; } set { this.username = value; } } /// /// Gets password. /// public long Password { get { return this.password; } set { this.password = value; } } } Please help me find the false. BTW: sorry I don’t use indentation. Just couldn’t understand the tags system here... Where would I find some info on it? TIA, Ronen

        R Offline
        R Offline
        Rob Graham
        wrote on last edited by
        #3

        You need to enclose your values in single quotes:

        ... "VALUES ('" + username.UserName+ "','" + username.Password + "')";
        

        note the extra single quotes I have added. A better way to do this would be to use parameters and a stored procedure (querydef in access lingo), then you wouldn't have to worry with things like the single quotes (which will need to be doubled up if they are part of a name - O'Neill - becomes O''Neill - in order to work). If you use a strored procedure with parameters, you won't have to worry about escaping the embedded single quote. Absolute faith corrupts as absolutely as absolute power Eric Hoffer The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not. Eric Hoffer

        1 Reply Last reply
        0
        • G Guffa

          Standard question #2: What error message do you get? --- b { font-weight: normal; }

          R Offline
          R Offline
          Ronen Kfir
          wrote on last edited by
          #4

          The Error I get is: "Syntax error in INSERT INTO statement" Tried it with the extra quotes, with no use. I would rather do it this way at this stage. I think the syntax is OK (?) What do you think of the user class I wrote?

          G 1 Reply Last reply
          0
          • R Ronen Kfir

            The Error I get is: "Syntax error in INSERT INTO statement" Tried it with the extra quotes, with no use. I would rather do it this way at this stage. I think the syntax is OK (?) What do you think of the user class I wrote?

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Username or password might be reserved words, depending on what connection you use. Put brackets around them: [username], [password] --- b { font-weight: normal; }

            S 1 Reply Last reply
            0
            • G Guffa

              Username or password might be reserved words, depending on what connection you use. Put brackets around them: [username], [password] --- b { font-weight: normal; }

              S Offline
              S Offline
              sea
              wrote on last edited by
              #6

              The problem was with Password as a reserved word. once put it brackets around it- solved. cheers! Ronen

              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