Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Implementing Enable button to unlock user accounts - Part 99

Suggested Videos
Part 96 - Implementing change password page
Part 97 - Changing password by providing current password
Part 98 - Unlocking the locked user accounts using a web page

This is continuation to Part 98. To implement, the "Enable" button, make the following changes to the gridview control.



First Change: Specify the CommandArgument attribute of the Button control in the Template column.
<asp:TemplateField HeaderText="Enable">
    <ItemTemplate>
        <asp:Button ID="btnEnable" runat="server" CommandArgument='<%# Eval("UserName") %>' 
        Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>'/>
    </ItemTemplate>
</asp:TemplateField>



Second Change: Generate the "RowCommand" event handler for the GridView control.
1. Right Click on the GridView Control and Select properties.
2. In the "Properties Window", click on events icon.
3. In the events windows, double click on the text box next to "Row Command" event.

With these 2 changes the HTML of the "LockedAccounts.aspx" should look as shown below.
<div style="font-family: Arial">
<asp:GridView ID="gvLockedAccounts" runat="server" AutoGenerateColumns="False" 
    OnRowCommand="gvLockedAccounts_RowCommand">
    <Columns>
        <asp:BoundField DataField="UserName" HeaderText="User Name" />
        <asp:BoundField DataField="Email" HeaderText="Email" />
        <asp:BoundField DataField="LockedDateTime" HeaderText="Locked Date &amp; Time" />
        <asp:BoundField DataField="HoursElapsed" HeaderText="Hours Elapsed">
            <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Enable">
            <ItemTemplate>
                <asp:Button ID="btnEnable" CommandArgument='<%# Eval("UserName") %>' runat="server"
                    Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
</div>

Copy and paste the following private method in "LockedAccounts.aspx.cs" page.
private void EnableUserAccount(string UserName)
{
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("spEnableUserAccount", con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter paramUserName = new SqlParameter()
        {
            ParameterName = "@UserName",
            Value = UserName
        };

        cmd.Parameters.Add(paramUserName);

        con.Open();
        cmd.ExecuteNonQuery();
    }
}

Invoke EnableUserAccount() method, in RowCommand() event handler as shown below.
protected void gvLockedAccounts_RowCommand(object sender, GridViewCommandEventArgs e)
{
    EnableUserAccount(e.CommandArgument.ToString());
    GetData();
}

1 comment:

  1. Dear Venkat,

    Just for the info, Stored Procedure - spEnableUserAccount is missing

    Create proc spEnableUserAccount
    @UserName nvarchar(100)
    as
    Begin
    update tblUsers
    set RetryAttempts=0, IsLocked=NULL, LockedDateTime=NULL
    Where UserName = @UserName
    end

    ReplyDelete

It would be great if you can help share these free resources