c# - How to check if user left register field empty -


i have registration form. in case contain username , password.

when created table in sql server, put both columns on not null when try register without entering either username or password there no errors , forms 'registrates' new user example without password (if didn't enter password).

here code if can me problem, , if other wrong/poorly written code because i'm new in of this. think did on sql injection problem never know. thank on reply.

private void button1_click(object sender, eventargs e)     {          sqlconnection cn = new sqlconnection("data source=home-pc;initial catalog=testdb1;integrated security=true");      try     {         cn.open();         string query = "insert dbo.users (uusername, upassword) values (@uusername, @upassword)";          sqlcommand command = new sqlcommand(query, cn);         command.parameters.addwithvalue("@uusername", textbox1.text);         command.parameters.addwithvalue("@upassword", textbox2.text);          command.executenonquery();          messagebox.show("ok");          this.userstableadapter.fill(this.testdb1dataset.users);      }     catch (exception ex)     {         messagebox.show("error");     }         {         cn.close();     }     } 

if i've understood correctly, have application written in c# interacts sql server end.

you should able add check in c# code check if text boxes empty or not when click on button. c# rusty done thus:

if (string.isnullorwhitespace(textbox1.text) || string.snullorwhitespace(textbox2.text))  {     messagebox.show("invalid entry");  } 

sadly don't have visual studio check this, i'd check text boxes aren't empty before doing sql stuff.

also i'd change name of textboxes you'll remember, otherwise you'll find nightmare when comes maintaining code.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -