Return a value from sql

前端 未结 2 1911
遥遥无期
遥遥无期 2021-01-26 04:24

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

相关标签:
2条回答
  • 2021-01-26 04:56
    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";
    }
    
    0 讨论(0)
  • 2021-01-26 04:58

    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);
    
    0 讨论(0)
提交回复
热议问题