Thursday, February 26, 2009

Image resizing asp.net c#

Some times you want to resize images as per your required size using asp.net.


using System.Drawing;
using System.Drawing.Drawing2D;

Resizeimage(intheight, intwidth, userPostedFile.FileName.ToString(), userPostedFile.InputStream);


public string Resizeimage(int intHeight, int intWidth, string ImageUrl, System.IO.Stream Buffer)
{
try
{ string filepath = ConfigurationManager.AppSettings["UploadURL"];
string savepath = ConfigurationManager.AppSettings["SaveURL"];
string filename;
string[] imagefilename = new string[2];
char[] split ={ '.' };
imagefilename = ImageUrl.Split(split);
imagefilename[0] = imagefilename[0].Replace(" ", "_") + DateTime.Now.ToFileTime();
filename = (imagefilename[0].ToString() + "." + imagefilename[1].ToString());
System.Drawing.Image image = System.Drawing.Image.FromStream(Buffer);
int srcWidth = image.Width;
int srcHeight = image.Height;
Decimal sizeRatio = ((Decimal)srcHeight / srcWidth);
Bitmap bmp = new Bitmap(intWidth, intHeight);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, intWidth, intHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
bmp.Save(filepath + intHeight.ToString() + System.IO.Path.GetFileName(filename));
string strImageUrl=savepath+intHeight.ToString() + System.IO.Path.GetFileName(filename);
bmp.Dispose();
image.Dispose();
return strImageUrl;
}
catch (Exception ex)
{
// MessageBox("Some Error has occured while uploading the images");
}
return "";
}

W3c error on asp.net pages due to script tag on page?

certain times we put javascript on the page itself. but putting javascript on the page gives w3c validation error.
solution for this is

Generating random number asp.net c#

using System.Security;

protected void Page_Load(object sender, EventArgs e)
{ string abc = "";
for (int i = 0; i < abc =" RandomNumberGenerator(6);" rng =" System.Security.Cryptography.RandomNumberGenerator.Create();" chars =" new" validchars = "abcdefghijklmnopqrstuvwxyzABCEDFGH IJKLMNOPQRSTUVWXYZ1234567890!@$%^*();
for (int i = 0; i < length; i++)
{ byte[] bytes = new byte[1];
rng.GetBytes(bytes);
Random rnd = new Random(bytes[0]);
chars[i] = validChars[rnd.Next(0, 61)];
}
return (new string(chars));
}
}

Anchor Title vs alt tag

Q) Where to use alt and where to use title in anchor tag?

Ans: Its all depends on your requirment.
alt tag is for w3c validation and in IE it will display text on mouse hover.
title tag works in the same way, it will display text on mouse hover in all browser. if you have specified both title and alt tag then on mouse hover titlte tag will be displayed.
one thing to be noted if u dont want mouse hover dont specify title tag but alt tage is necessary as per w3c validation.

Wednesday, February 25, 2009

F5 problem..asp.net

Whenever we press f5 insert query between page execution whole page execute again.

Solution for this

1) Capcha

2)Maintaining session state and viewstate

How to maintain session and view state

protected void Page_PreRender(Object sender, EventArgs e)
{
ViewState["update"] = Session["update"];
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());//to avoid problem of refresh frm Browser
}
}


protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{

if (Session["update"].ToString() == ViewState["update"].ToString())
{

//Code
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());

}
}


if user press f5 again then the buttonclick will execute again but it will not enter in
if (Session["update"].ToString() == ViewState["update"].ToString())
bcoz now session["update"] value has change.

Friday, February 20, 2009

How to send SQL parameters to sqlDataAdapter?


DataAdapter.SelectCommand.Parameters
.Add(New SqlParameter("@SomeParameter", someValue))

or
DataAdapter.SelectCommand.Parameters.Add("@SomeParameter", SqlDbType.NChar);
DataAdapter.SelectCommand.Parameters[0].Value = SomeParameter;

Thursday, February 12, 2009

GetDocumentById problem in javascript using masterpage

Many times you have faced a problem that you were not able to find the control which is there on ur page using document.getElementById("").
The solution for this problem is use document.getElementByID("")