Return a value from sql

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 the username exist. How should i modify my aspx .cs page to display error as a label in the aspx code page?

My original code on the aspx .cs page:

        string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            bool exists = false;

            // create a command to check if the username exists
            using (SqlCommand cmd = new SqlCommand("select count(*) from Member where UserName = @UserName", con))
            {
                cmd.Parameters.AddWithValue("UserName", UNameTxtBox.Text);
                exists = (int)cmd.ExecuteScalar() > 0;
            }

            // if exists, show a message error
            if (exists)
                ExistLabel.Text = "UserName exists";

My class file code:

public static string searchusername(string username)
{
    connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    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;
        }
        if (exists)
        {
            return "exist";
        }
        return null;
    }

And the code in my aspx cs file:

 MemberDB.searchusername(UNameTxtBox.Text);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);

Method 2

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";
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x