Insert in a table
-
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
-
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
You need to do an
UPDATE
not anINSERT
. 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
-
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
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