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. Database & SysAdmin
  3. Database
  4. Insert in a table

Insert in a table

Scheduled Pinned Locked Moved Database
questiondatabasehelp
3 Posts 3 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.
  • A Offline
    A Offline
    alee15 10 88
    wrote on last edited by
    #1

    Hi everyone, I have a small problem which I really need to solve today. I am doing a program which the user needs to log in. Now, when the user clicks the login btn, the username of that particular user is saved in a table called Marks. Then a new form is opened in which the user can do a small exam and finally submit it. When he clicks the submit btn the mark is saved in the table, in the column called subjectMarks, near the username which was already saved before (in the column userName). Unfortunately it is working wrong since the username is saved in the first row and then the mark is saved in the 2nd row. How can I do it? The following is the code I used in the submit btn: string query = ("INSERT INTO Marks (" + subjectMarks + ") VALUES ('" + marks + "')"); OleDbCommand myCommand = new OleDbCommand(query); myCommand.Connection = myConnection; try { myCommand.ExecuteNonQuery(); } catch(Exception ex) { ex.ToString(); } Thanks

    C E 2 Replies Last reply
    0
    • A alee15 10 88

      Hi everyone, I have a small problem which I really need to solve today. I am doing a program which the user needs to log in. Now, when the user clicks the login btn, the username of that particular user is saved in a table called Marks. Then a new form is opened in which the user can do a small exam and finally submit it. When he clicks the submit btn the mark is saved in the table, in the column called subjectMarks, near the username which was already saved before (in the column userName). Unfortunately it is working wrong since the username is saved in the first row and then the mark is saved in the 2nd row. How can I do it? The following is the code I used in the submit btn: string query = ("INSERT INTO Marks (" + subjectMarks + ") VALUES ('" + marks + "')"); OleDbCommand myCommand = new OleDbCommand(query); myCommand.Connection = myConnection; try { myCommand.ExecuteNonQuery(); } catch(Exception ex) { ex.ToString(); } Thanks

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You need to do an UPDATE not an INSERT. You might want to take steps to prevent SQL Injection Attacks[^] Also, from the limited amount of information that I have about your data model I have a feeling that it is not normalised properly. You shouldn't need to inject the column name in the way you are doing.


      "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

      1 Reply Last reply
      0
      • A alee15 10 88

        Hi everyone, I have a small problem which I really need to solve today. I am doing a program which the user needs to log in. Now, when the user clicks the login btn, the username of that particular user is saved in a table called Marks. Then a new form is opened in which the user can do a small exam and finally submit it. When he clicks the submit btn the mark is saved in the table, in the column called subjectMarks, near the username which was already saved before (in the column userName). Unfortunately it is working wrong since the username is saved in the first row and then the mark is saved in the 2nd row. How can I do it? The following is the code I used in the submit btn: string query = ("INSERT INTO Marks (" + subjectMarks + ") VALUES ('" + marks + "')"); OleDbCommand myCommand = new OleDbCommand(query); myCommand.Connection = myConnection; try { myCommand.ExecuteNonQuery(); } catch(Exception ex) { ex.ToString(); } Thanks

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        string query = "UPDATE Marks SET subjectMarks = '" + marks + "' WHERE userName = '" + cUserNameVar + "'" This assumes that the username is unique in the Marks table, since every record with that user name will be changed to have the new subjectMarks value. Either that, or you need an ID column - when you INSERT the name into the table from the login btn, get the ID and use that in the WHERE clause of the UPDATE when you put the subjectMarks in later. ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

        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