Showing posts with label Codes. Show all posts
Showing posts with label Codes. Show all posts

How to run a XAP file from desktop

Drives-Windows-Phone-Metro-icon Here’s how to run a XAP file from Windows (desktop/laptop) without the access of Windows Phone.

1. Install Windows Phone SDK

Download and install Windows Phone SDK.

2. Run “Application Deployment”

After the SDK installation, we will get access to Application Deployment app from the program files menu.

WP7ApplicationDeployment

3. Select a XAP file to simulate.

Choose your XAP file and select Windows Phone Emulator to simulate. In case you have no access to a XAP file, you may download and try a simple app i developed here.

WP7XAPSimulator

4. Click the Deploy button.

After the deployment, you can normally find the app under and application list.

WP7Menu

Read More

JMC Excel Split – Split excel worksheets into separate workbooks.

Microsoft-Excel-icon This tool helps splitting multiple excel sheets and save each of them as an independent file. It was created following a feature request in another tool that I created - JMC Excel.

If you have many excel sheets created in multiple excel files and wanted to save each of them separately as workbook (or independent .xlsx file), JMC Excel Split will come in very handy.

Below is the download link to the trial version (which can split up to 5 files and 5 sheets each).

JMC Excel Split Download Link

 

 

Want to thank me? Buy me a coffee. Paypal Donation of any amount is welcome. 

Remove limitation by Buying JMC Excel Split at USD 7 only. Contact jeeshenlee@gmail.com.

paypal-logo

Read More

How to update value of LINQ results using FOREACH loop

LINQ queries are lazy loading – that means execution of reading is delayed until it’s iterated. Once set in the LINQ Select statement, the value stored in the result are set as READ ONLY hence setting the value in a FOREACH loop will not really update its data.

For example:

1 var schedules = from roomRate in _entities.RoomRates
2 select new ScheduleViewModel
3 {
4 RoomTypeId = roomRate.RoomTypeId,
5 Rates = roomRate.Rates,
6 Selling = 0,
7 Booked = 0,
8 };
9 foreach (var schedule in schedules)
10 {
11 schedule.Booked = 1;
12 schedule.Selling = 1;
13 }
Variable ‘Schedules’ field Booked and Selling  are not updated even after the FOREACH Loop.

To update the LINQ result using FOREACH loop, I first create local ‘list’ variable and then perform the update using FOREACH Loop. The value are updated this way. The example code below exemplify my idea and i further cast it back to IQueryable :)


Hotelebuddy



1 public IQueryable<ScheduleViewModel> GetScheduleForHotelBetween(int hotelId, DateTime startDate, DateTime endDate)
2 {
3 var schedules = from roomRate in _entities.RoomRates
4 join ratesTypeEnforceDate in _entities.RatesTypeEnforceDates on roomRate.RatesTypeId equals
5 ratesTypeEnforceDate.RatesTypeId
6 where roomRate.RoomType.HotelId == hotelId &&
7 ratesTypeEnforceDate.EnforceDate <= endDate &&
8 ratesTypeEnforceDate.EnforceDate >= startDate
9 select new ScheduleViewModel
10 {
11 RatesType = ratesTypeEnforceDate.RatesType.Name,
12 Date = ratesTypeEnforceDate.EnforceDate,
13 RoomTypeId = roomRate.RoomTypeId,
14 Rates = roomRate.Rates,
15 Selling = 0,
16 Booked = 0,
17 };
18 var schedulesList = new List<ScheduleViewModel>(schedules.AsEnumerable().ToList());
19 foreach (var schedule in schedulesList)
20 {
21 schedule.Booked = BookingDetails.GetBookingCountForRoomTypeOn(schedule.RoomTypeId,
22 schedule.Date);
23 schedule.Selling = RoomForSales.GetRoomForSalesCountForRoomTypeOn(schedule.RoomTypeId,
24 schedule.Date);
25 }
26
27 return (IQueryable<ScheduleViewModel>) schedulesList.AsQueryable();
28 }

Read More

Develop web app on Azure using MVC3 + EntityFramework Database First.

Cloud-icon I was following this tutorial (.NET Web Application with SQL Azure) on Windows Azure website and realised that the tutorial were written using Entity Framework Code First approach. If you are like me - prefer Entity Framework Database First approach, below is the Entity Framework Database First version.

In this post, I will skip the details of the first few steps (setup Azure development environment, create MVC3 app, and Azure enable the app) and go straight to the Database part. You may refer to the article for the step by step guide and come back later for the Database implementation using Entity Framework Database First approach.

 

Azure + MVC3 + Entity Framework Database First Approach

1. Install Windows Azure SDK and setup development environment on Visual Studio.

2. Create ASP.NET MVC3 application.

3. Azure enable your ASP.NET MVC3 application.

4. Add new Database to our app. Right click on App_Data folder and select Add > New Item. Choose SQL Server Database and name it ToDoList.mdf.

AddDatabase

5. Double click on the created database (ToDoList) to add ToDoItem table to the database. Right click on Tables > Add New Table.

AddTable

6. Edit database table to include  these fields – Id, Name, IsComplete. Make sure you set the Id field as primary key and set the Identity Specification. Hit Save button and enter “ToDoItem” as the database table name.

AddDatabaseTable

7. Add data model. Right click on “Model” in the solution explorer and then Add New Item > ADO.NET Entity Data Model >  name it “ToDoListModel.edmx”. Choose "Generate from Database” option. Select “ToDoList.mdf” connection string. Click “Next”.

DatabaseConnectionString

8. Choose to include “ToDoItem” database object.

ChooseDatabaseItem

9. Create Repository Class for ToDoItem to refactor all the data access logic on ToDoItem table. You may refer to this article on why Repository Pattern is good for data access  logic implementation. Basically, Repository Pattern refactor all the data access logic to one class so it’s easier to maintain and test. Right click on Models > Add > Class > ToDoItemRepository.cs. Below is the complete implementation of my ToDoItemRepository class.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
 
namespace ToDoListApp.Models
{
    public class ToDoItemRepository
    {
        private readonly ToDoListEntities _entities = new ToDoListEntities();
 
 
        //
        // Query Methods
 
        public ToDoItem GetTDI(int id)
        {
            return (from tdi in _entities.ToDoItems
                    where tdi.Id == id
                    select tdi).FirstOrDefault();
        }
 
        public IQueryable<ToDoItem> GetAllToDoItems()
        {
            return from tdi in _entities.ToDoItems
                   select tdi;
        }
 
 
        //
        // Update/Add/Delete Methods
 
        public void Update(ToDoItem tdi)
        {
            _entities.ToDoItems.Attach(tdi);
            _entities.ObjectStateManager.ChangeObjectState(tdi, EntityState.Modified);
        }
 
        public void Add(ToDoItem tdi)
        {
            _entities.ToDoItems.AddObject(tdi);
        }
 
        public void Delete(ToDoItem tdi)
        {
            _entities.ToDoItems.DeleteObject(tdi);
        }
 
 
        //
        // Save
 
        public void Save()
        {
            _entities.SaveChanges();
        }
 
 
        // Dispose
 
        public void Dispose()
        {
            _entities.Dispose();
        }
    }
}





10. Recreate the HomeController to show all the ToDoItems. Right click on Controllers > Add > Controller > HomeController.cs.


AddHomeController


AddHomeControllerDetails


11. Modify the generated HomeController.cs to use Repository class.


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToDoListApp.Models;
 
namespace ToDoListApp.Controllers
{ 
    public class HomeController : Controller
    {
        private readonly ToDoItemRepository _tdiRepository = new ToDoItemRepository();
 
        //
        // GET: /Home/
 
        public ViewResult Index()
        {
            var tdis = _tdiRepository.GetAllToDoItems();
            return View(tdis.ToList());
        }
 
        //
        // GET: /Home/Details/5
 
        public ViewResult Details(int id)
        {
            var todoitem = _tdiRepository.GetTDI(id);
            return View(todoitem);
        }
 
        //
        // GET: /Home/Create
 
        public ActionResult Create()
        {
            return View();
        } 
 
        //
        // POST: /Home/Create
 
        [HttpPost]
        public ActionResult Create(ToDoItem todoitem)
        {
            if (ModelState.IsValid)
            {
                _tdiRepository.Add(todoitem);
                _tdiRepository.Save();
                return RedirectToAction("Index");  
            }
 
            return View(todoitem);
        }
        
        //
        // GET: /Home/Edit/5
 
        public ActionResult Edit(int id)
        {
            var todoitem = _tdiRepository.GetTDI(id);
            return View(todoitem);
        }
 
        //
        // POST: /Home/Edit/5
 
        [HttpPost]
        public ActionResult Edit(ToDoItem todoitem)
        {
            if (ModelState.IsValid)
            {
                _tdiRepository.Update(todoitem);
                _tdiRepository.Save();
                return RedirectToAction("Index");
            }
            return View(todoitem);
        }
 
        //
        // GET: /Home/Delete/5
 
        public ActionResult Delete(int id)
        {
            var todoitem = _tdiRepository.GetTDI(id);
            return View(todoitem);
        }
 
        //
        // POST: /Home/Delete/5
 
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var todoitem = _tdiRepository.GetTDI(id);
            _tdiRepository.Delete(todoitem);
            _tdiRepository.Save();
            return RedirectToAction("Index");
        }
 
        protected override void Dispose(bool disposing)
        {
            _tdiRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}

12. Login to https://windows.azure.com/. Add SQL Database on Azure. Database > Subscription > Create > Choose Server Region > Key in username and password >


AddSQLDatabaseOnAzure

Read More

Can’t find ADO.Net DbContext Code Generator for Entity Framework

I was following this Entity Framework Database First tutorial and couldn’t find the DbContext code generator shown in the video.

Can’t-find-ADO.Net-DbContext-Code-Generator-for-Entity-Framework

Here’s how to install ADO.Net DbContext Code Generator to Visual Studio:

1. Click on the “Online Templates” section.

2. Search for “ADO.Net DbContext Generator”.

Install-ADO.Net-DBContext-Code-Generator

3. Click “Add” and proceed with installation.

 

Embed below is the video tutorial that I'm referring to.

Read More

GetType() vs typeof() in C#

question-type-one-correct-icon‘typeof()’ is actually an operator for ‘GetType()’, so both function are same. Both returns an object ‘Type’ which can be used to find out further information like properties, methods and etc.

  1: Type typeObj = obj.GetType();  // Return the same result like typeof()
  2: Type typeObk = typeof(obj);    // Return the same result like .GetType()
Read More

Difference between ‘string’ and ‘System.String’ in C#

Programming-icon‘string’ is an alias (or shorthand) of System.String. That means, by typing ‘string’ we meant System.String. As pointed out by Jeffrey Richter (in his book CLR via C#, page 119), it’s as if every C# program (in Visual Studio) contained ‘using’ directives like the following:

1 using string = System.String;
2 using char = System.Char;
3 using int = System.Int32;
4 using double = System.Double;
Read More

Why I keep getting the wrong floating point result in C#

Something that took me while to figure out.

1 double result = 1 / 3; //will return 0.
2 double resutt = 1 / 3.0; // will return 0.33333
3

source-cs-iconEver wonder why I'm getting 0 (in Line 1) while i have already assign it to double type? Here’s the explanation: Line 1 return 0 as a result because C# interprets numeric value as integer, and integer division is performed with truncation; hence, 0. Line 2 is interpreted as floating point calculation because of 3.0 (putting a decimal point for the calculation to be done in floating point).

Read More

My new blog–Connecting the dots…

JasonHave decided to buy myself a domain for a long time. Finally, i made it. Winking smile  You can find my previous blog here – jeeshenlee.wordpress.com (yes, i will not longer post there but i do drop by occasionally to answer comments).

So, this is my new personal blog where i share stories about my entrepreneurship journey, codes that i wrote at work and circuits that i build at spare time.

I will try to keep it simple and manageable by posting with different tags:

  • Venture: about my entrepreneurship journey.
  • Codes: obviously, about programming codes. I’m a .NET developer.
  • Circuits: about electronics and circuits that i built at spare time.
  • Downloads: i do write small apps and made it available for downloads.
  • Ramblings: notes that are important to me but not others.

Well, that’s all for my first post – connecting the dots and setting up a new path ahead.

Read More