Friday, October 31, 2008

Validate Image Size using CustomValidator Control

Try this code in server side of CustomValidator

protected void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
if (IsValid)
{
int maxSize = 5 * 1024 * 1024;
if (FileUpload1.PostedFile.ContentLength > maxSize)
{
args.IsValid = false;
}
}
}

Good Luck

Get Yesterday Date

Try this code:

DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy");

Good Luck

Wednesday, October 29, 2008

Automatically change text to Uppercase

Try this with textbox in page Load

TextBox1.Attributes.Add("onblur", "this.value=this.value.toUpperCase();");

Good Luck

Change User Email using Membership

Try this:

Dim user As MembershipUser user = Membership.GetUser(UserName)
user.Email = TxtBxNewEmail.Text
Membership.UpdateUser(user)

-- But check for email availability:

public bool CheckEmail()
{
bool emailExsit = false;
MembershipUserCollection users = Membership.FindUsersByEmail(Email.Text);
foreach (MembershipUser user in users)
{
if (Email.Text == user.Email)
{
emailExsit = true;
break;
}
else
{
emailExsit = false;
}
}
return emailExsit;
}

Good Luck

Count Number of record in Table

Try this

public int Count_Company()
{
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand comm = new SqlCommand();
comm.CommandText = "Select count(UserName) from CompanyData"; comm.CommandType = CommandType.Text;
comm.Connection = conn;
conn.Open();
rows = int.Parse(comm.ExecuteScalar().ToString());
conn.Close();
return rows;
}

Good Luck

Show and Hide Hyperlink in LoginView according to Role of Login user

Try this:


protected void Page_Load(object sender, EventArgs e)
{
//-- show Admin Page Link in Login View for Admin user
if (Request.IsAuthenticated)
{
HyperLink lnk_Admin = (HyperLink)LoginView1.FindControl("lnk_Admin");
string userName=Membership.GetUser().UserName;
if (Roles.IsUserInRole(userName, "Admin"))
{
lnk_Admin.Visible = true;
}
else
{
lnk_Admin.Visible = false;
}
}

Good Luck

Get current page name

Try this:

public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath; System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}

Good Luck

Find Control in Gridview and DataList

Try this :

//--- DataList
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lbl_Desc = (Label)e.Item.FindControl("lbl_Desc");
lbl_Desc.Text = lbl_Desc.Text.Substring(0, 180).ToString() + "...";
}
}

//--- Gridview
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

//-- find control in row
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl_Type= (DropDownList)e.Row.FindControl("ddl_Type");
}

//-- find control in footer
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddl_Type = (DropDownList)e.Row.FindControl("ddl_Type");
}
}

Good Luck

Select top and last five record from Database

In Query try this:

FOR TOP 5
select top 5 * from table

//-------------------

FOR LAST 5
select top 5 * from table order by primarykey_columnname desc

Good Luck

Validate URL

Try this with Regular Expression Validator

^((htf)tp(s?)\:\/\/~//)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

Good Luck

Validate Fileupload to accept JPG image

Use this with Regular Expression Validator

ValidationExpression="^(([a-zA-Z]:)(\\{2}\w+)\$?)(\\(\w[\w](.)*))+(\.jpg\.JPG)$"

or

ValidationExpression="^(([a-zA-Z]:)(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg.JPG)$"

or

ValidationExpression="^.+\.(([jJ][pP][eE]?[gG])([gG][iI][fF]))$"

Good Luck

Preview Default image in Gridview

Try this Code:

protected void grdtest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image imgCarPic = (Image)e.Row.FindControl("imgCarPic");
if (string.IsNullOrEmpty(imgCarPic.ImageUrl.ToString()))
{
imgCarPic.ImageUrl = "../images/NoImageSmall.jpg";
}
else
{
String strUploadedPicPath = Server.MapPath(imgCarPic.ImageUrl.ToString());
if (!System.IO.File.Exists(strUploadedPicPath))
{
imgCarPic.ImageUrl = "../images/NoImageSmall.jpg";
}
}
}
}

Good Luck

Prevent Multiple Login at the same time

Try this:
protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
e.Cancel = Membership.GetUser(Login1.UserName).IsOnline;
}

or try this code

void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
MembershipUser u = Membership.GetUser(Login1.UserName); Response.Write(u.IsOnline);
if (u.IsOnline)
{
Login1.FailureText = "A user with this username is already logged in."; e.Cancel = true;
}
else
{
Login1.FailureText = String.Empty;
e.Cancel = false;
}
}

ASP.NET Regular Expression for date format

In ASP.Net Many times required to Validate Date Fromat Enter in Control.in Regular Expression Following RE used to Validate Date Format.

dd/mm/yyyy Format (optional mm,optional dd)
([1-9]0[1-9][12][0-9]3[01])[- /.]([1-9]0[1-9]1[012])[- /.][0-9]{4}$

mm/dd/yyyy (optional mm,optional dd)
^([1-9]0[1-9]1[012])[- /.]([1-9]0[1-9][12][0-9]3[01])[- /.][0-9]{4}$

mm/dd/yyyy (Exact Format)
^([01]\d)/([0-3]\d)/(\d{4})$

Good Luck