同じユーザー名を再度作成できないように、データベース テーブル「ユーザー」に対して「ユーザー名」が存在するかどうかを確認しようとしています。これをバリデータにしたいので、ユーザー名が存在する場合、メッセージボックスに存在が表示されます。
これについて教えてください。ユーザー名が存在するかどうかを追加して確認するボタンの背後に、これまでのところ次のコードがあります。
private void btnSignupNew_Click(object sender, EventArgs e)
{
if (txtUsername.Text == "")
{
errorUsername.SetError(txtUsername, "Enter A Username");
}
else if (txtPassword.Text == "")
{
errorPassword.SetError(txtPassword, "Enter A Valid Password");
}
//so if there isnt no error in the fields itll go on and add the data in to the database.
else{
//instance of sqlConnection
SqlConnection con = new SqlConnection("Data Source=etc");
//instance of sqlCommand
SqlCommand cmd = new SqlCommand("INSERT INTO [User] values ('" + txtForename.Text + "', '" + txtSurname.Text + "', '" + txtUsername.Text + "', '" + txtPassword.Text + "' )", con);
con.Open();
cmd.ExecuteNonQuery();
//query executed correcty or not
con.Close();