Updates from hengrr RSS Toggle Comment Threads | Keyboard Shortcuts

  • hengrr 9:39 pm on June 9, 2011 Permalink | Reply
    Tags: pattern   

    IRepository 

        public interface IRepository
        {
            T GetById(object id);
            List GetAll();
            IQueryable Query(Expression<Func> filter);
    
            void Add(T t);
            void Delete(T t);
            void Save();
        }
    
     
  • hengrr 9:33 pm on June 9, 2011 Permalink | Reply
    Tags: jquery ajax   

    what is the right way to parse JSON returned datetime field? 

    function updateChat(supplier_id, user_id, spantag) {
        $.ajax({
            type: "POST",
            url: "AjaxService/ChatService.asmx/GetChatBySupplierID",
            data: "{'supplier_id':'" + supplier_id + "', 'user_id':'" + user_id + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var result = ($.parseJSON(response.d));
    
                var str="";
                for (var i = 0; i < result.length; i++) {
                    str += "[" + parseJSONDate(result[i].chat_create_dt) + " " + result[i].usr_fname + "] " + result[i].chat_text;
                    str += "
    "; } $("#" + spantag).html(str); setTimeout(function () { updateChat(supplier_id,user_id, spantag) }, 3500); } }); } function parseJSONDate(jsonDate) { var d = parseFloat(jsonDate.slice(6, 19) ); return new Date(d).format("MM/dd/yyyy hh:mm:ss tt"); }
     
  • hengrr 10:21 pm on December 22, 2010 Permalink | Reply  

    FileHandler to process binary field 

    public class FileHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
    
            HttpResponse Response = HttpContext.Current.Response;
            HttpRequest Request=HttpContext.Current.Request;
            Response.Expires = 0;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
            if (Request.QueryString["fid"] != null)
            {
                ProductItem pi= new ProductItemRepository().GetById(Request.QueryString["fid"]);
    
                if (pi != null && pi.pi_image != null && pi.pi_file_name != null)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + pi.pi_file_name.Trim());
                    Response.ContentType = "application/octet-stream";
    
                    byte[] buffer = pi.pi_image.ToArray() as byte[];
    
                    if (Request.QueryString["t"] != null)
                    {
                        string t=Request.QueryString["t"];
                        int n;
                        Int32.TryParse(t, out n);
                        if (n == 1)
                        {
                            //Converts the byte array to memory stream, then resize the image, then convert back
                            using (MemoryStream ms = new MemoryStream(buffer))
                            {
                                using (Image image = Image.FromStream(ms))
                                {
                                    using (MemoryStream stream = new MemoryStream())
                                    {
                                        Image img = image.GetThumbnailImage(40, 40, null, new IntPtr());
                                        img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                        buffer = stream.ToArray();
                                    }
                                }
                            }
                        }
                    }
                    Response.BinaryWrite(buffer);
                }
            }
            Response.End();
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
     
  • hengrr 7:34 pm on February 20, 2009 Permalink | Reply  

    Linq to Sql: Upload binary file 

    Aneef has a great tutorial on uploading binary files to the db using Linq.  http://www.aneef.net/2009/01/16/uploading-binary-files-or-images-using-linq-to-sql/comment-page-1/#comment-100

    Here’s my version using only Linq.

    
    protected void btnUpload_Click(object sender, EventArgs e)
    {     
        if (fuFileUploader.HasFile && fuFileUploader.PostedFile.ContentLength > 0)
        {
            string path_file_name = fuFileUploader.PostedFile.FileName;
            string ext = Path.GetExtension(path_file_name).Replace(".", "");
            string file_name = Path.GetFileNameWithoutExtension(path_file_name);
            string file_title = txtFileTitle.Text.Trim();
            HelperDataClassesDataContext db = new HelperDataClassesDataContext();
    
            try
            {
                byte[] file_byte = fuFileUploader.FileBytes;
                Linq.Binary file_binary = new Linq.Binary(file_byte);
    
                ControlDocument cd = new ControlDocument
                {
                    guid = Guid.NewGuid(),
                    file_ext = ext,
                    file_nm = file_name.Trim(),
                    file_title=file_title,
                    file_bin = file_binary,
                    is_active = true,
                    upload_page_type = rblLocation.SelectedValue,
                    upload_dt = DateTime.Now,
                    upload_by = UtilUniverse.Common.CurrentUserLogin.Trim()
                };
    
                db.ControlDocuments.InsertOnSubmit(cd);
            }
            finally
            {
    
                db.SubmitChanges();
            }
        }
    
    }
    
    
     
    • charles 3:01 am on September 18, 2009 Permalink | Reply

      Hello Sir R,
      Are you able to send this newbie your sample code via email?
      I would like to see how you used HelperDataClassesDataContext. Also, your code got cut off the side. Thank you in advance for sharing your talents.

    • Afework 1:57 am on January 3, 2010 Permalink | Reply

      your sample code is very useful and it works properly for me.
      I am trying to do simple social network website in my campus.For the website i need users to upload phto and display on their page.Previously I use Handler.ashx page to display image from a database using sql command.when i try to change the code written in sql command to linq i got a problem.If you know how to display the image stored in database send me.
      here is my email :afework1980@yahoo.com
      thank you for your help!

    • Dhananjay Kumar 5:36 am on May 8, 2010 Permalink | Reply

      NIce article very informative

  • hengrr 8:04 pm on February 2, 2009 Permalink | Reply  

    Onion and his ways! 

    My dog Onion barfed on our carpet today.  What am I supposed to do?  I was ready to put him in his cage, but he was so quiet that I left him playing in the sun…more like lying in the sun.

     
  • hengrr 10:26 pm on December 18, 2008 Permalink | Reply  

    Clearing the clipboard: Windows XP 

    Sometimes your clipboard will get full of copy and pastes.  To clear it:

    1. Start > Run
    2. On the cmdline, type clipbrd (clipboard viewer will pop up)
    3. delete
     
  • hengrr 5:57 am on August 12, 2008 Permalink | Reply
    Tags: ordain   

    On Oct 25 – Ordination “Bouh” 

    The word “Bouh” means a lot to a family who follows “Theravada Buddhism”.  I did this to thank my parents and oh, were they so proud to be parents.  In my heart I did the best thing any child could do for their parent, family, and friends.  It’s a small sacrifice I’m willing to make.  Here’s my schedule as a monk.  I was there for two weeks-all the vacation I had left.

    Schedule

    4:30 AM – Wake up and get ready to chant
    5:00 AM – Chant :)
    6:00 AM – Meditate
    7:00 AM – Breakfast
    8:00 AM – Relax, read, meditate, clean, chores
    11:00 AM – Lunch

    Some pictures:

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Follow

Get every new post delivered to your Inbox.