Posts

Donate

Navigation Properties In Entity Framework Using Database First Approach

Image
Good day! Here's a simple step by step tutorial on exploring the Navigation Properties of EF using the DB approach. According to MSDN , Navigation properties provide a way to navigate an association between two entity types. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). You may also choose to have one-way navigation, in which case you define the navigation property on only one of the types that participates in the relationship and not on both. Given the description, this example demonstrates the concept using two tables Employees and Dependents wherein you search for a particular employee and you can access the related dependents of that employee. To start with, just perform the steps given below. Step 1. Cre...

Pivot DataTable Using LINQ In C# And VB.NET

Hello, A question was brought up in the forums on how to Pivot a DataTable object here. The OP has already a solution with reference to this link Cross Tab / Pivot from Data Table . An alternative solution is to utilize the features of LINQ using group by statement to achieve the desired output. This solution consists of few lines of code compared with the solution from the forum post. C# Code var query = ( from students in dt.AsEnumerable() group students by students.Field< string >( "StudID" ) into g select new { StudID = g.Key, Eng = g.Where(c => c.Field< string >( "SubSht" ) == "Eng" ).Sum(c => c.Field< double >( "Score" )), Fre = g.Where(c => c.Field< string >( "SubSht" ) == "Fre" ).Sum(c => c.Field< double >( "Score" )), Mat = g.Where(c => c.Field< string >( "SubSht" ) == "Mat" ).Sum(c => c.Field< double ...

Function <Anonymous Method> Doesn't Return A Value On All Code Paths (Action Statement)

Hello, Given the code statement below, the code throws an exception such as anonymous method does not return a value on all code paths. If txtEmployeeHireDate.IsHandleCreated Then If txtEmployeeHireDate.InvokeRequired Then BeginInvoke( New Action(Function() txtEmployeeHireDate.Text = objEmployee.HireDate End Function )) End If End If The code that's inside the BeginInvoke() statement does not necessarily returns a value since I only assigned an object's property value to the textbox control. In order to resolve this, either return a null value or a false value inside the Action delegate. If txtEmployeeHireDate.IsHandleCreated Then If txtEmployeeHireDate.InvokeRequired Then BeginInvoke( New Action(Function() txtEmployeeHireDate.Text = objEmployee.HireDate Return Nothing 'added this statement to fix issue End Function )) End If End If Another solution is to use Sub() which does not return a value instead of Function(...

Calling Web API not working in AngularJS using $http service

When calling ASP.NET Web API service inside the solution, I encountered an issue that is 404 not found. Given that this issue persists, I tried adding a forward slash before the url in the Ajax call which works. AngularJS $http({ //url: "EmployeeRoute/GetAll", //404 error url: "/EmployeeRoute/GetAll" , dataType: 'json' , method: 'POST' , data: GetAll, headers: { "Content-Type" : "application/json" } }).then( function (resp) { if ( typeof resp.data === 'object' ) { return resp.data; } else { return $q.reject(response.data); } }, function (err) { return $q.reject(err.data); }); I also make sure that the WebApiConfig.Register method gets executed in Global.asax.cs. Global.asax.cs protected void Application_Start( object sender, EventArgs e) { GlobalConfiguration.Configure(WebApiConfig.Register); }

AngularJS $http service returns html instead of JSON string

Good day! I've been trying to consume an ASP.NET Web Method using AngularJS $http service and all I get from the response is the html page source instead of string data. After investigating and doing some searching, the workaround is to set the responseType to json and pass an empty data to the Web Method given that the Web Method has no parameter. var myapp = angular.module( 'myApp' , []); myapp.controller( 'ctrl' , function ($scope, $http) { $http({ url: "63MakeAjaxCallAndReturnJSONWebService.aspx/HelloWorld" , dataType: 'json' , method: "POST" , responseType: 'json' , data: {}, headers: { "Content-Type" : "application/json;" } }).then( function (response) { $scope.value = response.data.d; }); }); And also declare the Web Method as static. [WebMethod()] public static string HelloWorld() { return "Hello World!" ; }

ASP.NET 4.5 Has Not Been Registered on the Web Server (Visual Studio 2012)

This issue "ASP.NET 4.5 Has Not Been Registered on the Web Server" occurred when I opened an asp.net application built with Visual Studio 2012 recently when I installed Visual Studio 2015 in my laptop with a Windows 8 operating system. Given that this happens I assume that some settings may have been updated or corrupted by the recent version of VS. The solutions I have tried included registering asp.net through Visual Studio command tools with no effect. After searching through the web, I found a solution which is to download the hotfix for VS 2012 here Unexpected dialog box appears when you open projects in Visual Studio 2012 after you install the .NET Framework 4.5.3 . After downloading and installing the hotfix, the issue was resolved.

Custom DateTimePicker Control With Background Color And Icon In Windows Forms

Image
Hello Here's a custom DateTimePicker with background color and image icon instead of using the ComboBoxRenderer class. The icon is an image that is added to the project as part of it's resource. The adding of icon is processed through the WndProc method while the setting of background color is handled in the OnPaint() event. Notice that in the constructor, the SetStyle()'s parameters are ControlStyles.UserPaint so that the control paints itself and true to apply the specified style to the control. public class DateTimePickerWithBackground : DateTimePicker { private Bitmap _bmp; enum BorderSize { One = 1, Two = 2 }; public DateTimePickerWithBackground() { _bmp = new Bitmap(ClientRectangle.Height, ClientRectangle.Width); this .SetStyle(ControlStyles.UserPaint, true ); } protected override void WndProc( ref Message m) { base .WndProc( ref m); if (m.Msg == 0xf) //WM_PAINT message { Graphics g = Graphics.FromHwnd(m.HWnd); g.Dr...

Load Images in a Windows Form Custom Control

Image
Good day! Often times when developing custom controls you will add images or icons to enhance the UI through the Bitmap class. But when getting the image from file using Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\calendar.png") you may encounter issues such as "path is null" since the path pointed by the BaseDirectory isn't the executable path. A simple workaround is to set the path of the image using hardcoded like Image.FromFile(@"C:\Images\ProjectX\calendar.png") . While this is acceptable, it is ugly to look at and may cause potential issues once the image has been transferred to another location. An accepted solution is to add the image as a project resource then reference it in the custom control code. To accomplish that, here are the steps. * Right Click on the Project -> Properties * In Resources, select Add Resource Dropdown -> Add Existing File - Choose the image or icon to be used as resource. Once done, it wil...

MVVM Basics With TextBlock Control

Hello, This post is based on the article Understanding the basics of MVVM design pattern . The author demonstrated the basics of MVVM using TextBlock controls. However, the code samples have several issues and in order for the sample application to work, I revise them with the following changes. BindableBase.cs - since SetProperty method uses T in it's parameter, you also need to reference T in your classname. public class BindableBase <T> : INotifyPropertyChanged { ... } MainPageViewModel.cs - update the code in the constructor to bind a single object to the TextBlock controls. public class MainPageViewModel : BindableBase<Book> { private Book _book; public Book Book { get { return _book; } set { SetProperty( ref _book, value ); } } public MainPageViewModel() { Book = new Book() { Title = "Harry Potter" , Author = "J. K. Rowling" , Category = "Young-adult fiction" ...

Export ASP.NET GridView To Excel And Preserve Leading Zeros

Image
Hi, There was a question raised on how to export data from ASP.NET Gridview to Microsoft Excel and preserve leading zeros of numeric values. A workaround that we have is to insert an   character before the cell value if that value starts with zero (0). So if the value starts with zero such as 0001, the result would be " 0001". Thus when exported to Excel, the leading zero is kept. cell.Text = string .Format( "&nbsp;{0}" , cell.Text); A suggested solution is to insert a Tab character ( ) but this fails on some occasions. After doing some research, replacing the tab character with &ensp; or &emsp; would also fix the issue. cell.Text = string .Format( "&ensp;{0}" , cell.Text); //or cell.Text = string .Format( "&emsp;{0}" , cell.Text); ASP.NET GridView Export Excel Report Cheers!

Donate