Thursday, October 23, 2008

Random String Generator

I encountered the problem where I needed to build a lot of controls at runtime and of course I needed the to have a name. I decided to use a Random String builder and here is what I cam up with:

 public static string RandomString(this String str, int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(2 * random.NextDouble() + 28)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

* One small side note is that the 2 and the 28 in the sample have no correlation to the result, I just wanted a completely random result.

Dynamically Render an Image on a Web Page ASP.net

I have used this trick so many times, I am somehow surprised that Microsoft don't have the built in functionality for rendering an image at runtime on a web page. All that needs to be done is creating a new .aspx page (we will call it RenderImage.aspx) within your project and in the Page_Load method, insert the following code.

byte[] ImageInfo = //get 
  MemoryStream s = new MemoryStream();
            try
            {
              s  = new MemoryStream(ImageInfo );
            }
            catch (Exception ex)
            {

            }
if (s.Length != 0)
            {
                        Response.BinaryWrite(ImageInfo) ;
}

Call it from another .aspx like so :

Image2.ImageUrl = "RenderImage.aspx";

Couldn't be easier.....well you say that now....just keep in mind that if you are using an UpdatePanel with ajax, that the call to BinaryWrite will not be liked, as it causes a MemoryStream error. 

      

Bind an Entity to a Row.DataItem

It is innevitable these days that when building a website you will need to use the GridView control within Visual Studio. In my early days of programming, I used to simply use Row[some int] to get row information on the Editing, Deleting and ItemBound events (Bad). Now that I am using the entity framework, I needed a way to be able to strongly bind an entity to a Row that I selected within the Gridview. Easy!

 public static class EntityDataSourceHelper where TEntity : class
    {
public static TEntity GetDataItemEntity(object dataItem)
        {
            var entity = dataItem as TEntity;
            if (entity != null)
            {
                return entity;
            }

            var td = dataItem as ICustomTypeDescriptor;
            if (td != null)
            {
                return (TEntity)td.GetPropertyOwner(null);
            }

            return null;
        }
}

Calling this is simple....

      protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                PersonEntity person=        EntityDataSourceHelper< PersonEntity  >. GetDataItemEntity(e.Row.DataItem);

            }
        }

This saves you from a couple of issues :

1. Referencing null column
2. Extra code to cater for null checks on rows
3. Extra code for casting etc