Tuesday, May 25, 2010

Encrypt / Decrypt Password

hi
In asp.net the FormsAuthentication class provides an Encrypt method to create a string value that can be stored in a cookie or in the URL from a FormsAuthenticationTicket. The FormsAuthentication class also provides a Decrypt method to create a FormsAuthenticationTicket object from the encrypted authentication ticket retrieved from the forms-authentication cookie or the URL.

this is the sample code to create cookie with encrypt data

create ticket
FormsAuthenticationTicket t = new FormsAuthenticationTicket(1,username,Datetime.Now,Datetime.Now.Adddays(1),true, userdata,FormsAuthentication.FormsCookiePath );
1-- version
Datetime.Now -- created Date
Datetime.Now -- expireDate
true -- isPersistent cookie
userdata -- which data want encrypt
FormsAuthentication.FormsCookiePath -- path of the cookie

//encrypt method

string Encrypt = FormsAuthentication.Encrypt(t);
response.cookie.add(new Httpcookie(nameofcookie,Encrypt));


Sample code for Decrypt
if(request.cookie[nameofcookie] != null)
{
var cookie_ = request.cookie[nameofcookie];
FormsAuthenticationTicket t = FormsAuthentication.Decrypt(cookie_.value);
string s = t.userdata;

}

Tuesday, August 25, 2009

Row Click Event in Grid View

Hi actually there is no row click event in gridview for this one using javascript we can solve this one.

step one
--------

In grid view row databound event
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow tr = e.Row;
tr.Attributes.Add("onClick", "javascript:selectMe(this);");
tr.Attributes.Add("style", "cursor:hand;");

}
}
catch
{
}
}

in this one we r adding javascript event to gridview row. same process we can do in another way also if you know jquery using jquery also we can add attribute to every row in gridview

now defining javascript function selectMe(obj).actually i'm getting the all columns values and alerting all values.in my gridview i have 3 columns for every row
script type="text/javascript"
fun ction selectMe(obj)
{
var name = obj.cells[0].firstChild.data;
var email = obj.cells[1].firstChild.data;
var number = obj.cells[2].firstChild.data;
alert("name =" + name + ",Email=" + email +",Number=" +number);
}
/script

Friday, February 6, 2009

Deleting selected rows from datagrid view Dynamically

This is the sample code for deleting selected rows from datagridview
using c#

private void Form1_Load(object sender, EventArgs e)
{
string constr = "Server = BUS-6A4CE2BF757; Database= master;Integrated Security = True;";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select * from emptable", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
// dataGridView1.DataBind();

}

private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
{
dataGridView1.Rows.Remove(dr);
}

}

Wednesday, January 28, 2009

Displaying Images in DataList

Hi in this program i'm displaying images from a folder.I'm fetching Images from folder and place in datalist,Here i'm using generics to store filenames & datasource for datalist.when user press buton it will display next Image from Folder.




using System;
using System.Data;
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;
using System.IO;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
static int i = 0;
static int[] a= new int[6];
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
a[i] = i;
DataList1.DataSource = GetPhotos(i);
DataList1.DataBind();
i++;

}

}
// This Method Returns The value generics type list of strings
public List GetPhotos(int i1)
{
List photos = new List();
String photopath = Server.MapPath("~/uploadimages");
string[] f1 = Directory.GetFiles(photopath);
photos.Add(f1[i1]);

return photos;
}
// This is the button event showing Next Image in this folder it contain 6 Images
// only .Other wise u can show msg when it 6
protected void Button1_Click(object sender, EventArgs e)
{
if (i >= 6)
{
i = 0;
}

DataList1.DataSource = GetPhotos(i);
DataList1.DataBind();
i++;
}



}

/================================================================

Tuesday, September 9, 2008

Arrays and ArrayLists

What is an Array?
Array is a group of similar data types so we can simply say Array's are homogeneous data structures. Array can store the elements in contiguous memory locations.We can access elements in array by index.
using array's
. we can easily store the data
. we can easily sort the data
. we can re size the array
Types of Array's:
. One dimensional array
int[] abc ;
int[] abc = new int[10];
. Multi dimensional array
int[,] abc;
int[,] abc = new int[2,3];
. Jagged array
arrays of arrays is called jagged arrays.
string[][] abc = new string[2][];

Disadvantage of an array's:
consider one array with 100 elements. suppose we want search an element of 99th it visit every element while searching so performance wise it is not good. To overcome arrays MS developed collections.
-------------------------------------------------------------------------------------------------
ArrayLists
ArrayList same as standard array. It supports dynamic array,which changes it's size as required. array can store only one type elements but ArrayList can store different types of object so ArrayLists are heterogeneous data structure. In ArrayList it re size allocation memory automatically when storing reach the array limits it double the size of an array. We can fix the size by assigning value to capacity parameter.

ArrayList can store collection of objects. It means it can store everything as a object. This is known as Boxing. In case of retrieving a value from this you need to cast the element of the object array to original type.This is known as unboxing.

Disadvantage:
sorting is not possible.

About C#

C# (pronounced C Sharp) is an object-oriented programming language developed by Microsoft as part of the .NET initiative and later approved as a standard by ECMA (ECMA-334) and ISO(ISO/IEC 23270). Anders Hejlsberg ,the designer of Delphi, leads development of the C# language, which has an object-oriented syntax based on C++ and includes influences from aspects of several other programming languages (most notably Delphi and Java) with a particular emphasis on simplification.
HISTORY:
During the development of .NET, the class libraries were originally written in a anguage/compiler called Simple Managed C (SMC). In January 1999, Anders Hejlsberg formed a team to build a new language at the time called Cool.By the time the .NET project was publicly announced at the July 2000 Professional Developers Conference(PDC), the language had been renamed C# and the class libraries and ASP.NET runtime had been ported to C#.
C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg, who was previously involved with the design of Visual J++, Borland Delphi, and Turbo Pascal. In interviews and technical papers he has stated that flaws in most major programming languages (e.g. C++, Java, DelpHi, and Smalltalk) drove the fundamentals of the Common Language Runtime (CLR), which, in turn, drove the design of the C# programming language itself. Some argue that C# shares roots in other languages.