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>

Monday, 23 December 2013

JavaScript - Generate an array of numbers in between range

<script type="text/javascript">
        var numbers = [];
        min = 0;
        max = 100;
        for (var i = min; numbersArray.push(i++) < max;);
        // numbers = [0,1,2,3 ... 100] 
 </script>

Sunday, 15 December 2013

C#.NET - Sending email through Gmail SMTP server in ASP.NET

Hi welcome to dotnetcookie.blogspot.in

In this article I will show you how to send an email through the Gmail SMTP server.

In this article we should use Gmail address (sample@gmail.com) and password (*******) to send an email.


SendMail.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="SendMail.SendMail" %>

<html>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h3>Sending Email in C#.NET using gmail.</h3>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Email</span>
                </td>
                <td>
                    <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Password</span>
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    <span>To</span>
                </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Subject</span>
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Body</span>
                </td>
                <td>
                    <asp:TextBox ID="txtBody" TextMode="MultiLine" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="btnSendEmail" runat="server" Text="SendMail" OnClick="btnSendEmail_Click" />
                </td>
            </tr>
        </table>

    </form>
</body>
</html>

Form


SendMail.aspx.cs

namespace SendMail
{
    public partial class SendMail : System.Web.UI.Page
    {
        protected void btnSendEmail_Click(object sender, System.EventArgs e)
        {
            try
            {
                // retrive all the details from the page.
                var fromAddress = txtFrom.Text;
                var toAddress = txtTo.Text;
                string fromPassword = txtPassword.Text;
                string subject = txtSubject.Text;
                string body = txtBody.Text;
 
                // Create a new SMTP Client instance by passing the SMTP details.
                var smtp = new System.Net.Mail.SmtpClient();
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
                    smtp.Timeout = 20000;
                }
 
                //Send the email using the send method of the System.Net.Mail.SmtpClient
                smtp.Send(fromAddress, toAddress, subject, body);
                Response.Write("<script>alert('Mail Sent Successfully...')</script>");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Response.Write("<script>alert('The username or password you entered is incorrect.')</script>");                
            }
            catch (System.Exception ex)
            {
                Response.Write("<script>alert('"+ex.Message+"')</script>");               
            }
            finally
            {
                ResetControls();
            }
        }
        
        private void ResetControls()
        {
            txtFrom.Text = string.Empty;
            txtTo.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtSubject.Text = string.Empty;
            txtBody.Text = string.Empty;
        }
    }
 
}

Form


Wednesday, 11 December 2013

HTML/CSS/jQuery - Show hide div when select option is selected


Hi in this small article i will show you how to show hide div when select (HTML Combobox) option is selected.

1. Create a html page.
2. Add reference to jQuery script.
3. Design the page with one combobox and one div.
4. Write the jQuery script to show and hide the div when select (HTML Combobox) option is selected.

Use the following script to to show and hide the div when select (HTML Combobox) option is selected.

HTML

<html>
<head>
    <!--load jQuery into your page by giving reference-->
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //Chage function call when you change the combobox value
            $("#action").change(function () {               
                var value = $("#action").val();
                //Show and hide the div tag based on the selected value of combobox.
                if (value == 'show') {
                    $("#divShowHide").show();
                }
                else {
                    $("#divShowHide").hide();
                }

            });
        });
    </script>
</head>
<body>
    Select Action:
    <select id="action">
        <option value="show">show</option>
        <option value="hide">hide</option>
    </select>
    <div id="divShowHide" style="height: 100px; width: 300px; background-color: gray; padding: 10px;">
        Sample Text
    </div>
</body>
</html>


Design




Design