28 Mar 2013

General programming rules #1

Don't use boolean method parameters.

Why? Because it makes your code unreadable and inextensible.

e.g. render(true) is meaningless but render(PageMode.Readonly) is semantic and unambiguous.

26 Mar 2013

Post an array in integers using Jquery & Ajax to MVC controller action

Mvc Action

public JsonResult Receive(int[] ages)
{            
    return Json(string.Format("ages {0} received", String.Join(",",ages)));
}

Jquery Code

        $(function() {


            $("#PostArray").click(function() {

                var arr = new Array();
                arr.push(1);
                arr.push(21);
                arr.push(23);

                $.ajax({
                    url: '/Home/Receive',
                    type: 'POST',
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify({ages: arr }),
                    success: function(data) {
                        alert(data);
                    }
                });

            });

        });
Download Example

List of ASP.NET Web API and HttpClient Samples

I just came across this massive list of web API code samples which is well worth a gander if you're looking at API development. And let's face it, in this new mobile age no developer can really be without this knowledge.

25 Mar 2013

Entity Framework 5 Many to Many relationship setup

Sample Solution Download Link
How to setup entity framework objects for many to many relationship.
I had a problem trying to get this to work but after I managed to get it working
I decided to post the solution here with a hope that it will help someone someday.

To begin.
1. Download the solution (it is a vs-2012 solution) & unzip accordingly
2. Create a database called (EdkSamples)
3. Create a login (user: developer, password: letmein$123)
4. Create database tables using the scripts
    Edikgale.Samples.Data\Database Setup\DatabaseCreate.sql
5. Initialise the database data with the script in folder
    Edikgale.Samples.Data\Database Setup\DataSetup.sql
6. To see how this works simply run the test in
    EdwardDikgale\Edikgale.Samples.Data.Tests\EdikgaleEfContextTest.cs

Note
1. The test project uses a connection string defined in App.config which
you may wish to adapt accordingly.
2. The linking table is not modelled as a poco object because there is no need to unless
you happen to have some extra properties on it.

if you have any problems feel free to email me: edward.dikgale@gmail.com

I hope this helps you in some way
Ed


posted by: Edward Dikgale

15 Mar 2013

100 Cool jQuery Plugins

Here's a great resource - 100 cool (and, in some cases, super-cool) jQuery plugins

13 Mar 2013

Some useful SQL statements

Here are a couple of SQL statements I seem to use on a regular basis.

  1. Find all objects that contain specific text:
    SELECT DISTINCT b.name + '.' + a.[name]
    FROM sys.objects a
    INNER JOIN sys.schemas b ON a.schema_id = b.schema_id
    INNER JOIN syscomments c ON a.object_id = c.id
    WHERE c.[text] LIKE '%CPRS%' 
    -- AND a.[type] = 'P' -- stored procedure
    -- AND a.[type] = 'V' -- view 
    -- AND a.[type] = 'FN' -- function etc.
    
  2. Find all foreign keys for a specific table column:
    SELECT 
        f.name AS ForeignKey,
        OBJECT_NAME(f.parent_object_id) AS TableName,
        COL_NAME(fc.parent_object_id,
        fc.parent_column_id) AS ColumnName,
        OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
        COL_NAME(fc.referenced_object_id,
        fc.referenced_column_id) AS ReferenceColumnName,
        COL_NAME(fc.parent_object_id, fc.parent_column_id)
    FROM 
        sys.foreign_keys AS f
        INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
    WHERE
     OBJECT_NAME (f.referenced_object_id) = '[TABLE_NAME]' 
    AND 
     COL_NAME(fc.parent_object_id, fc.parent_column_id) = '[COLUMN_NAME]'