That's not a WPF problem: it's what you are sending to SQL.
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(txtUserId.Text) ? DBNull.Value : (object)txtUserId.Text;
You don't show any code which validates any of your text boxes, so if
txtUserId
contains any non-numeric characters then the conversion will fail and your SQL will not work.
It's generally a good idea to validate and convert user input at the start of the method, then only continue if valid data has been entered.
if (!int.TryParse(txtUserId.Text, out int id))
{
... report a problem ...
return;
}
You can then use the
id
value directly in your SQL without any null checking:
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = id;