Posts

Showing posts from February, 2017

Donate

ASP.NET MVC CRUD(Create/Update/Delete) With Dapper ORM in VB.NET

Image
Konnichiwa, Here's a VB.NET ASP.NET MVC CRUD project using Dapper ORM which is the counterpart of this post ASP.NET MVC with Dapper ORM . The only file in the Models folder that has updates is Customer.cs class such as applying Display and DisplayAttributes. Imports System.ComponentModel.DataAnnotations Public Class Customer Public Property CustomerID() As Integer Get Return m_CustomerID End Get Set (value As Integer ) m_CustomerID = value End Set End Property Private m_CustomerID As Integer <Display(Name:= "Company Name" )> Public Property CompanyName() As String Get Return m_CompanyName End Get Set (value As String ) m_CompanyName = value End Set End Property Private m_CompanyName As String <Display(Name:= "Address" )> Public Property Address() As String

ASP.NET MVC CRUD(Create/Update/Delete) With Dapper ORM

Image
Hi All, Here's an ASP.NET MVC CRUD(Create/Update/Delete) project with Dapper ORM. The model classes and interface are based from this post Using Dapper ORM in ASP.NET Web Forms with few modifications in Customer class. The updates are adding Display attribute and DisplayFormat attribute to CreditLimit and IntroDate properties. using System; using System.ComponentModel.DataAnnotations; namespace ASPMVCDapper.Models { public class Customer { public int CustomerID { get ; set ; } [Display(Name="Company Name")] public string CompanyName { get ; set ; } [Display(Name = "Address")] public string Address { get ; set ; } [Display(Name = "City")] public string City { get ; set ; } [Display(Name = "State")] public string State { get ; set ; } [Display(Name = "Intro Date")] [DisplayFormat(DataFormatString = "{0:yyyy-MM

Unable to cast object of type 'DapperRow' to return Type

Aloha, Given the following code which generates an exception "Unable to cast object of type 'DapperRow", the cause for this is that the return type of FirstOrDefault() dynamic. public Customer FindById( int Id) { return this ._db.Query( "SELECT * FROM Customer WHERE CustomerID=@Id" , new { Id = Id }).FirstOrDefault(); } In order to solve this error, you have several options. One is to use Query.<TReturn>() instead of Query() wherein you can explicity specify the type. public Customer FindById(int Id) { return this._db.Query<Customer>( "SELECT * FROM Customer WHERE CustomerID=@Id" , new { Id = Id }).FirstOrDefault(); } Another option is to modify the code with issue, that is to store the result of the query in a dynamic variable and then assign the dynamic properties value to the class properties. public Customer FindById( int Id) { dynamic customerRecord = this ._db.Query( "SELECT * FROM Customer WHERE CustomerID=@Id&qu

Using Dapper ORM in ASP.NET Web Forms (Visual Basic.NET)

Image
Hi, This is a conversion of this post Using Dapper ORM in ASP.NET WebForm to VB.NET language. Customer.vb Public Class Customer Public Property CustomerID() As Integer Get Return m_CustomerID End Get Set (value As Integer ) m_CustomerID = Value End Set End Property Private m_CustomerID As Integer Public Property CompanyName() As String Get Return m_CompanyName End Get Set (value As String ) m_CompanyName = Value End Set End Property Private m_CompanyName As String Public Property Address() As String Get Return m_Address End Get Set (value As String ) m_Address = Value End Set End Property Private m_Address As String Public Property City() As String Get Return m_City End Get Set (

Using Dapper ORM In ASP.NET Web Forms

Image
Hello, This is a simple tutorial of using Dapper Micro ORM in an ASP.NET Webform application. According to Wikipedia, Dapper is an object-relational mapping (ORM) product for the Microsoft .NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks. The key feature of Dapper is performance as presented in Dapper website (github). It is second to Hand coded SQLDataReader class when querying specific number of records. To get started with, create an Empty ASP.NET WebForm project and then add the Dapper to our project via NuGet. Make sure to alter the connection string in Web.Config to point to your database. Then we need to add one interface and three classes in our Models folder which applies the idea of Repository. Customer.cs public class Customer { public int CustomerID { get ; set ; } publ

ASP.NET Web Forms Multiple Server Validation Controls Wide Space Issue

Image
Good day! When working with multiple server validation controls for one input control, you often encountered wide space gap if the validation controls are placed inside a single container such as div or td such as the sample screenshot below. The solution to remove the wide gap is to set the Display property of the validation controls to Dynamic. <td> <asp:TextBox ID= "txtCreditLimit" runat= "server" Width= "150" /> <asp:RegularExpressionValidator ValidationGroup= "valCustomerEntry" Display= "Dynamic" ID= "regexpName" runat= "server" ErrorMessage= "The value entered is not valid" ControlToValidate= "txtCreditLimit" ValidationExpression= "\d+(\.\d*)?|\.\d+" ForeColor= "Red" /> <asp:RequiredFieldValidator ValidationGroup= "valCustomerEntry" Display= "Dynamic" ID= "RequiredFieldValidator6" runat= "ser

Serialize .NET Classes With Inheritance To XML In C#

Good day! Here's a simple example on how to Serialize .NET classes that applied the concept of Inheritance to XML. Given the model classes below: [Serializable] public class Employee { public int EmployeeId { get ; set ; } public string Name { get ; set ; } public string Address { get ; set ; } } [Serializable] public class Utility : Employee { public string Category { get ; set ; } } [Serializable] public class Supervisor : Employee { public int OverrideCode { get ; set ; } } The code to populate and serialize the objects to XML is presented here using XmlSerializer class: List<Employee> employees = new List<Employee>(); Supervisor supervisor1 = new Supervisor(); supervisor1.Name = "Michael" ; supervisor1.Address = "Manila" ; supervisor1.EmployeeId = 11111; supervisor1.OverrideCode = 234; employees.Add(supervisor1); Utility utility1 = new Utility(); utility1.Name = "Erick" ; utility1.Address = "Masba

$.validator.unobtrusive.adapters.addBool() Not Working In ASP.NET MVC 5 CheckBoxFor()

Image
Hello, I was testing the addBool() method of jQuery validation to a CheckBoxFor() which will prevent the form from submitting if the checkbox is unchecked. To my surprise, the JavaScript code below doesn't work. @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") <script type= "text/javascript" language= "javascript" > (function ($) { $.validator.addMethod('checkboxrequired', function (value, elem) { var $elem = $(elem); if ($elem.prop('type') == 'checkbox') { if (!$elem.prop('checked')) { return false; } } return true; }); $.validator.unobtrusive.adapters.addBool('checkboxrequired', 'required'); }(jQuery)); </script> After series of investigation, I disc

Donate