Pages

Monday 16 February 2015

Issues when creating a ASP.NET Dynamic data web application using C# with Entity Framework 6.0 ?

What is ASP.NET Dynamic Data?
Visual studio web application/website project that generates code for CRUD operations [Create, Read, Update, Delete] along with dynamic creation of UI for to display or edit the database information. Clubbing entity framework and ASP.NET  routing helps the dynamic data application to create dynamic URLs for screens. This makes life easier for developers to create a data driven website in few minutes without any hassles.

For more information of ASP.NET Dynamic Data, please refer MSDN - ASP.NET Dynamic Data

Blocker:
ASP.NET Dynamic Data Entities web application project do not work with Entity framework 6.0 in Visual studio 2013.
Issues:
  1. Missing namespaces
  2. Cannot convert lambda expression to type system.web.dynamicdata.modelproviders
  3. iobjectcontextadapter could not be found
  4. JavaScript runtime error: ASP.NET Ajax client-side framework failed to load.

Steps to cross the hurdles and run the application successfully,

  1. Create a ASP.NET Dynamic Data Entities web application project
  2. Refer the Entity Framework 6.0 NuGet Package
  3. Search for EFProvider NuGet and refer the same
  4. Add the entity model and configure the database with entity model
  5. Add the below code snippet in Global.asax file in register routes method,
    1. DefaultModel.RegisterContext(new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(() => new TP_WORKINGEntities()),new ContextConfiguration {ScaffoldAllTables = true});
  6. Build and Run the application
  7. You may get JavaScript Runtime error if yes, Please add the below code snippet in Global.asax file in register routes method
    1. routes.Ignore("{resource}.axd/{*pathInfo}");
  8. Now ASP.NET Dynamic Data web application should run fine without any issues

Tuesday 29 July 2014

Wi-Fi network with limited access after the update of WIN 8.1

Excited about 8.1 update and started installing in HP ProBook 6460b, every step had smooth transition with backup of data and retain my settings done in WIN 8.0

AMG, realized it very late.....I was in real mess after the update, my Wi-Fi is not working. Surprise to see in blogs to rollback the drivers, its better not to update OS instead of searching for legacy drivers.

After few days of struggle, able to fix the problem. May be this could be helpful for some one.

  1. Go to start menu[Tile view] by clicking on windows key
  2. Type in "Device"
  3. Search charm automatically picks the word and list items
  4. Select update device drivers
  5. Expand Network Adapters node
  6. Uninstall all drivers listed, if knowledgeable in identifying specific network adapters uninstall only those adapaters.
  7. Restart your PC
  8. Enable Wi-Fi and log in
  9. Now Wi-Fi should be enabled with internet access.

Sunday 20 July 2014

Packaging and Consuming NuGet packages in .NET projects

NuGet Packaging

Package manager tool with the ability to produce and consume packages, makes a clutter free references when installing/uninstalling packages.
Upon installing a package will automatically add references added in packages and update appropriate configuration files.

Pre-requisites


Available packaging options

1.       Command line
2.       Package Explorer [GUI]

Packaging using command line

  • Launch command prompt [run as administrator]
  • Get in to NuGet executable path


  • Pick the assembly you want to create package(for e.g. DocumentFormat.OpenXml.dll)
  • Get in to NuGet installation folder and create “lib” folder
  • Place the assembly to be packaged in lib folder
  • Run the command in command prompt

  • Creates a new spec file in the NuGet installation folder

  • Open the spec file in notepad, and Update as per spec file mentioned below with appropriate details. Note: Do not copy content from the below mentioned spec file

  • Run command in command prompt with appropriate version details of the assembly

  • Package file will be created


Packaging using package explorer

  •  Launch NuGet Package Explorer and create new package
 

  • Enter appropriate details in meta data and click tick mark in top left corner of the view
  • Add lib folder to the package contents

  • Right mouse click on lib folder and add .NET folder [this may change based on your usage]

  • Right click on the .NET created on step above and add existing assembly file

  • Save the package

  • Verify the package in local folder

Consuming packages in projects using visual studio extension

  • Launch Visual studio and select package manager console
  • Click on Settings options located beside Package source list and add the location for local packages
  • Package repositories from web locations can also be referred and added in package sources.

  • Open project file to add references using NuGet Package
  • Right mouse click on project file and select Manage NuGet Packages

  • Select Local NuGet Packages Added in settings of package source in prior steps, you will be able to see packages dropped in the local folder

  • Click Install

  • Verify references in project references, assembly added in package will automatically added as reference to the project.

Wednesday 8 August 2012

WHY MY ANONYMOUS TYPE CANNOT BE IDENTIFIED AS SAME TYPE IN ANOTHER ASSEMBLY?



WHY MY ANONYMOUS TYPE CANNOT BE IDENTIFIED AS SAME TYPE IN ANOTHER ASSEMBLY?

When we return an anonymous type from different assembly, assembly creates a different identification for the specific type
For e.g. CalledClass [In CalledAssembly which has customerDetail type will have different identification from the customerDetail type in the calling class].

Using reflection we can access anonymous type from another assembly but not suggestible which make your assemblies tightly coupled and maintainability will be too complex.
Reference for access anonymous from different assemblies:
http://blogsprajeesh.blogspot.com/2008/05/accessing-anonymous-types-from.html

Workarounds to access types across assemblies:

1. Using dynamic objects

Caveats:

Option strict should be OFF in VB.NET

2. Using Tuples
References:
http://msdn.microsoft.com/en-us/library/bb384767.aspx

Wednesday 4 April 2012

Return multiple variables from VB.NET function


Problem:

How to return multiple variables from a single method call?

Scenario:

To fetch error messages with the warning level based on the error code, there could be more scenarios in similar fashion. Almost every developer will be muddled for a moment how [?] and start thinking to overcome the situation in a better way.

Problem Code
Public Function GetErrorMessage(errorCode As IntegerAs ??
     Dim errorMessage As String = "How to return multiple values!"
     Dim errorWarningLevel As Integer = 3

     Return errorMessage
     Return errorWarningLevel
End Function


Queries while implementing the above code,

  •        What could be the return type [String/Integer/ etc…]?
  •        If we write multiple return code lines, will it return to the calling method what we expect?


There have been lots of workarounds to handle this scenario here are few,

  •        Split the method into two business operations/methods
  •        Create a type which holds the required properties and return the type
  •        Adding in a list/hash-table and read the variables looping or with specific key
  •        Return as object [base type] and type cast in the calling method


Couple of advanced coding workarounds,

  •        Create an anonymous type in the called method and return as object, caller will type cast and convert into specific type
  •        Create an [Iterator] function and [Yield] the return variables/types, we can implement this using C# and also added as new feature in VB11

  Workaround:

Here is simple data structure introduced in .NET framework 4.0 TUPLE
Tuple can hold up to 8 different types in a single instance and return back to calling method, it not only holds the values but also acts as factory class that create instance of its static members.

Workaround Code
Private Sub MyTuple_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load
        Dim errorDetails = GetErrorMessage(555)
        Dim errorMessage = errorDetails.Item2 'Item2 holds Error message in Tuple
        Dim errorWarningLevel = errorDetails.Item3 'Item2 holds Error WarningLevel in Tuple
    End Sub
 
Public Function GetErrorMessage(errorCode As IntegerAs Tuple(Of IntegerStringInteger)
        Dim errorMessage As String = "How to return multiple variables in a single method call!"
        Dim errorWarningLevel As Integer = 3
 
        'Commented old code for reference
        'Return errorMessage
        'Return errorWarningLevel
 
        'Option 1: using TUPLE helper method 
        Dim errorDetails = Tuple.Create(errorCode, errorMessage, errorWarningLevel)
 
        'Option 2: Using TUPLE constructor
        Dim errorDetails As New Tuple(Of IntegerStringInteger)(errorCode, errorMessage, errorWarningLevel)
 
        Return errorDetails
 
    End Function


Advantages of using Tuples:

  •        Strongly typed
  •        Can hold any defined type [Structured  data types]
  •        Implements IStructuralComparable and IStructuralEquatable

Monday 26 March 2012

Anonymous Types implementation in VB.NET

Anonymous types were designed to provide greater flexibility to create your own type at run time with a simple keyword {New} with as many properties you need, there is no limit in declaring properties with different types to create an anonymous type.

How to create an anonymous type?

Create an object and declare properties that you require, similar to the below mentioned code. After that .NET compiler will take care of everything.

New With {.FirstName = Robert, _
.MiddleName = De, _
                    .LastName = niro, _
                    .Age =27
                     }

What .NET compiler does?

It generates a sealed class similar to the anonymous type declared and it makes the type as immutable and properties declared will be read-only.

Advantages of using Anonymous type:


  • Compiler creates the code on fly and I believe compiler creates a better code than any developer.
  • Reduce in number of code files that holds properties and need not bother about maintenance.

Caveats in using anonymous type:


  • Scope of an anonymous type will be specific to the implemented member
  • Anonymous type neither can be used as parameters nor used as return types
  • Cannot be generated in designer files [HLD/DSD] as separate entity

HOW TO RETURN AN ANONYMOUS TYPE?

It’s really great that we can return an anonymous type and direct cast the object with the original type, then you can access the properties in the instead of using reflection. Kudos to Type inference in VB.NET which makes developer life easier with one setting {Option Infer On}, this helps you to get the actual type from calling methods without the mentioning the actual type name.

Below is the sample that return an anonymous type as object and in the caller class how we type cast using type inference,

Declare a member the returns an object as mentioned below, in the calling class

Public Function GetCustomerDetails() As Object
Return New With {.FirstName = Robert, _
                        .MiddleName = Sr, _
                    .LastName = Deniro, _
                    .Age =27
                     }
End Function
In the caller class
Dim customerDetail = CastCustomerDetail(GetCustomerDetails(), _
                                               New With {                       .FirstName = String.Empty, _
                                                                                                .MiddleName = String.Empty, _
                                                                                                               .LastName = String.Empty, _
                                                                                      .Age = 0
                                                    })

Private Function CastCustomerDetail (Of T)(customerDetail As Object, customerDetailType As T) As T
        Return DirectCast(customerDetail, T)
End Function
Now in the customerDetail variable you can access the properties of {.FirstName as Robert and .LastName as Deniro}

Caveats:

Anonymous types cannot be identified across assemblies with same type and can be realized at runtime and not in compile time {we will discuss this in the below section}

Tuesday 8 November 2011

Installation of windows 8 developer preview


With lot of enthusiasm started installation of windows8[32-bit] in physical box instead running the image in the virtual box.

System Configuration,
Core 2 DUO
4GB RAM
1.5TB HDD
No Graphics card*, no touch screen monitor*
Current box is running with dual OS {Windows server 2003 and Windows7}

How did I proceed with installation of Windows8?
Downloaded the WIN8 developer preview , downloaded version will be in {.ISO} format.
First hurdle to my installation, system BIOS cannot read boot files from ISO.
What next? To make this as an new installable version we need to extract the image file, one more stop How to extract? After a quick Google, got a very good tool to extract the Image files downloaded from EXTRACTNOW.

Extracted the 2.82GB .ISO file and got 2.88GB file list along with bootable files. Loaded my USB memory drive with win8 installable files and restarted my PC by the changing the BIOS settings to USB enabled Boot. No clue why my BIOS didn't boot the windows8 for installation?...Its a perfect disaster with all the above stuff.

But still I have some hope in creating a new installation instance of Win8 in my box by installing from different OS. Logged in to windows server2003 OS and executed the win8 setup.exe file, upgrade of OS was in disable mode and I preferred custom installation with new instance of windows8 in a different drive. Setup was completed approximately with in 15 minutes, eagerly waiting to check the performance and navigations in windows8.

What's new? Awesome all I could see a new screen to select the OS after the restart. Screen really surprised me, I was not able to find instance of windows server 2003 OS in the selection list. I didn't really bothered about my missing windows server2003 OS why because I got windows8 in my box.

Selected the WIN8 developer preview OS and booted win8 for the first time in my box. Able to see all the metro style apps but not able to run the metro style apps, again a problem but able to find it quick.
Workaround!, changed the resolution to 1024*768. Assuming this is the lowest resolution to run metro apps.

Observations, when you select the OS from the boot up screen, surprised to see that it again detect the mother board and hard ware settings to boot the OS. I guess this is to find the touch enabled devices connected to the box.