Friday, August 31, 2007

Customize attachment settings in outlook

I got a mail in my outlook and the application didn't let me open the attachment because it might be unsecure. The mail was from a know person and I want to access the attachment file. It appears that you can override the security level as described by Microsoft ( Customize attachment settings in Outlook 2007) and let some file types allowed to be opened.
 
To do this you have to tell the outlook which types of files are allowed. You do it in the registry:
HKCU\Software\Microsoft\Office\12.0\Outlook\Security\Level1Remove
 
You simply mention the extentions seporated by a semicolon like this one :
pts;bat;reg;vbs
 
 

Tuesday, August 21, 2007

Read/Write files from/to an Image field in SQL-Server

In this example I have a table called Attachment having a field of time Image which holds binary value. In this field I am going to put any type of file in a byte array and read it back. I have defined a class called Attachment to read and write the records. This class has a variable called Data which is defined as byte[]


Read Image Field


This example passes a reference to an object called record of type Attachment. The record object has an attribute called Data where the attachment file will be streamed into it.


/// <summary>
/// Read a TestLog record identified by the given parameter
/// </summary>
/// <param name="id">the record key</param>
/// <returns>a Attachment object that maps to the record</returns>
public static void Read(Attachment record)
{


if (record.Id < 1)
throw new ApplicationException("Invalid Record ID");

using (SqlConnection sqlConnection1 = Connection)
{
SqlCommand cmd
= sqlConnection1.CreateCommand();
cmd.CommandType
= CommandType.Text;
cmd.CommandText
= "SELECT TestLogId, Name, Description, CreatedBy, CreateDate, Data FROM Attachment WHERE ID=@Id";

SqlParameter par
= new SqlParameter("@Id", SqlDbType.Int);
par.Value
= record.Id;
cmd.Parameters.Add(par);


sqlConnection1.Open();
SqlDataReader dr
= cmd.ExecuteReader();
if (!dr.HasRows)
throw new ApplicationException(String.Format("Could not find TestLog '{0}'.", record.Id));

dr.Read();

if (!dr.IsDBNull(0))
record.TestLogID
= dr.GetInt32(0);
if (!dr.IsDBNull(1))
record.Name
= dr.GetString(1).TrimEnd();
if (!dr.IsDBNull(2))
record.Description
= dr.GetString(2).TrimEnd();
if (!dr.IsDBNull(3))
record.CreatedBy
= dr.GetString(3).TrimEnd();
if (!dr.IsDBNull(4))
record.CreateDate
= dr.GetDateTime(4);

// Read the bytes into the Data attribute of the record
int PictureCol = 5; // the column # of the BLOB field
record.Data = new Byte[(dr.GetBytes(PictureCol, 0, null, 0, int.MaxValue))];
dr.GetBytes(PictureCol,
0, record.Data, 0, record.Data.Length);

dr.Close();
sqlConnection1.Close();
}
}

Write File into the record



/// <summary>
/// Save the Attachment record. If Id doesn't exist, it will create a new one, otherwise it will update.
/// </summary>
/// <param name="record">the attachment record to save</param>
public static void Save(Attachment record)
{

using (SqlConnection sqlConnection1 = Connection)
{
SqlCommand cmd
= sqlConnection1.CreateCommand();
cmd.CommandType
= CommandType.StoredProcedure;
cmd.CommandText
= "SaveAttachment";

#region Add the input parameter and set its value

SqlParameter par
= new SqlParameter("@TestLogId", SqlDbType.Int);
par.Value
= record.TestLogID;
cmd.Parameters.Add(par);

par
= new SqlParameter("@Name", SqlDbType.NVarChar, 128);
par.Value
= record.Name;
cmd.Parameters.Add(par);

par
= new SqlParameter("@Description", SqlDbType.NVarChar, 320);
par.Value
= record.Description;
cmd.Parameters.Add(par);

par
= new SqlParameter("@Data", SqlDbType.VarBinary, record.Data.Length);
par.Value
= record.Data;
cmd.Parameters.Add(par);

#endregion

// Add the output parameter
par = new SqlParameter("@Id", SqlDbType.Int, 4);
par.Direction
= ParameterDirection.InputOutput;
par.Value
= record.Id;
cmd.Parameters.Add(par);

sqlConnection1.Open();
cmd.ExecuteNonQuery();
int id = Int32.Parse(cmd.Parameters["@ID"].Value.ToString());
record.Id
= id;
sqlConnection1.Close();
}
}

Read the file into the variable




/// <summary>
/// Read the file into the Data field
/// </summary>
/// <param name="path">A valid path to the file</param>
public void ReadFromFile(string path)
{
System.IO.FileStream fs
=
new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);

this.data = new Byte[fs.Length];
fs.Read(
this.data, 0, this.data.Length);
fs.Close();
}


Write the byte array to a File


This simple method is using the name of the file as is stored in the attachment record to save in the location specified by parameter.


/// <summary>
/// Save the content into a file
/// </summary>
/// <param name="path">the full path to write to</param>
public void SaveToFile(string path)
{
if (path.EndsWith("\\"))
path
+= "\\";

string DestFilePath = path + this.name;
System.IO.FileStream fs
=
new System.IO.FileStream(DestFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);

fs.Write(
this.data, 0, this.data.Length);
fs.Close();

}

Monday, August 20, 2007

Merging Menu and Toolbar to MDI Form

I have a form that it is used both stand alone and within an MDI from. When used in an MDI form I wanted to move the menu's and toolbars to the shared placed on the MDI form whereas using it on a standalone case I had no option than having it on the owner form.

To establish this behavior I had to implement the activate and deactivate methods to make the toolstrip and menu strip, which hold the toolbar items and menu items, invisible and move the items to the new container. This is a stait forward solution as illustrated below:



private void OnFormActivate(object sender, EventArgs e)
{
try
{
// Get the parent
Form parent = this.MdiParent;
if (parent == null)
return;

// Get the toolbar on the parent by name
string targetToolbar = "toolStrip"; // using the name of the control
if (!parent.Controls.ContainsKey(targetToolbar))
return;
ToolStrip target
= parent.Controls[targetToolbar] as ToolStrip;
if (target == null)
return;

ToolStripManager.Merge(
this.toolStrip1, target);

// Hide the original container
this.toolStrip1.Visible = false;
this.menuStrip1.Visible = false;
SetToolbarCaption();
}
catch { } // Any exception will cause the toolbar to remain on its oroginal parent
}
private void OnFormDeactivate(object sender, EventArgs e)
{
try
{
// Get the parent
Form parent = this.MdiParent;
if (parent == null)
return;

// Get the toolbar on the parent by name
string targetToolbar = "toolStrip";
if (!parent.Controls.ContainsKey(targetToolbar))
return;

// Double check
ToolStrip target = parent.Controls[targetToolbar] as ToolStrip;
if (target == null)
return;

// Actual Merge Revert of the contents of the toolStrip
ToolStripManager.RevertMerge(target, this.toolStrip1);

// Keep hiding the original container
this.toolStrip1.Visible = false;
}
catch { }
}


All I need to do next is to wire these events to the propper events: FormActivate and Deactivate.

Thursday, August 9, 2007

Welcome

I have lost my blogs that I used to store on http://blogs.wdevs.com/a.Panahy/ The site is just gone down and made me desporate. I decided to start my own. Thanks to NiMS I got this subdomain up and running in short time and the idea is to build up my personal blogs in this blog site.
The site will start with my technical blogs to support my (leaking) memory. I might have a copy of them on other online site (maybe on codeproject.com or msn.Live) and later on probably some private blogs.
for now all I want to say is yoopy