Wednesday, September 26, 2007

"Remember User Login" using ASP.NET,C#.NET

// Write this code in the Login button click
// Store the user's username and password in a cookie when checkbox Remember Me checked
if (chkRememberLogin.Checked == true)
{
HttpCookie myCookie = new HttpCookie("myCookie");
Response.Cookies.Remove("myCookie");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("UserName", this.txtUserID.Text.ToString());
myCookie.Values.Add("PassWord", this.txtPassword.Text.ToString());
DateTime dtExpiry = DateTime.Now.AddMonths(1); //you can add years and months too here Response.Cookies["myCookie"].Expires = dtExpiry;
}

// Write this code in the Form Load()
if (!IsPostBack)
{

if (Request.Cookies["myCookie"] != null)
{
HttpCookie myCookie = Request.Cookies.Get("myCookie");
this.txtUserID.Text = myCookie.Values["UserName"].ToString();
this.txtPassword.Attributes.Add("Value", myCookie.Values["PassWord"].ToString());
}
}

Tuesday, September 25, 2007

Select Distinct columns from a DataTable

public static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
{
object[] lastValues;
DataTable newTable;
DataRow[] orderedRows;
if (FieldNames == null FieldNames.Length == 0)
throw new ArgumentNullException("FieldNames");
lastValues = new object[FieldNames.Length];
newTable = new DataTable();
foreach (string fieldName in FieldNames)
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));
foreach (DataRow row in orderedRows)
{
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
{
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
setLastValues(lastValues, row, FieldNames);
}
}
return newTable;
}

Friday, September 21, 2007

Programatically highlight a row in Telerik RadGridView [ WinForms]

There are two ways to programmatically highlight a row:

Use the Row's IsCurrent property:

this.radGridView1.Rows[RowIndex].IsCurrent = true;

Use the GridViewInfo's CurrentRow property:

this.radGridView1.MasterGridViewInfo.CurrentRow = this.radGridView1.Rows[RowIndex];

Tuesday, September 18, 2007

Validation in the grid cells

//Validating the Order column [Only numeric values allowed]
private void DataGrid1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int newinteger;
if (e.ColumnIndex == 4)
{
bool Check = int.TryParse(e.FormattedValue.ToString(), out newinteger);
if (Check == false && e.FormattedValue.ToString() != "")
{
MessageBox.Show("Enter numeric value");
e.Cancel = true;
}
}
}