Thursday, October 16, 2014

Send Email using ASP.NET with C# with SMTP gmail Server

  • Create new ASP.NET website with simple design as follows 
    • Four Lables : lblName, lblEmail, lblKey, lblComment
    • Four Textboxes : txtName,  txtEmail, txtKey and txtComment
    • A button : btnSubmit
        The design would look as below. (In this design  TextBoxWatermarkExtender is used )



  • Add a class 'clsSend_Email' to your website code as follows

using System.Net.Mail;

public class clsSend_Email
{
     public clsSend_Email()
     {
     }

    public string Send_Email(string ToMail,string fromMail,string  
    SubjectMail,string BodyMail)
    {
        try
        {

            MailMessage mmSubmit = new MailMessage();
            mmSubmit.To.Add(ToMail);

            MailAddress maFrom = new MailAddress(fromMail);
            mmSubmit.From = maFrom;
           
            mmSubmit.Subject = SubjectMail;
            mmSubmit.Priority = MailPriority.Normal;
            mmSubmit.Body = BodyMail;
           
            SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
            //we will send email using gmail smtp server

            sc.Credentials = new System.Net.NetworkCredential("loginid",             "password");
                       
            //   loginid and password would be login id and password
            //   for your gmail account (from Email)
            sc.EnableSsl = true;
            sc.Send(mmSubmit); 
            return string.Empty;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
   
    }
}




  • On button click event of submit button, call Send_Email method from class clsSend_Email
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Send Email
        try
        {
            clsSend_Email email = new clsSend_Email();
            string Subject="New Email from "+txtName.Text+" regarding "+txtKey.Text.Trim()+
            EmailID : "+txtEmail.Text.Trim();
            string mailStatus= email.Send_Email("From Email ID here ",                
            txtEmail.Text.Trim(),Subject,txtComment.Text.Trim());

            if (mailStatus == string.Empty)
            {
                lblErrMSG.Text = "Thank You for your response!";
            }
            else
            {
                lblErrMSG.Text = mailStatus;
            }
        }
        catch (Exception ex)
        {
           
            lblErrMSG.Text = ex.Message;
        }
       
    }


  • Run and Execute application

Simple Stored Procedure to perform various select operations

Note : Stored Procedure below uses same Users table in my earlier Post .


  • Following stored procedure helps to perform various select operation on Users table (Refer Note).
  • @Function Parameter will help us to decide the search criteria, that is kind of search that we need to perform on our Users table. 
  • We have used one parameter as @Name  instead two different parameters for First_Name and  Last_Name. This way we will check whether entered search @Name field matched with either First_Name  or Last_Name.
  • We have used few simple search criteria in this stored procedure to show SQL Functions : LIKE ,OR,  AND 
  • Various Searches performed are as follows
    • SELECTALL Show All records. 
    • Name First Name or Last Name starts with user entered @Name field.
    • EMail Email contains field @Email
    • NameCity User with First Name or Last Name (@Name) from a specific city field  (@City)


CREATE PROCEDURE [dbo].[SP_User_GetData]
      -- Add the parameters for the stored procedure here
      @Function VARCHAR(100),@Name VARCHAR(100),@Email VARCHAR(100),
      @City VARCHAR(100)
AS
BEGIN
      
      -- SET NOCOUNT ON added to prevent extra result sets FROM
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

      
      -- Insert statements for procedure here
      -- Show all Users : This is default Search, sort by User_ID
      IF @Function = 'SELECTALL'
      BEGIN
            SELECT U.User_ID,U.First_Name,U.Last_Name,U.Email,U.City,U.Phone
            FROM dbo.Users U
            ORDER BY U.User_ID;
      END
     
      
     --Show user with First Name or Last Name search criteria, sorting by First
     -- Name followed by Last Name
      IF @Function='Name'
      BEGIN
            SELECT U.User_ID,U.First_Name,U.Last_Name,U.Email,U.City,U.Phone
            FROM dbo.Users U
            WHERE U.First_Name LIKE '%'+@Name+'%' OR U.Last_Name LIKE       
            '%'+@Name+'%'
            ORDER BY U.First_Name, U.Last_Name;
      END
     
     
      --Show users with Email search criteria, Sorting result with Email
      IF @Function='EMail'
      BEGIN
            SELECT U.User_ID,U.First_Name,U.Last_Name,U.Email,U.City,U.Phone
            FROM dbo.Users U
            WHERE U.Email LIKE '%'+@Email+'%'
            ORDER BY U.Email;
      END
     
      -- Search by Name (First Name / Last Name) and City
      IF @Function='NameCity'
      BEGIN
            SELECT U.User_ID,U.First_Name,U.Last_Name,U.Email,U.City,U.Phone
            FROM dbo.Users U
            WHERE U.City LIKE  @City
            AND (U.First_Name LIKE @Name+'%' OR U.Last_Name LIKE @Name+'%')
            ORDER BY U.User_ID;
      END
     

END


Stored Procedure Execution 

Using various search criteria with @Function parameter mentioned in stored procedure above.

  • Using SELECTALL to show all records in Users table 


DECLARE     @return_value int

EXEC  @return_value = [dbo].[SP_User_GetData]
            @Function = N'SELECTALL',
            @Name = N'  ',
            @Email = N' ',
            @City = N' '

SELECT      'Return Value' = @return_value

GO



  • Using Name to show First Name or Last Name specific records in Users table 
DECLARE     @return_value int

EXEC  @return_value = [dbo].[SP_User_GetData]
            @Function = N'Name',
            @Name = N'P',
            @Email = N' ',
            @City = N' '

SELECT      'Return Value' = @return_value

GO



  • Using EMail to show  Email specific records in Users table 
DECLARE     @return_value int

EXEC  @return_value = [dbo].[SP_User_GetData]
            @Function = N'EMail',
            @Name = N' ',
            @Email = N'xyz',
            @City = N' '

SELECT      'Return Value' = @return_value

GO


  • Using NameCity to show users with either specific First Name or Last Name belonging to specific city records in Users table 
DECLARE     @return_value int

EXEC  @return_value = [dbo].[SP_User_GetData]
            @Function = N'NameCity',
            @Name = N'P',
            @Email = N' ',
            @City = N'City1'

SELECT      'Return Value' = @return_value

GO


Wednesday, October 15, 2014

Simple Stored Procedure to Perform GridView Insert, Update and Delete

Following stored procedure will help to perform Gridview Insert, Update, Delete operation.

Consider we have a table as Users - table structure as follows :




User_ID is a primary key and set Identity = 'Yes' for auto-generate primary key.
  • In Stored procedure below we have a parameter @Function. while using this stored procedure set value of @Function as 'insert' , 'update', 'delete' for respective operation.
  • Update and delete operation required primary key field User_ID
  • GETDATE()  : to get current DateTime is used in insert operation.


CREATE PROCEDURE [dbo].[SP_User_Modify]
      -- Add the parameters for the stored procedure here
      @Function varchar(20) ,@UID int,@FName varchar(100),@LName      
      varchar(100),@Email varchar(100),@City varchar(100),
      @Phone int
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

      -- Insert statements for procedure here

      -- Insert operation
      IF @Function='insert'
      BEGIN
            insert into dbo.Users            
            (First_Name,Last_Name,Email,City,Phone,Active_Date)
            values
            (@FName,@LName,@Email,@City,@Phone,GETDATE())
      END
      
      -- Update operation
      IF @Function='update'
      BEGIN
            update dbo.Users
            set 
            First_Name=@FName,Last_Name=@LName,Email=@Email,City=@City,
            Phone=@Phone
            WHERE User_ID=@UID;
      END
     
      -- Delete operation
      IF @Function='delete'
      BEGIN
            delete from dbo.Users
            WHERE User_ID=@UID;
      END

END