Sunday, 4 February 2018

Mail Alerts

A Task to send the details of the anything using Table ( here Faculty Course details) through Email:

Output:


Database:

USE [Testdb]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Blog_Department](
[DNo] [int] NOT NULL,
[DName] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[CreatedBy] [nvarchar](50) NULL,
[UpdatedDate] [datetime] NULL,
[UpdatedBy] [nvarchar](50) NULL,
[Status] [int] NULL,
 CONSTRAINT [PK_Blog_Department] PRIMARY KEY CLUSTERED
(
[DNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


GO

*********************************************************************

USE [Testdb]
GO

/****** Object:  Table [dbo].[Blog_Course]    Script Date: 2/4/2018 11:50:11 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Blog_Course](
[CID] [int] IDENTITY(1,1) NOT NULL,
[CName] [nvarchar](50) NULL,
[StartDate] [datetime] NULL,
[Duration] [nvarchar](50) NULL,
 [EndDate] [nvarchar](50) NULL,
[Time] [nvarchar](50) NULL,
[Dno] [int] NULL,
[BatchType] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[CreatedBy] [nvarchar](50) NULL,
[UpdatedDate] [datetime] NULL,
[UpdatedBy] [nvarchar](50) NULL,
[Status] [int] NULL,
 CONSTRAINT [PK_Blog_Course] PRIMARY KEY CLUSTERED
(
[CID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

***************************************************************************

USE [Testdb]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Blog_Faculty](
[FID] [int] IDENTITY(1001,1) NOT NULL,
[FName] [nvarchar](50) NULL,
[EmailID] [nvarchar](50) NULL,
[Mobile] [nvarchar](50) NULL,
[DNo] [int] NULL,
[CreatedDate] [datetime] NULL,
[CreatedBy] [nvarchar](50) NULL,
[UpdatedDate] [datetime] NULL,
[UpdatedBy] [nvarchar](50) NULL,
[Status] [int] NULL,
 CONSTRAINT [PK_Blog_Faculty] PRIMARY KEY CLUSTERED
(
[FID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

********************************************************************
USE [Testdb]
GO

/****** Object:  Table [dbo].[Blog_StudentDetails]    Script Date: 2/4/2018 11:52:07 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Blog_StudentDetails](
[SID] [int] NULL,
[SName] [nvarchar](50) NULL,
[EmailID] [nvarchar](50) NULL,
[Mobile] [int] NULL
) ON [PRIMARY]

GO
*******************************************************************
Added EndDate in Blog_course Table

*************************************************
Stored Procedure:


Create procedure usp_Blog_FacultyReport
as
begin
select [dbo].[Blog_Department].[DName] as Category,[Blog_Course].CName as Course,[Blog_Course].BatchType,convert(varchar,[dbo].[Blog_Course].StartDate,103) as StartDate,
[dbo].[Blog_Course].Duration,[dbo].[Blog_Course].[Time] as Timings,
(CASE 
      WHEN [dbo].[Blog_Course].StartDate > = DATEADD(dd,0,DATEDIFF(dd,0,GETDATE())) then 'Upcoming Batch'
      WHEN [dbo].[Blog_Course].EndDate <= DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) then 'Batch Completed'
  WHEN convert(varchar, [dbo].[Blog_Course].StartDate, 120) <= (convert(varchar, [dbo].[Blog_Course].EndDate, 120))  then 'Going on Batch'
       END) as Status from [dbo].[Blog_Department] inner join
[dbo].[Blog_Course] 
on [dbo].[Blog_Department].DNo = [dbo].[Blog_Course] .Dno
where [dbo].[Blog_Course].[Status]=1
end
===================================================

Note: Based on requirement you we can do changes to the above tables and stored procedures


CoursesInfo.aspx.cs:


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CourseInfo : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

// CoursesInfo.aspx add Button

    protected void Button1_Click(object sender, EventArgs e)
    {
        FacultyReport obj = new FacultyReport();
        obj.SendMail();
    }
      public class FacultyReport 
    {
         
        public string PrepareTable()
        {
           string currentDate = DateTime.Now.ToString("dd-MM-yyyy");
            string mailBody = @"<html><body><p><Strong>Dear All,</strong></p><h2 style='color:blue;'>Batch Updates Of Mr. Narasimha Rao</h2><table border=2 cellpadding='5' cellspacing='0' style='padding:5px;' width='80%'> <tr style='background-color:#A9A9A9'><th>S.No</th><th>Category</th><th>Courses</th><th>Batch Type</th><th>StartDate</th><th>Duration</th><th>Timings</th><th>Status</th></tr><tr style = 'background:#6495ED;' >@courseTable@</tr></table><br/><br/><table><tr> <td style='background-color:#A9A9A9';line-height:20 ;padding:15px 0;font-size:14px;color:#444444;> Sent from Dotnet Narasimha </td> </tr> </ body></ html > ";
            mailBody = mailBody.Replace("@date@", currentDate);
            string SiteSqlServer = "Data source=xx;database=Testdb;Uid=xx;Password=xxx;";
           
            using (var con = new SqlConnection(SiteSqlServer))
            {
                using (SqlCommand cmd = new SqlCommand("usp_Blog_FacultyReport", con))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    if (ds.Tables.Count > 0)
                    {
                        DataTable courseTable = (ds.Tables[0].DefaultView).ToTable();
                        string rowStr = string.Empty;
                        int sno = 1;
                        if (courseTable.Rows.Count > 0)
                        {
                            foreach (DataRow row in courseTable .Rows)
                            {
                                rowStr += "<tr style='background:#6495ED;width='80%'>";
                                rowStr += "<td>" + sno.ToString() +"</td>";
                                rowStr += "<td align=center>" + row["Category"].ToString() + "</td>";
                                rowStr += "<td align=center>" + row["Course"].ToString() + "</td>";
                                rowStr += "<td align=center>" + row["BatchType"].ToString() + "</td>";
                                rowStr += "<td align=center>" + row["StartDate"].ToString() + "</td>";
                                rowStr += "<td align=center>" + row["Duration"].ToString() + "</td>";
                                rowStr += "<td align=center>" + row["Timings"].ToString() + "</td>";
                                rowStr += "<td align=center>" + row["Status"].ToString() + "</td>";
                                
                                rowStr += "</tr>";
                                sno++;
                            }

                            mailBody = mailBody.Replace("@courseTable@", rowStr);
                        }
                        else
                        {
                            rowStr += "<tr style='background:#fff;'><td colspan='16'>No information         available</td></tr>";
                            mailBody = mailBody.Replace("@courseTable@", rowStr);
                        }

                   }
                }
            }
            return mailBody;
        }
      

        public void SendMail()
        {
            try
            {
                string body = PrepareTable();
                using (var message = new MailMessage("From@gmail.com", "From@gmail.com"))
                {
                    message.Subject = "Mr. Narasimha Rao Course Schedule Details :: " + DateTime.Now.ToString("dd-MM-yyyy");
                    message.To.Add(new MailAddress("To@gmail.com"));
                    message.CC.Add(new MailAddress("To@gmail.com"));
                    message.Bcc.Add(new MailAddress("To@gmail.com"));
                    message.Body = body;
                    message.IsBodyHtml = true;
                    SmtpClient client = new SmtpClient();
                    client.EnableSsl = true;
                    client.Host = "smtp.gmail.com";
                    client.Port = 587;
                    client.Credentials = new NetworkCredential("From@gmail.com.com", "password");
                    client.Send(message);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}

OUTPUT:







Note:  In Department table I took Department name as Basics and Advanced as Category.
We will explore this example more, as we have just posted to send mail updates .we will update this using Students table to send mulitple mail alerts.

                                    Lets hope you will also Explore

Saturday, 9 January 2016

Jquery Effects

Class Room Examples Of Mr. Narasimha Sir

By Srinivas Vasu



Monday, 9 November 2015

Clear all fields in a form using Jquery

                                       Clear / Reset Buttons
Clear Button: When you clear a form, you would be removing ALL values from the form.
Reset Button: When you resets a form, it will resets the original values of all fields in a form.
OR 
Reset all form fields to their default values.
  ModelClass for this Example 
    public partial class EmployeeDetail
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Address { get; set; }
       [Display(Name="Languages Known")]
        public string Languages { get; set; }
        public Nullable<decimal> Salary { get; set; }
        public Nullable<System.DateTime> HireDate { get; set; }
        public string Email { get; set; }
     }

}
Create.cshtml
@model Demo.Models.EmployeeDetail
@{
    ViewBag.Title = "Create";   
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Employee", FormMethod.Post, new {id="createform" }))
{
    @Html.ValidationSummary(true)
 <fieldset>
        <legend>Employee Detail</legend>
   <table>
           <tr><td>@Html.LabelFor(model => model.FirstName)</td>
               <td>  @Html.EditorFor(model => model.FirstName)
             @Html.ValidationMessageFor(model => model.FirstName)</td>
           </tr>
           <tr>
               <td> @Html.LabelFor(model => model.LastName)</td>
               <td>@Html.EditorFor(model => model.LastName)
               @Html.ValidationMessageFor(model => model.LastName)</td>
           </tr>
            <tr>
               <td>@Html.LabelFor(model => model.Gender)</td>
               <td>
             @Html.RadioButton("Gender", "male")Male
             @Html.RadioButton("Gender", "female")Female
             @Html.ValidationMessageFor(model => model.Gender)
               </td>
           </tr>
          <tr><td>@Html.LabelFor(model => model.City)</td><td>              @Html.DropDownList("City", new List<SelectListItem>
            {
              new SelectListItem { Text ="Hyderabad",Value="Hyderabad"},
              new SelectListItem { Text = "Amaravati",Value="Amaravati"}
            },"-------------Select----------")
            @Html.ValidationMessageFor(model => model.City)
            @Html.ValidationMessageFor(model => model.City)</td></tr>
            <tr><td>@Html.LabelFor(model => model.State)</td><td>  @Html.DropDownList("State", new List<SelectListItem>
            {
              new SelectListItem { Text ="Telangana",Value="Telangana"},
              new SelectListItem { Text ="Andhra Pradesh",Value="Andhra Pradesh"},
            },"-----------Select------------")
            @Html.ValidationMessageFor(model => model.State)
            @Html.ValidationMessageFor(model => model.State)</td></tr>
            <tr><td> @Html.LabelFor(model => model.Address)</td><td> @Html.TextAreaFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)</td></tr>
            <tr><td> @Html.LabelFor(model => model.Languages)</td><td>
                @Html.CheckBox("Lang", "english") English
                 @Html.CheckBox("Lang", "hindi") Hindi
                 @Html.CheckBox("Lang", "telugu") Telugu
                 
            @Html.ValidationMessageFor(model => model.Languages)</td></tr>
            <tr><td> @Html.LabelFor(model => model.Salary)</td><td> @Html.EditorFor(model => model.Salary)
            @Html.ValidationMessageFor(model => model.Salary)</td></tr>
           <tr><td>
                @Html.LabelFor(model => model.HireDate)
               </td>
               <td>@Html.TextBoxFor(m => m.HireDate, "{0:MM/dd/yyyy}", new { @class = "datepicker" })
                   @Html.ValidationMessageFor(model => model.HireDate) </td>
           </tr>
        @*   <tr><td> @Html.LabelFor(model => model.MobileNo)</td>
               <td> @Html.EditorFor(model => model.MobileNo)
                  @Html.ValidationMessageFor(model => model.MobileNo)</td>
           </tr>*@
            <tr><td>@Html.LabelFor(model => model.Email)</td>
               <td> @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)</td>
           </tr>
     </table>
          <p>
            <input type="reset" value="Reset" />
            <input type="button" value="Clear" id="clearform" />
        </p>
    </fieldset>
}

<script>
    $(document).ready(function () {
         $('#clearform').on('click', function () {
            $('#createform').find('input:text, input:password, select, textarea').val('');
            $('#createform').find('input:radio, input:checkbox').prop('checked', false);
        });
          $('.datepicker').datepicker();
       })
    </script>



Note: Add these scripts in Layout
    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
   <script src="~/Scripts/jquery-ui-1.8.20.js"></script>

   <link href="~/Content/jquery-ui.css" rel="stylesheet" />

Note : Here The reset function does the same if we write
    input type="reset" in the form and clicked it.
It will reset the values in all fields in the form to the value they had when the page loaded.