Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i add a new column(user_securitycode) in the table(userlogistic) and it shows NULL. i wanna replace the value by using the "UPDATE" command. but, it just replace NULL value and leave empty value in it. so, what is the exact codes or commands i need to insert the new value?

What I have tried:

Private Sub dataDisplay()
      cs.Open()
      cmd.CommandText = "SELECT * FROM userlogistic WHERE user_no = '" + Request.QueryString("id") + "'"
      cmd.Connection = cs
      reader = cmd.ExecuteReader

      If reader.HasRows Then
          While reader.Read
              uId = reader.Item("user_id").ToString()
              uRank.SelectedValue = reader.Item("user_rank").ToString()
              uname = reader.Item("user_name").ToString()
              uSec.SelectedValue = reader.Item("user_section").ToString()
              usecuritycode = reader.Item("user_securitycode").ToString()
          End While
      End If
      reader.Close()
      cs.Close()
  End Sub

  Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

          cs.Open()
          cmd.CommandText = "UPDATE userlogistic SET" & _
              " user_id = '" & Request.Form("uid") & "', " & _
              " user_rank = '" & uRank.SelectedItem.Value & "', " & _
              " user_name = '" & Request.Form("uname") & "' , " & _
              " user_securitycode = '" & Request.Form("usecuritycode") & "' , " & _
              " user_section = '" & uSec.SelectedItem.Value & "' WHERE user_no = '" + Request.QueryString("id") + "'"

          cmd.Connection = cs
          cmd.ExecuteNonQuery()
          cs.Close()
Posted
Updated 17-Nov-20 8:51am

Not like that!
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
As OriginalGriff has said it is bad practice to concatenate strings to build sql statements, however his solution does not offer a solution at all but rather a recomendation of best practices at best and in probably should have been better posted as a comment.

I looked at your code and I believe the problem/issue you may be having is a result of a misspelling in the line below.

" user_securitycode = '" & Request.Form("usecuritycode") & "' , " & _


Isn't
"usecuritycode"
misspelled or is that the actual name referenced on your form?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900