Thursday 30 January 2014

DotNetNuke / DNN - Method to delete user role programmatically in C#.NET

This is a simple method how to delete the dnn user role programmatically in C#.NET.

Method

private void DeleteUserRoleByName(string username, string roleName)
    {
        //Get Current Dnn Portal ID
        var portalID =
            DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().PortalId;

        //Initialize RoleController
        var rc = new DotNetNuke.Security.Roles.RoleController();

        //Retrieve role
        var ri = rc.GetRoleByName(portalID, roleName);

        // Delete User role
        rc.DeleteUserRole(
            portalID, 
            DotNetNuke.Entities.Users.UserController.GetUserByName(portalID, username).UserID, ri.RoleID );

    }

Usage

DeleteUserRoleByName("username", "rolename");

Thursday 23 January 2014

ASP.NET - Code to select Year, Month and Day from drop down list

Hi,
Here I created a small page to select the Year, Month and Day manually in ASP.NET.
In this page if we select the Year from years drop down list then the respected Months binds to the Months drop down list like that if you select the Month from the months drop down list respected Days will be binds to the Days drop down list.

Here special thing is the days drop down list will load the no.of days in the Month of the Year.

DayMonthYear.aspx

<%@ Page Language="C#" %>
<html>
    <head  runat="server">
        <title>
            Select Year Month and Day from drop down list in ASP.NET
        </title>
    </head>
<body>
    <form id="form1" runat="server">
        <table class="chartoptions">
            <tr>
                <td>
                    <label>Select Year:</label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" Style="width: 90px;"
                        OnSelectedIndexChanged="ddlYear_SelectedIndexChanged">
                        <asp:ListItem Selected="True" Value="Select">Select</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Select Month:</label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" Style="width: 90px;"
                        OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged">
                        <asp:ListItem Selected="True" Value="Select">Select</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Select Day:</label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlDay" runat="server" Style="width: 90px;">
                        <asp:ListItem Selected="True" Value="Select">Select</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadYears();
        }
    }
    private void LoadYears()
    {
        ddlYear.Items.Clear();
        ddlYear.DataSource = Enumerable.Range(2011, 10);
        ddlYear.DataBind();
        ddlYear.Items.Insert(0, new ListItem("Select", "Select"));
        ddlYear.SelectedIndex = 0;
    }
 
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDays();
    }
 
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadMonths();
        LoadDays();
    }
 
    private void LoadDays()
    {
        ddlDay.Items.Clear();
        if (ddlMonth.SelectedIndex != 0)
        {
            System.Globalization.DateTimeFormatInfo dtfi = 
                System.Globalization.DateTimeFormatInfo.CurrentInfo;
            int numberOfDaysInMonth =
                DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), 
                                     Convert.ToInt32(ddlMonth.SelectedValue));
            ddlDay.DataSource = Enumerable.Range(1, numberOfDaysInMonth);
            ddlDay.DataBind();
        }
        ddlDay.Items.Insert(0, new ListItem("Select", "Select"));
        ddlDay.SelectedIndex = 0;
    }
 
    private void LoadMonths()
    {
        ddlMonth.Items.Clear();
        if (ddlYear.SelectedIndex != 0)
        {
            System.Globalization.DateTimeFormatInfo info = 
                            System.Globalization.DateTimeFormatInfo.CurrentInfo;
            for (int i = 1; i <= 12; i++)
            {
                ddlMonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
            }
        }
        ddlMonth.Items.Insert(0, new ListItem("Select", "Select"));
        ddlMonth.SelectedIndex = 0;
    }

</script>