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";
}
Add a label
in your page and set the Text
property of label to the value
returned
by the method.
<asp:Label runat="server" ID="userExists"></asp:Label>
And than in code behind do like this
userExists.Text = MemberDB.searchusername(UNameTxtBox.Text);