Tuesday, May 8, 2012

JavaScript for Clientside validation for asp.net Single-CheckBox using CustomValidator

<script  language="javascript" type ="text/javascript" >
function checkAgreement(source, args)
{
var elem = document.getElementById("cbxPDCheque");
if (elem.checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
 
<asp:CheckBox ID="cbxPDCheque" runat="server" AutoPostBack="True" Text="PD Cheques" />
<asp:CustomValidator ID="CustomValidator1" runat="server" EnableClientScript ="true"
ErrorMessage="Select PD Cheques." ClientValidationFunction ="checkAgreement" >*</asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" ShowSummary="False" />
<asp:Button ID="Button1" runat="server" Text="Button" />
 
http://www.codeproject.com/Questions/188889/JavaScript-for-Clientside-validation-for-asp-net-S  

Thursday, May 3, 2012

Export DataTable to Excel File in asp.net

dt = city.GetAllCity();//your datatable 
string attachment = "attachment; filename=city.xls"; 
 Response.ClearContent(); Response.AddHeader("content-disposition", attachment);
  Response.ContentType = "application/vnd.ms-excel"; string tab = ""; 
 foreach (DataColumn dc in dt.Columns) { 
   
Response.Write(tab + dc.ColumnName);
    tab
= "\t"; } Response.Write("\n"); int i; foreach (DataRow dr in dt.Rows) {
    tab
= "";
   
for (i = 0; i < dt.Columns.Count; i++)
   
{
       
Response.Write(tab + dr[i].ToString());
        tab
= "\t";
   
}
   
Response.Write("\n"); } Response.End(); 
 
 
http://stackoverflow.com/questions/7843822/export-datatable-to-excel-file-in-asp-net 

Adding Facebook Share Functionality to an ASP.NET Website

Adding Facebook Share Functionality to an ASP.NET Website
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title>Facebook share sampel</title>
    <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"
           type="text/javascript">
</script>
</head>

<body>

<form id="Form1" runat="server">

<div>

 <a name="sharebutton" type="button" href="http://www.facebook.com/sharer.php">Share</a>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</div>

</form>

</body>

</html>





Aspx.cs



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Sharefacebook : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "<a name=\"fb_share\" type=\"button\"></a>" +
"<script " +
"src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" " +
"type=\"text/javascript\"></script>";
HtmlMeta tag = new HtmlMeta();
tag.Name = "title";
tag.Content = "This is the page title";
Page.Header.Controls.Add(tag);
HtmlMeta tag1 = new HtmlMeta();
tag.Name = "description";
tag.Content = "This is a page description.";
Page.Header.Controls.Add(tag1);
}
} 

OR
 
 
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

namespace MHT_Web_Site
{
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
labelSteps_1_2.Text = "<a name=\"fb_share\" type=\"button\"></a>" +
"<script " +
"src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" " +
"type=\"text/javascript\"></script>";

HtmlMeta tag = new HtmlMeta();
tag.Name = "title";
tag.Content = “This is the Title”;
Page.Header.Controls.Add(tag);

HtmlMeta tag = new HtmlMeta();
tag.Name = "description";
tag.Content = “This is a short summary of the page.”;
Page.Header.Controls.Add(tag);

HtmlLink link = new HtmlLink();
link.Href = “http://www.murrayhilltech.com/images/LogoColorNoText.jpg”;
link.Attributes["rel"] = "image_src";
Page.Header.Controls.Add(link);

}
catch (Exception ex)
{
// Handle the exception
}
}
}
}

Wednesday, May 2, 2012

Encrypting Configuration Information in ASP.NET 2.0 Applications

When creating ASP.NET 2.0 applications, developers commonly store sensitive configuration information in the Web.configfile. The cannonical example is database connection strings, but other sensitive information included in the Web.configfile can include SMTP server connection information and user credentials, among others. While ASP.NET is configured, by default, to reject all HTTP requests to resources with the .config extension, the sensitive information in Web.configcan be compromised if a hacker obtains access to your web server's file system. For example, perhaps you forgot to disallow anonymous FTP access to your website, thereby allowing a hacker to simply FTP in and download your Web.config file. Eep.
Fortunately ASP.NET 2.0 helps mitigate this problem by allowing selective portions of the Web.config file to be encrypted, such as the <connectionStrings> section, or some custom config section used by your application. Configuration sections can be easily encrypted using code or aspnet_regiis.exe, a command-line program. Once encrypted, the Web.config settings are safe from prying eyes. Furthermore, when retrieving encrypted congifuration settings programmatically in your ASP.NET pages, ASP.NET will automatically decrypt the encrypted sections its reading. In short, once the configuration information in encrypted, you don't need to write any further code or take any further action to use that encrypted data in your application.
In this article we'll see how to programmatically encrypt and decrypt portions of the configuration settings and look at using the aspnet_regiis.exe command-line program. We'll then evaluate the encryption options ASP.NET 2.0 offers. There's also a short discussion on how to encrypt configuration information in ASP.NET version 1.x. Read on to learn more!
http://www.4guysfromrolla.com/articles/021506-1.aspx

Save username and password in cookies in asp.net

My web application's home page has a RememberMe checkbox.. If the user checks it, i ll store emailId and password in cookies.. My code is::::


if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
   
{
     
HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text);
     cookie
.Expires.AddYears(1);
     
Response.Cookies.Add(cookie);
   
}

Tuesday, April 24, 2012

online shopping

Suteki Shop is an eCommerce application. The orginal aim is to write a site for a fashion retail business. It includes a product catalogue, shopping cart and order processing.
Email Mike Hadlow, mike@suteki.co.uk, with any problems or suggestions.
It's based on the following technologies:
  • .NET 4.0
  • ASP.NET MVC 3
  • MVC Contrib
  • NHibernate
  • Windsor IoC Container
Built using TDD with the following tools:
for further detail plz visit:
http://code.google.com/p/sutekishop/

Friday, April 20, 2012

how to Encript and decript values

public  string Encrypt(string toEncrypt, bool useHashing)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        System.Configuration.AppSettingsReader settingsReader =
                                            new AppSettingsReader();
        // Get the key from config file

        string key = (string)settingsReader.GetValue("SiteName",
                                                         typeof(String));
        //System.Windows.Forms.MessageBox.Show(key);
        //If hashing use get hashcode regards to your key
        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //Always release the resources and flush data
            // of the Cryptographic service provide. Best Practice

            hashmd5.Clear();
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)

        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        //transform the specified region of bytes array to resultArray
        byte[] resultArray =
          cTransform.TransformFinalBlock(toEncryptArray, 0,
          toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Clear();
        //Return the encrypted data into unreadable string format
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

    public  string Decrypt(string cipherString, bool useHashing)
    {
        byte[] keyArray;
        //get the byte code of the string

        byte[] toEncryptArray = Convert.FromBase64String(cipherString);
        //byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(cipherString);

        System.Configuration.AppSettingsReader settingsReader =
                                            new AppSettingsReader();
        //Get your key from config file to open the lock!
        string key = (string)settingsReader.GetValue("SiteName",
                                                     typeof(String));

        if (useHashing)
        {
            //if hashing was used get the hash code with regards to your key
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //release any resource held by the MD5CryptoServiceProvider

            hashmd5.Clear();
        }
        else
        {
            //if hashing was not implemented get the byte code of the key
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
        }

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)

        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(
                             toEncryptArray, 0, toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor               
        tdes.Clear();
        //return the Clear decrypted TEXT
        return UTF8Encoding.UTF8.GetString(resultArray);
    }