November 28, 2011

Coding in server side for uploading Photo


protected void imgbtnAddPhoto_Click(object sender, ImageClickEventArgs e)

{

try

{

if (flpAddPhoto.PostedFile != null && flpAddPhoto.HasFile)

{





System.IO.FileInfo inf = new FileInfo(flpAddPhoto.FileName);

string Extension = inf.Extension.ToString().ToLower();

if (Extension == ".jpg" || Extension == ".jpeg" || Extension == ".png" || Extension == ".bmp" || Extension == ".gif")

{

double length = (flpAddPhoto.PostedFile.InputStream.Length) / 1024;

if (length <= 51200)

{

string fileName = System.IO.Path.GetFileName(flpAddPhoto.PostedFile.FileName);

if (fileName != "")

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

string _fileTime = DateTime.Now.ToFileTime().ToString();

string _fileorgPath = System.IO.Path.GetFullPath

(flpAddPhoto.PostedFile.FileName);

string _newfilePath = _fileTime + "~" + fileName;

string tempFolder = Environment.GetEnvironmentVariable("TEMP");

string _filepath = tempFolder + _newfilePath;

flpAddPhoto.PostedFile.SaveAs(_filepath);

Stream fStream = flpAddPhoto.PostedFile.InputStream;

byte[] Contents = new byte[fStream.Length];

fStream.Read(Contents, 0, (int)fStream.Length);

imgPhoto.ImageUrl = _filepath;

if (insert(fileName, _filepath))

{

}

ViewState["PhotoContent"] = Contents;

ViewState["FilePath"] = _filepath;

ViewState["Filename"] = fileName;

fStream.Close();

fStream.Dispose();

});

}

}

}

}

}

catch (Exception ex)

{

ErrorLog.Add(Convert.ToString(ex.Message), Convert.ToString(ex.StackTrace));

Response.Redirect("/_catatlogs/masterpage/Error.aspx");

}

}

Expiry date validation






function ExpDateChk(sender, args)
{

       
var Today = new Date();

var expDate = new Date(document.getElementById("<%# txtLicenceExpiryDate.ClientID %>").value)

if (Today.setDate(Today.getDate()+6) > expDate) {

args.IsValid = false;

}

else {

args.IsValid = true;

}

}

Combining more than one Checboxes using string builder



public string GetMuscleGravity()

{
StringBuilder sb = new StringBuilder();


            if (chkbxLU.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkbxLU).ToString();

sb.Append(",");

}

if (chkbxRU.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkbxRU).ToString();

sb.Append(",");

}

if (chkLH.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkLH).ToString();

sb.Append(",");

}

if (chkbxRH.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkbxRH).ToString();

sb.Append(",");

}

if (sb == null)

{

sb.Append(0).ToString();

}

return sb.ToString();

}