Wednesday, January 23, 2013

Convert Datareader to Datatable in asp.net



using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    IDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(string));
        dt.Columns.Add("Address", typeof(string));
        DataRow drQuery;
      
      
        da = getReader();

        da.Fill(ds);
        int QCount1 = 0;
        while (QCount1 < ds.Tables[0].Rows.Count)
        {
            drQuery = dt.NewRow();

            drQuery["ID"] = Convert.ToString(ds.Tables[0].Rows[QCount1][1].ToString());
            drQuery["Name"] = ds.Tables[0].Rows[QCount1][3].ToString();
            drQuery["Age"] = ds.Tables[0].Rows[QCount1][2].ToString();
            drQuery["Address"] = ds.Tables[0].Rows[QCount1][9].ToString();
            dt.Rows.Add(drQuery);
            QCount1 = QCount1 + 1;
        }
      
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    private IDataAdapter getReader()
    {
     
    
        SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["testcon"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select * from t_users",con);
      


        return da;
    }
}



Thursday, January 17, 2013

Cross-Site Scripting Protection in ASP.NET(A potentially dangerous Request.Form value was detected from the client)

This is basically to  catches malicious scripting code used by hacker.This is simply done in ASP.net by using a filter....
We need to add this in webconfig file as: 



<configuration>
    <system.web>
        <httpModules>
            <add name="ValidateInput" 
               type="Corillian.Web.ValidateInput,ValidateInputASPNET10" />
        </httpModules>
    </system.web>
</configuration>




For more reference use this link
http://msdn.microsoft.com/en-us/library/ms972967.aspx


Tuesday, January 15, 2013

How to Enable TLS for SMTP Authentication ?

I have made a function for sending email and inside here smtp client object is created and set TLS for it



public Boolean SendMailNew(string filename, bool isAttach)
        {
            mailSent = false;
            Boolean IsSuccess = false;
            try
            {

                MailAddress senderAddress = new MailAddress(fromAddress, fromWhom);
                MailMessage message = new MailMessage();
                string messageString = "";
                message.From = senderAddress;
                string[] toAddressArray = ToAddressArray(toAddress);
                foreach (string recAddress in toAddressArray)
                {
                    message.To.Add(new MailAddress(recAddress, recAddress));
                }

                if (ccAddress != null && ccAddress.Trim().Length > 0)
                {
                    string[] ccAd = ToAddressArray(ccAddress);
                    foreach (string ccAddStr in ccAd)
                    {
                        MailAddress ccAddr = new MailAddress(ccAddStr);
                        message.CC.Add(ccAddr);
                    }
                }

                if (bccAddress != null && bccAddress.Trim().Length > 0)
                {
                    string[] bccAd = ToAddressArray(bccAddress);
                    foreach (string bccAddStr in bccAd)
                    {
                        MailAddress bcc = new MailAddress(bccAddStr);
                        message.Bcc.Add(bcc);
                    }
                }

                string path = ConfigurationSettings.AppSettings["ImagePath"].ToString();

                LinkedResource lnkResource1 = new LinkedResource(path + "header1.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource1.ContentId = "headerPict1";
                LinkedResource lnkResource2 = new LinkedResource(path + "dotted.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource2.ContentId = "dottedPict1";
                LinkedResource lnkResource3 = new LinkedResource(path + "footer.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource3.ContentId = "footerPict1";
                LinkedResource lnkResource4 = new LinkedResource(path + "f.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource4.ContentId = "fPict1";
                LinkedResource lnkResource5 = new LinkedResource(path + "t.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource5.ContentId = "tPict1";
                LinkedResource lnkResource6 = new LinkedResource(path + "yt.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource6.ContentId = "ytPict1";
                LinkedResource lnkResource7 = new LinkedResource(path + "l.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource7.ContentId = "lPict1";
                LinkedResource lnkResource8 = new LinkedResource(path + "robot.jpg", MediaTypeNames.Image.Jpeg);
                lnkResource8.ContentId = "RobotPict1";

                ContentType ct = new ContentType();
                ct.MediaType = MediaTypeNames.Text.Html;
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(messageBody, ct);

                htmlView.LinkedResources.Add(lnkResource1);
                htmlView.LinkedResources.Add(lnkResource2);
                htmlView.LinkedResources.Add(lnkResource3);
                htmlView.LinkedResources.Add(lnkResource4);

                htmlView.LinkedResources.Add(lnkResource5);
                htmlView.LinkedResources.Add(lnkResource6);
                htmlView.LinkedResources.Add(lnkResource7);
                htmlView.LinkedResources.Add(lnkResource8);
                message.AlternateViews.Add(htmlView);

                messageString = messageBody;
                message.Subject = subjectLine;

                if (isAttach)
                {
                    Attachment attach = new Attachment(filename);
                    // Add time stamp information for the file.
                    ContentDisposition disposition = attach.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(filename);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(filename);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(filename);
                    // Add the file attachment to this e-mail message.
                    message.Attachments.Add(attach);
                }
               

                message.IsBodyHtml = true;
                message.Body = messageString;
             
                //NetworkCredential loginInfo = new NetworkCredential(ConfigurationSettings.AppSettings["UserId"].ToString(), ConfigurationSettings.AppSettings["Password"].ToString());
                //SmtpClient client = new SmtpClient("pod51014.outlook.com", 587);
               
                //client.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["SMTPport"]);
                //client.EnableSsl = true;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                //client.UseDefaultCredentials = false;
                //client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //client.Credentials = loginInfo;

                SmtpClient client = new SmtpClient(smtpClientString);
                client.EnableSsl = true;
               //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

                client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


                client.Send(message);
                message.Attachments.Clear();
                message.Dispose();

                return (IsSuccess = true);

            }
            catch (Exception exc)
            {
                exc.ToString();
                return IsSuccess;
                //throw exc;
            }
        }


======================

Add the setting in webconfig file,Here from adress address must be same as user name

<system.net>
<mailSettings>
<smtp from="abc@test.net" deliveryMethod="Network">
<network userName="abc@test.net" password="5ama" host="po.outlook.com" port="587" />
</smtp>
</mailSettings>
</system.net>