Tuesday, July 10, 2007

Make password fileld not clear when the page is post back


protected void Page_Load(object sender, EventArgs e)
{
//to preserve pasword text at the pose back
txtPword.Attributes.Add("Value", txtPword.Text.ToString());

}

//in the pasword box prerender event
protected void txtPword_PreRender(object sender, EventArgs e)
{
txtPword.Attributes["Value"] = txtPword.Text;
}

Tuesday, July 3, 2007

Reading binary data from Sql sever Database C# Code

//Define Memory Stream
MemoryStream msMemoryStreams = new MemoryStream();
DBConnection conn = new DBConnection();
SqlDataReader sdrReader;
sdrReader = conn.GetDownloadFile("GetFilesDownlod", Convert.ToInt32(DownloadID));
sdrReader.Read();
// Reading the IMAGE data filed data to byte array
// sdrReader.GetBytes(XXX, 0, null, 0, int.MaxValue) , XXX defines the colum number (Zero based)for image data ;see SQL query


byte[] byteData = new Byte[(sdrReader.GetBytes(3, 0, null, 0, int.MaxValue))];
sdrReader.GetBytes(3, 0, byteData, 0, byteData.Length);
//Get the file name
// sdrReader.GetString(XXX) , XXX defines the colum number (Zero based) for file name ;see SQL query

string strOutputFileName = sdrReader.GetString(0);
//get file extension from sqldatareader

string Extension = "." + sdrReader["Extension"].ToString();
strOutputFileName = strOutputFileName+Extension;
msMemoryStreams.Write(byteData, 0, byteData.Length);
//Convert the memorystream to an array of bytes.
byte[] byteArray = msMemoryStreams.ToArray();
//Clean up the memory msMemoryStreams

msMemoryStreams.Flush();
msMemoryStreams.Close();
// Clear all content output from the buffer msMemoryStreams
Response.Clear();
// Add a HTTP header to the output msMemoryStreams that specifies the default filename
// for the browser's download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + strOutputFileName);
// Add a HTTP header to the output msMemoryStreams that contains the
// content length(File Size). This lets the browser know how much data is being transfered


Response.AddHeader("Content-Length", byteArray.Length.ToString());
// Set the HTTP MIME type of the output msMemoryStreams
Response.ContentType = sdrReader["ContentType"].ToString();
sdrReader.Close();
// Write the data out to the client.

Response.BinaryWrite(byteArray);
//to stoping adding unwanted html coding

Response.End();