con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into Login values('" + textBoxUsername.Text + "', '" + textBoxRetype.Text + "')";
if (cmd.ExecuteScalar() != null) { } else { MessageBox.Show("Username exist"); con.Close(); }
cmd.ExecuteNonQuery(); cmd.CommandText = "insert into Employee values('" + textBoxName.Text + "','" + textBoxEmail.Text + "','" + textBoxAddress.Text + "','" + textBoxMobile.Text + "')"; cmd.ExecuteNonQuery(); con.Close(); display_data(); MessageBox.Show("Account Registered!");
Im usimg c# windows form in Visual Studio 2017. From the above coding, i have used cmd.ExecuteScalar to prevent users from using the duplicate usernames but its not working. When i clicked on the register button after keying in all the name,email and etc.... , the message box will keeo showing "Username exisit" even if its not duplicate usernames.