Same Username
-
Hi, I want to prevent the admin from being able to store 2 user records which both have the same username. So far, I have pupulated a Dataset variable with the details, but I am unable to work it into an IF statement. Any suggestions?? Thank you
Have you tried ?: string sql = @' IF NOT EXISTS(SELECT * FROM foo WHERE user = @username) BEGIN INSERT INTO foo (user) VALUES (@username) SET @Success = 1 END ELSE BEGIN SET @Success = 0 END'; bool bSuccess = false; using(SqlConnection conn = new SqlConnection("connection string")){ SqlCommand cmd = new SqlCommand(conn, sql); cmd.Parameters.Add("@username", SqlDbType.Varchar, 50); cmd.Parameters.Add("@Success", SqlDbType.Bit); cmd.Parameters["@username"].Value = DataSet.Tables[0].Rows[0]["Username"]; cmd.Parameters["@Success"].Direction = ParameterDirection.InputOutput; conn.Open(); cmd.ExecuteNonQuery(); bSuccess = (bool) cmd.Parameters["@Success"].Value; } if(!bSucess) { //tell the user }