vb.net - Validate SQL data and check for Null -


i have textbox(txtordernum) enter 'order' number , update 'userfield' based on date generated in textbox(txtdate), want validate 'order' number entered matches 'order' number in database, update 'userfield1' if null, if doesn't match nothing.

my goal, don't want overwrite data in userfield1, , want ensure update correct 'order' number exist.

****i've updated code based on suggestion below, need validate if 'order' number exist , matches order number entered in textbox, run update query. (also need parameterized query, have ideas use help)****

public sub executequery(byval query string)     try         dim cmd new sqlcommand(query, conn)         conn.open()         if (conn.state = connectionstate.open)             cmd.executenonquery()             conn.close()         else             messagebox.show("check connection")             conn.close()         end if          catch ex exception         msgbox(ex.tostring)         return             conn.close()     end try end sub 

'my call event via enter key within textbox (txtordernum)

dim conn new sqlconnection("my data source")     try         dim updatequery string = ("update [data].[dbo].[order] set [userfield1] = '" & txtdate.text.trim() & "'  [order] ='" & txtordernum.text.trim() & "' , [userfield1] null")         if e.keychar = chr(13) 'chr(13)              if txtordernum.text.length >= 8                 'messagebox.show(updatequery)                 executequery(updatequery)         else             messagebox.show("invalid order number'")         end if     catch ex exception         msgbox(ex.tostring)         return     end try 

as n0alias stated, add "and [userfield1] null".

just careful way build query. building 1 example can allow sql injection.

you should use 'sqlcommand.parameters' property add values query.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -