Coding Geek

A blog by Nadeem Iqbal
Upload file Directly to Amazon S3 using C# via POST

There are scenarios when you want to directly upload the file from the client browser/application to S3.

Amazon provides simple POST method to upload files to amazon File Upload. That method is not acceptable when you want to upload the file(s) programatically using any programming language such as C# or by using the Silverlight.

C# doesn't provide the support of  "multipart/form-data" form posting which is required to upload directly to the S3. So, HttpWebRequest class can be configured so that it will send the request in the required format.

 So, I've written a custom class which will generate the request contents in "multipart/form-data" format.

Below is the code how to use that custom class and upload to the amazon S3 directly using C#/SilverLight

UploadFile uf = new UploadFile("file.txt", "file", "txt/html"); // file should be in bin directory
            string []a = new string[5];
            UploadFile[] files = new UploadFile[1];
            files[0] = uf;

            NameValueCollection form = new NameValueCollection();
            form["key"] = "file.txt";
            form["acl"] = "public-read";
            form["success_action_redirect"] = "http://www.yahoo.com";
           
            form["x-amz-meta-uuid"] = "14365123651274";
            form["x-amz-meta-tag"] = "";
            form["AWSAccessKeyId"] = "zzzzzzzz";
            form["Policy"] = "zzzzzzzzzzzzzzzzzzzzzzzz";
            form["Signature"] = "zzzzzzzzzzzzzzzzzzzzzzzzz";
           
            
            string url = "http://MyBucket.s3.amazonaws.com/";

            string resultQuery = HttpUploadHelper.Upload(url, files, form);
            Console.WriteLine(resultQuery);
 

CHEERS :)
Working @ SnapFlow - BPM Solution

SnapFlow is a RIA Application develop on the Cutting Edge Microsoft Technologies such as SilverLight, ASP.Net 3.5, LINQ, WCF, WWF and SQL Server 2005.

SnapFlow is a workflow-making platform that helps you coordinate your work with anyone so that you can get things done. And with our super-simple tool, you can build a flow that reflects your unique process.

SnapFlow provides the interactive interface for designing the workflows by dragging and dropping items.

I really enjoyed being the part of the SnapFlow Development team and worked on the SilverLight part of the application. The application extensively use the Reflection for generating the classes and collections on the fly as well as populating the controls with dynamic collections such as DataGrid etc..

I really appreciate the effort of Tim Heuer for writing excellent blog on SnapFlow Method~Of~Failed

 

SilverLight Unit Test for Value Converter

When writing unit tests for the Silverlight application, its important to write the unit test for the value converters so that you could test whether converters are working fine or not as Value Converters are most commonly used in Silverlight applications.

 IValueConverter interface exposes two methods Convert & ConvertBack which are private by default and one can't call these methods explicitly in any other class. Neither you can write public wrappers on the Convert and ConvertBack methods as we can't explicitly call these methods. So we need some type of workaround to test the Value Converters.

Here is what we will have to do  read full artical

SilverLight Formatting Text

There are scenarios when you want to Format the bound string value for example you want to display the long names as nadeemIq... or display a different value based on the bound string value & get back the original value. Read the complete blog post

SilverLight DataGrid Binding

 

I was working on the unbound SilverLight DataGrid and thought to write one in this regard,

There are scenarios when you don't have a predefined collection of objects to bind to SilverLight DataGrid or you don't know the structure of the object to bind with untill run time. For Example you want to bind the DataGrid with XML or to any other source.

So the way is to Create the Structure (Type/Class) of object dynamically on the fly and then build the generic List of newly created Type and bind it to SilverLight DataGrid. Moreover you want to add the new empty rows in grid, get the object bound to that row, Extract the values from the bound object, delete a row etc......

 Lets first look at how to create a new Type(class) on the fly with properties and bind it to the DataGrid;

Dynamic Typed Objects Creation by Vladimir Bodurov (thanks)

Update the DataSourceCreator class  to add a public property to maintain the type of the new structure

public static Type getType{get; set;}

and set this property before the last return statement of the ToDataSource function

getType = tb.CreateType(); 

Now how to get the bound Object of selected row of DataGrid on a button click & get values of the properties

var item = Activator.CreateInstance(DataSourceCreator.getType);
int index = mygrid.SelectedIndex;
           
item = mygrid.SelectedItems[0];

MethodInfo theMethod = DataSourceCreator.getType.GetMethod("get_Column1");
object res = theMethod.Invoke(item, new object[0]);  

in the above code i created the new var variable of the Type and stores the selected bound object in it. And then using reflection extracted the value of the property Column1.

Lets Add new row in SilverLight DataGrid

 IEnumerable<object> collection = mygrid.ItemsSource.Cast<object>();
List<object> list = collection.ToList();


var item = Activator.CreateInstance(DataSourceCreator.getType);
            
list.Add(item);

mygrid.ItemsSource = list;

It first gets the bound collection of the DataGrid and then adds a new object into the collection

Similarly you can remove an object from the bound collection

list.Remove(item)

 

Its all about Unbound Simulation of SilverLight DataGrid. Cheers :)

Customizing DataGridView

You can customize the DataGridView to work the way you want. There are scenarios when default behavior of the DataGridView is not sufficient to fulfill the requirements. For example you want to override the behavior of DataGridView such that when i press enter key on a particular cell the cursor/focus move to the cell below to that cell not in the next cell (which is default behavior) similarly while editing data in DataGridView you don't want that the cursor move to the first cell of the next row after editing a row however you want to skip the first column for editing e.g there is an row counter in the first column and you don't want to edit it.

So lets dig into customizing the DataGridView and make it the way we want....

In this sample we modify the DataGridView Behavior as follows; On pressing enter on any cell it creates the new DataGridView row below to that cell and when tab button is pressed at the last cell of the row it creates the new row and moves the cursor to the second cell of the new row (skipping the first column).

 Sample DataGrid

 

 In order to customize the DataGrid, we would have to create a new Component which will directly inherit from the DataGridView as;

 public partial class myDGView : DataGridView

and we will required to override the following two methods;

  • protected override bool ProcessDialogKey(Keys keyData)

This method is called during message preprocessing to handle dialog characters, such as TAB, RETURN, ESCAPE, and arrow keys.

  • protected override bool ProcessDataGridViewKey(KeyEventArgs e)

Processes keys used for navigating in the DataGridView

In both of the above methods; we do much the same thing. We check which key has been pressed and then respond accordingly;

The below code snippets checks the key pressed and then adds the new row below the current row

if (keyData == Keys.Enter)
            {               
                base.Rows.Insert(base.CurrentRow.Index + 1, 1);
                base.SelectedCells[0].Selected = false;
                base.Rows[base.CurrentRow.Index + 1 + 1].Cells[1].Selected = true;
                base.CurrentCell = base.Rows[base.CurrentRow.Index + 1 ].Cells[1];               
                return true;
            }

 

The source code is attached which contains the C# Library Project sample code. Which will generate the DLL which you can add in your Visual Studio's Toolbox by just drag & drop and can use the customized DataGridView as an simple DataGridView.

More Posts