Posts

Donate

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

How To Upload And Publish Visual Studio 2012 Project To GitHub

Image
Hello, In this tutorial, I will demonstrate on how to upload and publish a Visual Studio 2012 project to GitHub. Steps 1 and 2 are needed because editions of Visual Studio (2013 and 2015) has built-in extension for Git. So to start with, perform the detailed steps below. 1. Close all instance of Visual Studio. Download and Install Visual Studio 2012 Update 4 (Latest Update) 2. Download and Install Visual Studio Tools for Git. (Search in marketplace.visualstudio.com) 3. Create a sample Repository. (Uncheck Initialize this repository with a README) 4. Copy the url generated by the repository with .git extension. This will be used when you publish the project. 5. Open Sample project to be committed. Right click on the solution and choose Git instead of Team Foundation Version Control. 6. Open Team Explorer via View Menu (View-> Team Explorer). 7. Click Home (Home icon) and then choose Changes. This will open up the project in which you can select files to be Included o

Github Repository (DotNetGenetics)

Hi! Today, I just created a repository account in GitHub .NET Genetics . I'll be adding some snippets and projects posted in this blog or in the .NET Community VBForums. Cheers! :-)

AllowUsersToAddRows In DataGridView Not Working If DataSource is List<T>

Greetings! Going back to a previous project of mine, I found out that some DataGridView control's DataSource where set using List<T> and as a result, prevented the users to add new data to the DataGridView. private void List() { List<Item> list = new List<Item>(); for ( int i = 0; i < 100; i++) { list.Add( new Item() { ID = i, Name = String.Format( "{0}:{1}" , "Test" , i.ToString()) }); } DataGridView1.DataSource = list; } The common solution is to use DataTable as the DataSource but if we want to use a List type object, we could use BindingList<T> or BindingSource. Both of these have AllowNew property which indicates that you can add items to the list using the AddNew() method. It is stated in DataGridView.AllowUsersToAddRows that " If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNew property are set to true. ".Well, Lis

Unexpected character encountered while parsing value: C. Path '', line 0, position 0. (Deserialize JSON String Error In C#)

Hi, A common issue when deserializing a JSON object in C# is to pass the filepath of the JSON file to the Deserialize() method instead of the string content of that file such as the code below. var obj = JsonConvert.DeserializeObject<Employee>( @"D:\Data\Employee.json" ) When calling the Deserialize() method, you need to make sure that the parameter passed is a JSON string value instead of the file path. Use StreamReader class or File.ReadAllText() to get the content of the JSON file. using (StreamReader reader = new StreamReader( @"D:\Data\Employee.json" )) { string json = reader.ReadToEnd(); var obj = JsonConvert.DeserializeObject<Employee>(json); }

Exclude Property Mapping In Entity Framework Code First Approach

Hi all, Given a database table such as User with fields Id, UserName and Password that will be mapped to a class using Entity Framework called User with an additional field of ConfirmPassword. public class User { public int Id { get ; set ; } public string UserName { get ; set ; } public string Password { get ; set ; } public string ConfirmPassword { get ; set ; } } I would like to exclude the ConfirmPassword field from being mapped to the User table. So, after doing some searching, the solution is to decorate the property with [NotMapped] attribute and make sure to reference the DataAnnotations namespace ( System.ComponentModel.DataAnnotations.Schema ). using System.ComponentModel.DataAnnotations.Schema; public class User : IEntity { public int Id { get ; set ; } public string UserName { get ; set ; } public string Password { get ; set ; } [NotMapped] public string ConfirmPassword { get ; set ; } }

Update DataTable Values Using LINQ In C# And VB.NET

Image
Hello, Here's how to update DataTable value(s) using Method or Query syntax in LINQ. C# Code private void Form1_Load( object sender, EventArgs e) { DataTable dt = new DataTable( "tblEntTable" ); dt.Columns.Add( "ID" , typeof ( string )); dt.Columns.Add( "Amount" , typeof ( decimal )); dt.Rows.Add( new object [] { "1" , 100.51}); dt.Rows.Add( new object [] { "2" , 200.52}); dt.Rows.Add( new object [] { "3" , 500.24}); dt.Rows.Add( new object [] { "4" , 400.31}); dt.Rows.Add( new object [] { "5" , 600.88}); dt.Rows.Add( new object [] { "6" , 700.11}); //QuerySyntax(dt); MethodSyntax(dt); } private void QuerySyntax(DataTable dt) { var result = from row in dt.AsEnumerable() select new { ID = row.Field< string >( "ID" ), Amount = 900.23 }; DataGridView1.DataSource = result.ToList(); } private void MethodSyntax(DataTa

Donate