Wednesday, 5 June 2013

Send Mail in Asp.net,C# using Gmail/Yahoo

First method

MailMessage _MailMsg = new MailMessage();

 _MailMsg.From = new MailAddress(FromEmail);
_MailMsg.To.Add(EmailId);
_MailMsg.Subject = Subject;
  _MailMsg.Body = Body;
 _MailMsg.Priority = System.Net.Mail.MailPriority.High;
  _MailMsg.IsBodyHtml = true;
           
  _MailMsg.Attachments.Add(new Attachment(FileAttachment));

                SmtpClient mailSender = new SmtpClient();
                mailSender.Host = SMTP_Host;

                //mailSender.Credentials = new System.Net.NetworkCredential(FromEmail, Password);
                mailSender.Port = 578;//--- used for abc@gmail.com
                mailSender.EnableSsl = true;
                mailSender.Credentials = new System.Net.NetworkCredential(FromEmail, Password);
                mailSender.Timeout = 200000;
                mailSender.Send(_MailMsg);

Second Method

 Codebehnd: .CS


protected void btnsend_Click(object sender, EventArgs e)
    {
        string from = "test@123@yahoo.in"; //Replace this with your own correct Gmail Address

        string to = txtmail.Text; //Replace this with the Email Address to whom you want to send the mail
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from, "Harish", System.Text.Encoding.UTF8);
        mail.Subject = txtsub.Text;
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "your body msg";
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
        

        SmtpClient client = new SmtpClient();
        //Add the Creddentials- use your own email id and password

        client.Credentials = new System.Net.NetworkCredential(from, "password");
        client.Port = 587; // Gmail works on this port 
       // client.Port=465; //YahooMail on this port
       
        client.Host = "smtp.gmail.com";
        //client.Host = "plus.smtp.mail.yahoo.com";//Yahoomail works on this Host Name
        client.EnableSsl = true; //Gmail works on Server Secured Layer
        try
        {
            client.Send(mail);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            lblmessege.Text = errorMessage;
            
        }
        finally
        {
            lblmessege.Visible = true;
            lblmessege.Text = txtname.Text+  " : Your Messege Submitted";
                       

        }


Datatable To Json String when use ajax call in asp.net

public static string DatatableToJson(DataTable objDT, int count = 0, string[] captionArray = null)
    {
        string json = string.Empty;

        if (objDT != null && objDT.Rows.Count > 0)
        {
            string[] ColumnArr = objDT.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
            var LinqResult = objDT.Rows.Cast<DataRow>().ToList();
            List<List<string>> aaList1 = new List<List<string>>();
            foreach (var item in LinqResult)
            {
                List<string> n = new List<string>();
                for (int i = 0; i < ColumnArr.Length; i++)
                {
                    n.Add(item[ColumnArr[i].ToString()].ToString());
                }
                aaList1.Add(n);
            }

            var formatedList = new
            {
                aaData = aaList1,
                aaHeader = ColumnArr,
                aaCount = count,
                iTotalDisplayRecords = count,
                aaCaption = captionArray
            };

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            json = serializer.Serialize(formatedList);
        }
        else
        {
            json = "NO_RECORD";
        }
        return json;
    }

Tuesday, 4 June 2013

Timer in asp.net C#

Use below code in codebehind or in global.ashx for send mail or do any other logic on time bases.


System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Interval = TimeSpan.FromHours(1).TotalMilliseconds;
myTimer.AutoReset = true;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(MailTimer);
myTimer.Enabled = true;


public void MailTimer(object source, System.Timers.ElapsedEventArgs e)
{
//Use logic
}

             System.Timers.Timer UnTimer = new System.Timers.Timer();
            UnTimer.Interval = TimeSpan.FromMinutes(15).TotalMilliseconds;
            UnTimer.AutoReset = false;
            UnTimer.Elapsed += delegate { MailTimer1(s1, s2); };
            UnTimer.Enabled = true;
           public void MailTimer1(string s1, string s2)
        {
//logic
                   }

Javascript Number validation on keypress

function validInt(event) { if (event.which != 43 && event.which != 0 && event.which != 8 && (event.which <= 47 || event.which >= 59)) event.preventDefault(); }

Sunday, 2 June 2013

Bind Asp.net Gridview with DataReader in C#

// This method is used to bind gridview from database

protected void BindGridview()
{
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
gvUserInfo.DataSource = dr;
gvUserInfo.DataBind();
con.Close();
}
}

SQL Query to Find Text of storeprocedure/function

-- Get the schema name, table name, and table type for:

-- Table names
SELECT
       TABLE_SCHEMA  AS 'Object Schema'
      ,TABLE_NAME    AS 'Object Name'
      ,TABLE_TYPE    AS 'Object Type'
      ,'Table Name'  AS 'TEXT Location'
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%'+@Text+'%'
UNION
 --Column names
SELECT
      TABLE_SCHEMA   AS 'Object Schema'
      ,COLUMN_NAME   AS 'Object Name'
      ,'COLUMN'      AS 'Object Type'
      ,'Column Name' AS 'TEXT Location'
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%'+@Text+'%'
UNION
-- Function or procedure bodies
SELECT
      SPECIFIC_SCHEMA     AS 'Object Schema'
      ,ROUTINE_NAME       AS 'Object Name'
      ,ROUTINE_TYPE       AS 'Object Type'
      ,ROUTINE_DEFINITION AS 'TEXT Location'
FROM  INFORMATION_SCHEMA.ROUTINES 
WHERE ROUTINE_DEFINITION LIKE '%'+@Text+'%'
      AND (ROUTINE_TYPE = 'function' OR ROUTINE_TYPE = 'procedure');
 

Step By Step Installation of SQL Server 2008

http://www.blog.sqlauthority.com/2008/06/12/sql-server-2008-step-by-step-installation-guide-with-images/