Skip to content

Retrieving images from a dabatase in ASP.NET MVC

If you run into a ASP.NET MVC site that needs to get images stored as binary data in a SQL database you can do the following to help you read these images and display them in your view.

The Controller

First, create a controller that will serve these images and add a void method that requires the image id as parameter to locate the right image to serve. In the example below I used the Adventure Works database and Linq to query the database and read an image stored in MS SQL. Make sure you set the content type to the right type of image file you’ll be getting from the database, in the example below I used “image/gif” as the content type.

The View

In your view, you only need to use the Url.Action helper to form the url to the image stored in the database by calling the method in the image controller you just created (see above).

“Show” is the name of our method and “Image” is the name of the controller, what’s on the right to “id=” is just the image id that I want to get.

 

 

 

 

Infinite scroll with ASP.NET MVC

An infinite scroll is a nice solution when you need to display large amounts of content in page, it helps by increasing performance in such a page because only a specific number of items is shown when the page first loads. As the user scrolls down, more content is shown. An infinite scroll is a better solution than having a paginated view of the page which usually breaks the flow of the page by splitting the content into multiple pages and then users have to click on a button or link to be able to see the next group of items…

I’ve used the following infinite scroll solution with ASP.NET MVC sites and it works great and it is simple to implement, all you need is jQuery and a little code in the controller and the view.

First, add a parameter to the controller action that returns the data for your page, this parameter is the one that we’ll use to specify the page number we need to get data for. The real work happens in the GetPaginatedProducts method, every time the user scrolls down the page this method is called by the controller action, we pass the page number and then use the Skip Linq command to get the next set of items.

The Controller

Here is the code we need in the controller for the infinite scroll to work:

controller

The JavaScript

The following is the javascript needed to display a loading image and to initiate the call to the Product action on the HomeController, see below. Also, you will need to create a loading image to display while the application is getting data from the server, I used this site to create mine.

javascript

The View

Finally, in the view we make sure we have at least two divs, one with the id “productList” where the data is appended to when scrolling to the bottom of the page and another one with the id “loading” to use it to display the loading image:

view

Where is the sample project?

I’ve added the sample project to CodePlex, you can download it here: https://github.com/ricardodsanchez/InfiniteScroll

Making progress, slowly but surely

I can’t believe it’s been about a year since I started with the idea of TidyContact, time really flies, especially when you have a busy life full of projects, kids, family, etc, and I am blessed for that. So far I have learned lots of things building TidyContact, most of the things I’ve learned are related to the web framework I am using (ASP.NET MVC), and the service oriented architecture I am implementing with this project. Yes it is taken longer than I expected and I know this goes against the Lean Startup methodology, but frankly, I do not care since what I am doing is fun (to me at least) and it is also a way for me to learn about third-party apis, frameworks, databases, etc… it is geek fun.

During this time I have been able to collect about 60 names of people who are interested in TidyContact, and about half of them showed enough interest to actually pay a monthly subscription for it, that is good. It is truly difficult to develop a product from scratch without any help from other developers, designers, etc… at the same time, it is rewarding every time I reach a milestone such as completing a feature, finishing a module, getting the landing page done, etc… I am looking forward to launching the beta version soon and hope to get valuable feedback to make the application better before the first version launches. I would say I am about 70% done with the version I want to release to beta users, I believe in releasing a minimum viable product and all that stuff, but since this application is something we’ll be using internally in TodoTax.com it won’t make sense to release it if it doesn’t do at least what we want it to do.Continue Reading →

Installing Windows Home Server (WHS) Connector in Windows 7 64-bit

I bought a Windows Home Server about two years ago, I use it primarily for backups and file sharing in my home network. It is a good solution for a small home network such as mine because it is sufficient, it is not overkill and it is easy to manage.

A few months ago, I put together a new PC which I use for development when I am home and for gaming. I installed Windows Ultimate 64-bit in it and up until today, I didn’t worry about connecting it to the WHS because I didn’t have much to backup, now I do.Continue Reading →

NuGet, The Package Management System for the .Net Platform

Update – 2019

NuGet is now part of Visual Studio and some of the information described here does not apply anymore.

NuGet is a package management system for the .NET platform, it is supposed to simplify the process of incorporating third-party libraries into a .NET project during development. Basically, it is a great way to add (and update) oopen-sourcelibraries and the like to your .NET project. The first version of NuGet was released in October 2010 and it was originally named NuPack.

How to get it

NuGet can be installed from here or from Visual Studio’s Extension Manager located under the Tools menu.

The awesomeness of it

Before NuGet, the way we incorporated third-party libraries into our projects was by doing the following:

  1. Download third-party library (and any dependencies) from the web.
  2. Unpack them (as many of them come in compressed files) to a folder in our project, I usually create (or created) a folder labeled “lib” and placed all the third-party libraries there.
  3. Import to the .NET project.
  4. Make any necessary changes to the config file when needed.
Too much work right? With NuGet, you basically open your project in Visual Studio and type a command in the new Package Manager Console. For example, if you wanted to add NUnit to your project, you’ll type the following command:
Install-Package NUnit

Continue Reading →

MIX11 – A tribute to Microsoft’s Web Stack of Love!

A few weeks ago I attended MIX11 in Las Vegas. MIX is an annual event where Microsoft showcase their new web technologies. This year it was all about new versions of existing products, such as Windows Phone 7, Entity Framework, Silverlight, ASP.NET MVC, IIS, etc… Below is a summary of what was shared on both keynotes:

  • Mango: code name for coming version of Windows Phone 7 (WP7).
  • ASP.NET MVC 3 Tools update
  • Entity Framework 4.1
  • Silverlight 5 Beta
  • NuGet
  • IIS Express
  • SQL Compact Edition 4
  • Internet Explorer 10 Preview
There were many sessions available, some of them really good and some others were not. One thing that I did appreciate was the endless supply of coffee, water and other drinks. Snacks were available as well between breakfast and lunch which were also provided by Microsoft.
All sessions are now available online as well as both keynotes, check them out!
As a software developer, the most interesting for me was the ASP.NET MVC 3 tools update, EF 4.1.Continue Reading →
1 16 17