Friday, September 21, 2012

Maintaining Session Variable in DLL in Asp.Net

In order to control Session Variable in DLL, the class need to inherit from IRequiresSessionState interface and set the class Serializable attribute.
The property should be static (class member) in order to avoid instantiate new object.
eg. CurrentUserSESSION.USER_ID 

public enum SESSION_NAME
    {
        USER_ID,
        USER_PASSWORD,
        USER_FULLNAME,
        USER_ROLE
    }

    [Serializable()]
    public class CurrentUserSESSION : IRequiresSessionState
    {
        public static string USER_ID        {
            get
            {
                return GetSessionValue<string>(SESSION_NAME.USER_ID);
            }
            set
            {
                SetSessionValue<string>(SESSION_NAME.USER_ID, value);
            }
        }
        public static string USER_PASSWORD //login user id
        {
            get
            {
                return GetSessionValue<string>(SESSION_NAME.USER_PASSWORD);
            }
            set
            {
                SetSessionValue<string>(SESSION_NAME.USER_PASSWORD, value);
            }
        }
        public static string USER_FULLNAME
        {
            get
            {
                return GetSessionValue<string>(SESSION_NAME.USER_FULLNAME);
            }
            set
            {
                SetSessionValue<string>(SESSION_NAME.USER_FULLNAME, value);
            }
        }
        public static string USER_ROLE
        {
            get
            {
                return GetSessionValue<string>(SESSION_NAME.USER_ROLE);
            }
            set
            {
                SetSessionValue<string>(SESSION_NAME.USER_ROLE, value);
            }
        }
      
        public CurrentUserSESSION()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private static T GetSessionValue<T>(SESSION_NAME SESSIONNAME)
        {
            if (null != HttpContext.Current.Session[SESSIONNAME.ToString()])
                return (T)HttpContext.Current.Session[SESSIONNAME.ToString()];
            else
                return default(T);
        }

        private static void SetSessionValue<T>(SESSION_NAME SESSIONNAME, T SESSIONVALUE)
        {
            HttpContext.Current.Session[SESSIONNAME.ToString()] = SESSIONVALUE;
        }
    }

Generating MS Word file on the fly in Asp.Net

There is a simple way in order to generate MS Word using ContentType = "application/word" but it will give you in Web Layout instead of Print Layout generally.

public void ExportToMSWord(string fileNameWord)
    {
        StringBuilder sb = new StringBuilder();
        string strBody = string.Empty;
        strBody = @"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
        "xmlns:w='urn:schemas-microsoft-com:office:word'" +
        "xmlns='http://www.w3.org/TR/REC-html40'>";

        strBody = strBody + "<!--[if gte mso 9]>" +
        "<xml>" +
        "<w:WordDocument>" +
        "<w:View>Print</w:View>" +
        "<w:Zoom>100</w:Zoom>" +
        "</w:WordDocument>" +
        "</xml>" +
        "<![endif]-->";

        sb.Append(strBody);
        sb.Append("<table width=\"100%\" style=\"background-color:#cfcfcf;\"><tr><td>aaaaaaaaaaa</td><td>bbbbbbbbb</td></tr></table>");

        string attachment = "attachment; filename= " + fileNameWord;
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/word";
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        ////this.RenderControl(htw);
        //Response.Write(strBody + sw.ToString());
        Response.Write(sb);
        Response.End();
    }


Alternatively, Interop assemblies of Microsoft Word Object Library can be used and in that case, Microsoft office version need to be aware.