Thursday 18 December 2014

Create A Dropdown List with A Range of Numeric Values in ASP.NET MVC 4

If you want to have a dropdownlist list containing a range of numerical values here's how to do it:

Source: http://stackoverflow.com/questions/12070363/dropdown-list-asp-mvc

@{ var values = new SelectList(Enumerable.Range(0, 60)); }

@Html.DropDownListFor(model => model.NoOfUserAccounts,values)
@Html.DropDownListFor(model => model.NoOfUserAccounts)s)

Thursday 27 November 2014

Entity Framework 5 Code First - Customizing Migrations

You want to make changes to your Models and Database, you have auto migration enabled and you run:

update-database -force

you get this error:

'FK_dbo.{TABLENAME}_dbo.{OTHER_TABLENAME}_otherID' is not a constraint.
Could not drop constraint. See previous errors.

You sh*t a brick.

Solution:

Run this:

Add-Migration Update{You Model Name}Class

This creates a file in Migrations containing a Partial Class outlining what changes the Framework is attempting as a "best guess"

Remove any unwanted changes in this file.

E.g DropForeignKey("dbo.{TABLENAME}", "otherID", "dbo.{OTHER TABLE}");

Now run:

Update-Database –TargetMigration:Update{You Model Name}Class

NOW READ THIS, TWICE!

http://msdn.microsoft.com/en-gb/data/jj591621.aspx#customizing

And if none of that works

If you're still getting errors, and trust me, it happens. Then I recommend backing everything up and starting with a clean slate.

I followed the instructions here http://stackoverflow.com/questions/11679385/reset-entity-framework-migrations from Todd. Thank's Todd :)

  1. Delete the migrations folder in your project
  2. Delete the __MigrationHistory table in your database (may be under system tables)
  3. In PM Console run: Enable-Migrations -EnableAutomaticMigrations -Force
  4. In PM Console run: Add-Migration Initial
  5. In PM Console run: update-databse -TargetMigration:Initial

Wednesday 6 August 2014

Adding a Horizontal Line to PDF similar to hr tag

iTextSharp.text.pdf.draw.LineSeparator line1 = new iTextSharp.text.pdf.draw.LineSeparator(0f, 100f, BaseColor.BLACK, Element.ALIGN_LEFT, 1);
document.Add(new Chunk(line1));

Friday 25 July 2014

Restrict a Method To Members of Certain Roles

Simple Example

// Only Let People who are in the Administrator or Managers Roles Use this method
   [Authorize(Roles = "Administrator,Managers")]
        public ActionResult Delete(int id = 0)
        {
            Report Report = db.Report.Find(id);
            if (Report == null)
            {
                return HttpNotFound();
            }
            return View(Report );
        }

MVC 4 - Creating PDF Files with iTextSharp

I've found in my latest job that just about every project requires spitting out PDFs in various flavours so here's a simple example:

// First things first, make sure you've got the package installed
// Package Manager
PM> Install-Package iTextSharp

// In your Controller make sure you reference iTextSharp.text and iTextSharp.pdf
using iTextSharp.text;
using iTextSharp.text.pdf;

// Outputs a Simple PDF with the Name and Description of a given Product
    public FileStreamResult ProductInfoPDF(int id)
        {
        Product = context.Products.Find(id);
// if we don't find anything then just quit
            if (Product == null)
            {
                return null;
            }

            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
           // Landscape Layout
            document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
            PdfWriter.GetInstance(document, workStream).CloseStream = false;
            document.Open();
            // Heading
            Paragraph pHeading = new Paragraph("Product Information");
            pHeading.Alignment = Element.ALIGN_CENTER;
            pHeading.Font.SetStyle(Font.BOLD);
            pHeading.Font.Size = 12;
            pHeading.SpacingAfter = 18f;
            document.Add(new Paragraph(pHeading));                  
            Paragraph p = new Paragraph("Some background about this product.");
            p.Font.SetStyle(Font.BOLD);
            p.Font.Size = 8;
            document.Add(p);
            // Product Name
            Paragraph pProductName = new Paragraph("Product Name: " + Product.Name);
            pProductName.Font.SetStyle(Font.BOLD);
            pProductName.Font.Size = 8;
            document.Add(pProductName);
            // Description
            document.Add(new Paragraph("\n"));
            document.Add(new Paragraph("\n"));
            Paragraph pDescription = new Paragraph("Description");
            pEquip.Font.SetStyle(Font.BOLD);
            pEquip.Font.Size = 8;
            document.Add(pDescription );        
            document.Close();
            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;
            return new FileStreamResult(workStream, "application/pdf") { FileDownloadName = "Product_" + Product.Id.ToString() + ".pdf" };

        }


// Add  a link in your View
  @Html.ActionLink("Print", "ProductInfoPDF","Products" ,new { id = Model.Id }, new { @class = "printLink"})

LINQ Kung Fu - Tips from t'coalface of t'Interwebs

I find myself constantly having to look up how to do things in LINQ that I find straightforward in SQL, so I'm making a note of them here. Hopefully someone else will find these useful.


Select Statements

Get List based on Category

// Get Products in a certain category
// same as SQL: select * from Products where CategoryId = CategoryId
    int CategoryId = 1;
    List<Product> Products = db.Products.Where(x => x.CategoryId == CategoryId).ToList();

Select a Single Value with Where Clause

// get the Price of the latest product entered with the Name: "Box of Blue Sparks"
// SQL Equivalent: select TOP 1 Price from Products where Name = 'Box of Blue 
// Sparks' ORDER BY CreatedDate DESC
string pName = "Box of Blue Sparks";
  var available = db.Products.Where(x => x.Name == pName).OrderByDescending(x => x.CreatedDate).Select(x => x.Price).FirstOrDefault();
                              

Select DISTINCT

// Get a list of Distinct Product Colours
// SQL Equivalent: SELECT DISTINCT(Colour) FROM Products WHERE Price is more then £10
List<string> ProductNames = db.Products.Where(x => x.Price > 10).Select(x => x.Colour).Distinct().ToList();

// Note to the Colonials, that is the correct way to spell COLOUR!

Select a Single Record

// Get the first product priced over £100
// SQL Equivalant: SELECT TOP 1 * FROM Products WHERE Price > 100
Product p = db.Products.Where(x => x.Price > 100).FirstOrDefault();
I'll add to this as I get further along the learning curve.

Update 08-01-2015

Get all Model Validation Errors via LINQ

This is very useful when debugging MVC Controller Methods.

var allErrors = ModelState.Values.SelectMany(v => v.Errors);

Tuesday 22 July 2014

MVC 4 Display Correct Date Format in Edit Mode

This works:

@Html.TextBox("DateOrdered", string.Format("{0:d}", Model.DateOrdered))

Monday 21 July 2014

MVC 4, EF5 Code First - Forcing a Decimal to x Number of Places

Stick this in your DB Context if you need to have EF recongise more than two decimal places.

  protected override void OnModelCreating(DbModelBuilder modelBuilder)  
     {  
       // We have a Decimal Field in Our SQL Table decimal(18, 4)  
       // By default only the first two digits after the decimal will be recognised if this property is set to type decimal  
       // we need to override this behaviour and here's how  
       modelBuilder.Entity<MyObjec t>().Property(f => f.totalWeightMg).HasPrecision(18,4);       
       base.OnModelCreating(modelBuilder);  
     }  

Wednesday 9 July 2014

Return Yes, No from Boolean instead of True, False

I recently needed to output Boolean values into an iTextSharp PDF document having them represented as Yes or No rather than True or False

Cue MSDN. This article Boolean Structure explains how to do this.

Here's a quick example:

bool JobDone = true;
string strJobDone = JobDone ? "Yes" : "No"; // Returns "Yes"

Wednesday 4 June 2014

MVC4 Entity Framework and Decimal Precision

This just caught me out today so hopefully it will help someone else.

Stack Overflow answer here http://stackoverflow.com/questions/3504660/decimal-precision-and-scale-in-ef-code-first

If you have a database field with the spec decimal(18,5) the Entity Framework will still round the number to 2 decimal places by default, so if you enter a number e.g. 15.42700 it will be saved to your database as 15.42000, not very helpful eh. Anyway to fix it you need to override OnModelCreating in your DBContext Class

public class EFDbContext : DbContext { protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { modelBuilder.Entity().Property(object => object.property).HasPrecision(12, 10); base.OnModelCreating(modelBuilder); } }

Friday 11 April 2014

Getting to Grips with Sharepoint Foundation 2013 REST API

I'm finding myself constantly struggling against the OOB (Out Of Box) features of Sharepoint Foundation 2013 and often need to find ways to interact with Lists in ways that are just not available. Enter the REST API. If, like me, you need to get to grips with using the Sharepoint REST API then I recommend the article SharePoint: Adventures with the REST API Part 1 which is by far the best guide I have found on them t'interwebs.

Autocomplete Lookup Field in Sharepoint Foundation 2013

Recently I've been working on a project where there are several related Lists, in this particular instance a list of Appointments and Patients. So the Appointments List has a Lookup field Patient. By default this gives you a select or drop down list, not ideal if you end up with several items on the list.

I eventually stumbled across SharePoint 2013 Autocomplete Lookup Field. Pretty straightforward to install provided you have server access and works like a charm. Highly recommeded.

Adding a simple Chat Facility to a Sharepoint Foundation 2013 Site

I've recently been working on a Sharepoint Project where the client requires their receptionist to be able to send them messages without flooding their email. So they asked for some kind of chat or instant messaging facility. I stumbled across Simple Chat a basic, lightweight JQuery based script which does the job very well.

Implementing the script is really easy. Here's the short version:

  • Create a Document Library to store your JavaScript files.
  • Create a Custom List to display your messages
  • Add a Script Editor Web Part to where you want to keep your Chat facility. Edit Snippet, add JQuery code etc.

Job done.

Tuesday 1 April 2014

Restore Content Types in Sharepoint Foundation 2013

If you find yourself in the situation where you've messed up with the Content Types in a Sharepoint Site Collection then this will help restore everything back to the default settings:

Open - Start - All Programs - Microsoft Sharepoint Products - Sharepoint Management Shell and run this:

stsadm -o deactivatefeature -name Fields -url http://sitecoll -force
stsadm -o activatefeature -name Fields -url http://sitecoll -force

Source: http://sharepoint.stackexchange.com/questions/8517/site-content-types-screwed-up-how-to-restore-to-default/26225#26225?newreg=0c4f241b46ab466ea0d265069efdca5f

Wednesday 26 March 2014

SharePoint Foundation 2013 Forms Based Authentication (FBA)

Scenario

You need to have individual Forms Based Authentication (FBA) on several different Sharepoint Foundation 2013 Site Collections.

Solution

  1. Run c:\windows\Microsoft.NET\Framework64\v4.0.30319\ aspnet_regsql.exe to set up Membership Database
  2. Give Identity Running Sharepoint "db_owner" priveleges in MSSMS for the aspnetdb database.
  3. Create a Dummy Website in IIS "MembershipConfig"
  4. Install FBA for SharePoint 2013 - This allows management of users inside sharepoint
  5. Edit machine.Config File
  6. Configure Forms Auth in Sharepoint for Site Collection

Caveats

  • Use SQL Authentication for ALL Connection Strings and DB User Accounts.

Resources

Here's a list of the resources I found which helped get the job done:

Friday 21 February 2014

Searching Model Data based on string and integer field types

Scenario

You need to add a search function based on model data based on searching both string and int datatypes. If you try the obvious method in your LINQ query as just converting your int field ToString you end up with an error similar to this:

ToString is not supported in linq to entities

Or even this bad boy:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

After trawling around the forums for a good hour or so I found the following solution. Hope it helps someone else too.

 public ActionResult Search(string search = null)  
     {  
       int intSearch;  
       if (!Int32.TryParse(search, out intSearch))  
       {  
       }  
       IEnumerable<Car> cars = context.Cars.Where(  
            c => search == null  
              || c.IntegerField == intSearch  
              || c.Manufacturer.Contains(search)  
              || c.Model.Contains(search)             
             );  
       cars = cars.OrderBy(c => c.Id).ToList();  
       ViewBag.Search = search;  
       return View(cars);  
     }