Thursday, May 9, 2013

Call stored procedure with parameters using Entity Framework 5 and return scalar

Entity Framework 5 using a DbContext
bool result = myDbContext.Database.SqlQuery<bool>(
   "myProc @param1, @param2",
   new SqlParameter("param1", id),
   new SqlParameter("param2", name))
   .First();

Monday, May 6, 2013

ASP.NET dynamic images without a separate aspx file

In the past, when I wanted to display a dynamically generated image in ASP.NET, I would have a standalone aspx page that would stream back the image bytes using a image ContentType.

I recently found out that you can also stream images by setting the src attribute of the image with encoded bytes. This can be easily done using the ASP.NET Image control. Assume you have "myobject" that contains an image byte[] property called "imagebytes":

image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(myobject.imagebytes);

This will increase the size of your HTML since the image bytes are encoded directly into the HTML source. But hey, you don't need a separate aspx file.

Yippie.

Thursday, April 4, 2013

Great jQuery widgit tutorial...finally

I was wanting to find a well-balanced tutorial on jQuery widgets. If you, like me, have searched far and wide only to find a mismash of tutorials, your wait is over. I found this great tutorial on jQuery widget creation. It provides a concise overview, demonstrates the basic construction, and provides just the right amount of complexity for those looking at widgets for the first time.

http://bililite.com/blog/understanding-jquery-ui-widgets-a-tutorial

Enjoy!

Monday, April 1, 2013

Easily open command prompt from Windows Explorer

When using Windows Explorer it is nice to open a command prompt with the initial directory from the one selected. I've known for years you could select a directory in the right window pane, then hold SHIFT and right click to get the context menu; the context menu contains an "Open command window here" option. Nice.

I recently learned there is another easy way to open the command prompt without using the mouse. With the directory selected in the right window pane, press F4 to move to the directory path input box at the top of the window, then simply type "cmd" and press Enter.

Presto! Look Mom, no mouse!

Friday, November 16, 2012

Creating charts in ASP.NET

You can easily create ASP.NET charts in Visual Studio 2010 and VS 2012. Here are the links for getting started.

VS 2010
Visual Studio 2010 Charts

VS 2012
Visual Studio 2012 Charts

A sample pie chart

Have fun!

Friday, September 21, 2012

Randomly sort and display a set of elements using jQuery

I ran into a situation where I needed to randomly select a set of hidden elements from a web page and display only those elements. I also wanted to randomize the order of those elements so the page looked different each time. Here is a quick jQuery example.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var root = $('#tips');
            var tips = $('div.singleTip:hidden');
            $(tips).detach();
            var count = 0;
            var maxCount = 2;
            while (count <= maxCount) {
                var idx = Math.floor(Math.random() * tips.length);
                if ($(tips[idx]).is(':hidden')) {
                    $(tips[idx]).show();
                    root.append($(tips[idx]));
                    ++count;
                }
            }
        });
    </script>
    <style type="text/css">
        .singleTip {
            display: none;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="tips">
            <div class="singleTip">one</div>
            <div class="singleTip">two</div>
            <div class="singleTip">three</div>
            <div class="singleTip">four</div>
            <div class="singleTip">five</div>
            <div class="singleTip">six</div>
            <div class="singleTip">seven</div>
        </div>
    </form>
</body>
</html>

Sunday, August 5, 2012

I've finally got around to formalizing on a command-line parse tool for C#. And the winner is...

Command Line Parser Library

Great CSS 3 style maker

Check out this nice site for crafting CSS 3 styles with multiple browser support.

CSS 3.0 Maker

Thursday, June 21, 2012

Determine which process has a file locked - Windows 7

Are you getting that "can't delete" or "can't rename" a file because it is in use message.  On Windows 7 you can use the Resource Monitor (just type the name into the Start search box to find it) to easily find out which process has a file locked.

Start the Resource Monitor and switch to the CPU tab.  Just type the file name into the Associated Handles search box and you will be able to see which processes have matching file names locked.  To kill the process, just right-click and choose "End Process".

Easy as pie and all part of Windows 7.


Tuesday, June 12, 2012

Better regular expression for phone numbers (multiple formats)

I'm often surprised at how often websites only accept a specific format for phone number input (U.S.) so I thought I would post a more flexible input format.

We want to allow users to enter phone numbers in formats like:

(123) 456-7890
123-456-7890
123.456.7890
1234567890

Using a regular expression, we can allow all these formats and more.


^\s*[(]?(?<AreaCode>\d{3})[)]?(\s*|[.-])(?<Prefix>\d{3})(\s*|[.-])?(?<LineNumber>\d{4})\s*$

Notice that we specify regular expression group names to easily pick up the phone number parts. In C#, this can be done like:

Code:
string prefix = match.Groups["Prefix"].Value;

Finally, here the some C# code to kick things off.

Code:
Regex rePhone = new Regex(@"^\s*[(]?(?<AreaCode>\d{3})[)]?(\s*|[.-])(?<Prefix>\d{3})(\s*|[.-])?(?<LineNumber>\d{4})\s*$");
Match match = rePhone.Match("123.456.7890");
Console.WriteLine(match.Success);

Monday, June 11, 2012

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...