Validate business rules (e.g., check if the data fits within acceptable ranges or?
dates).
Example:
string customerName = txtCustomerName.Text;
if (string.IsNullOrWhiteSpace(customerName))
throw new ArgumentException("Customer name cannot be empty");
SqlCommand command = new SqlCommand("INSERT INTO Customers
(CustomerName) VALUES (@CustomerName)", connection);
command.Parameters.AddWithValue("@CustomerName", customerName);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Follow:
This ensures that the CustomerName is not empty before inserting it into the database.