sql - How to prevent certain data from being inserted into the database with If Else statements? -
i have 2 comboboxes, messagebox , send button. when app startups , click on send button comboboxes , messagebox empty, pop-up box comes , says "select client"
after doing this, go database , see has added new record table, though didn't put in data after clicking on "send" button. same applies when 1 of 3 controls have has data in it, other 2 don't, , program asks me enter data before succeeds. still adds record despite having if statements. doing wrong?
my code:
using con new sqlconnection(configurationmanager.connectionstrings("constr").connectionstring) con.open() using cmd new sqlcommand cmd.connection = con cmd.commandtext = "insert tblmytable(client, username, message) values('" & cboclient.text & "', '" & cbouser.text & "', '" & rtfmessage.text & "')" cmd.executenonquery() end using if cboclient.text = "" msgbox("select client") elseif cbouser.text = "" msgbox("select user") elseif rtfmessage.text = "" msgbox("enter message") else msgbox("message sent") end if con.close() end using
i think want (note not address parameterization concerns):
using con new sqlconnection(configurationmanager.connectionstrings("constr").connectionstring) if cboclient.text = "" msgbox("select client") elseif cbouser.text = "" msgbox("select user") elseif rtfmessage.text = "" msgbox("enter message") else con.open() using cmd new sqlcommand cmd.connection = con cmd.commandtext = "insert tblmytable(client, username, message) values('" & cboclient.text & "', '" & cbouser.text & "', '" & rtfmessage.text & "')" cmd.executenonquery() end using msgbox("message sent") end if con.close() end using
Comments
Post a Comment