Reading what the visitor typed
A form is a set of values travelling from the browser to the server. Your job is to read each value safely.
Read each control by name
C#
string name = txtName.Text.Trim();
string chosen = ddlSize.SelectedValue;
bool agreed = chkAgree.Checked;Handle empty and odd input
- Trim whitespace so a space is not mistaken for a value.
- Check for an empty string before using it.
- Convert numbers with care, because text is not a number.
Never trust the browser. Anyone can send a form value that your page never offered. Treat every value as untrusted until you have checked it, and never build a database query by joining text together.
The tidiest way to check input is a validation control, beginning with making a field required.