Im trying move my sql connection to a method in a class file and return a value to check if the username existing in the db and display as text in the page to show others that t
public static bool searchusername(string username)
{
connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using(SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
bool exists = false;
using (SqlCommand comm = new SqlCommand("select count(*) from Member where UserName = @UserName", conn))
{
comm.Parameters.AddWithValue("@username", username);
exists = (int)comm.ExecuteScalar() > 0;
}
return exists;
}
In your page
if(MemberDB.searchusername(UNameTxtBox.Text))
{
ExistLabel.Text = "UserName exists";
}