Sunday, April 24, 2016

Cascading dropdown with ASP.NET Web Forms and Entity framework

This post may be pretty basic for many people. But I have been getting this request over and over so I thought it will be a good idea to write a blog post about it.

In this blog post, We are going to learn how we can create the cascading dropdown with ASP.NET Web forms and Entity framework. To create an application first we need to create web application like following.


After creating a web application like following. It’s time to create model classes for our application. Here to illustrate the example, We are going to use Standard and Student models. Multiple students can be there in a standard. So we are going to have two dropdowns Standard and Student. Once you change select standard based on that student dropdown will be filled.

Here is code for Standard Student Model class:
using System.Collections.Generic;

namespace CascadingDropdownEF.Models
    public class Standard
        public int StandardId { get; set; }
        public string Name { get; set; }

        public virtual  ICollection<Student> Students { get; set; }
And following is the code for the Student model.
using System.ComponentModel.DataAnnotations.Schema;

namespace CascadingDropdownEF.Models
    public class Student
        public int StudentId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int StandardId { get; set; }
        public Standard Standard { get; set; }
Now once we have our model ready it’s time to create our Entity framework context class. We are going to use Entity code first model here . So here is the code for the Entity framework context.
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace CascadingDropdownEF.Models
    public class StudentContext : DbContext
        public StudentContext() : base("name=StudentConnectionString")
        public DbSet<Standard> Standards { get; set; }
        public DbSet<Student> Students { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
Here I have created two DbSet for students and standards. Also in model creating event I have removed plural names so when entity framework create tables when we run this application it will remove plural names from the table.

Creating Web Forms and using Entity framework for cascading dropdowns:

Now we have our database layer or operation ready. it’s time to create a new Web Form which will have Standard and Student drop-down.


After creating Web Form I have written following HTML code in aspx file.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CascadingDrodownDemo.aspx.cs" Inherits="CascadingDropdownEF.CascadingDrodownDemo" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title>Cascading dropwon demo</title>
    <form id="form1" runat="server">
            <asp:DropDownList runat="server" ID="ddlStandard" CssClass="dropdown" AutoPostBack="true" OnSelectedIndexChanged="ddlStandard_OnSelectedIndexChanged"/>
            <asp:DropDownList runat="server" ID="ddlStudent" CssClass="dropdown"/>

You can see that I have created two ASP.NET dropdowns Standard and Student and Also for Standard dropdown I have autopostback=”true” and selected index change event.
On the aspx.cs file i have written the following code.
using System;
using System.Linq;
using CascadingDropdownEF.Models;

namespace CascadingDropdownEF
    public partial class CascadingDrodownDemo : System.Web.UI.Page
        private readonly StudentContext _studentContext;

        public CascadingDrodownDemo()
            _studentContext = new StudentContext();

        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)

        private void BindStudents(int? standardId=null)
            if (standardId != null)
                var students = from student in _studentContext.Students
                    where student.StandardId == standardId.Value
                    select new
                        StudentId = student.StudentId,
                        Name = student.FirstName + " " + student.LastName

                ddlStudent.DataSource = students.ToList();
                ddlStudent.DataTextField = "Name";
                ddlStudent.DataValueField = "StudentId";

            ddlStudent.Items.Insert(0,"--Select Student--");

        private void BindStandards()
            var standards = _studentContext.Standards.ToList();
            ddlStandard.DataSource = standards;
            ddlStandard.DataTextField = "Name";
            ddlStandard.DataValueField = "StandardId";

            ddlStandard.Items.Insert(0,"--Select Standard--");

        protected void ddlStandard_OnSelectedIndexChanged(object sender, EventArgs e)
Here You can see that I have created two method BindStudent and BindStarnds. In Bindstandards it fetches all the standards and bind it to Standard dropdown . In BindStudents method, I have used a nullable parameter standardId which will be null by default. So If standard id is not provided as you can see it will only insert default item.  Now in page load method, I have called this two methods. Also, you can see that I have written Standard event dropdown selected index change event which will pass standard value.

Now everything is ready. I have inserted following data in Standard and Student table.


And student table like following.


Now when you run the application. It will look like following.


That’s it. Hope you like it. Stay tuned for more.
You can find complete source code of this application at following location on github-
Friday, April 22, 2016

DocumentDB data in Visual Studio with cloud explorer

Recently, During Global Azure Boot camp presentation , One offline question was asked that how we can see and browse the data from DocumentDB. So I thought there will be lots of developers who might have the same question that where I can see data from DocumentDB in visual studio.

Yes you can see your DocumentDB data into visual studio. There is an extension called Cloud explorer from where you can browse all the services of Azure. With Visual Studio community edition its comes inbuilt But If you have not that installed then you can install via following steps.

How to install Cloud explorer in Visual Studio:

Open visual studio and go to Extensions and updates like below.


Once you click on that it open a window loading all the extensions select online and type cloud in the search box and you will able to see cloud explorer like below.


I have Cloud explorer installed in my Visual studio that’s its showing Green tick otherwise it will show download button there and you can download and install it.

You can also download manually from the following link.

For Visual Studio 2015:

For Visual Studio 2013:

How to use Cloud explorer to see data of DocumentDB:

One you are done with installing Cloud explorer, You can load that from View Menu-> Cloud explorer in Visual Studio. It will load like below.


Here I have already given my Azure Credentials If you have not given that it will ask for credentials and then will load this screen.

Here it will show the list of all services available for storage. To see what is there in our DocumentDB account click on DocumentDB Account.  It will show your account name lie below.


One you click on the database name it will show all the collection and record below.


One you select records(JSON documents) and right click. It asks for you to open in the editor like below.


Once you click on open editor, It will ask open that JSON document like below.


So you can see that it's very easy to browse data of DocumentDB in cloud explorer in Visual Studio itself. Hope you like it. Stay tuned for more!.
Wednesday, April 20, 2016

Visual Studio code 1.0 Released

Microsoft has released visual studio code on 1.0 version 14th April 2016. Those who have not used Visual Studio code, Visual studio code a is cross platform open source editor from Microsoft. It supports the variety of languages and you can do almost everything that you can do in an IDE. It also provides support for the debugging few languages.

In blog of Visual Studio Code, Microsoft says
Since our initial launch one year ago, 2 million developers have installed VS Code,” the Visual Studio Code team revealed. “Today, we’re excited to report that more than 500,000 developers actively use VS Code each month.
Image source: Visual Studio code blog.

There is some important milestone that Visual studio code is received like following.
  • Visual studio code is now fully localizable and supports 9 languages
  • Visual studio supports extensions and community has built more than 1000 extensions in such a sort span
  • It now supports syntax highlighting and few more features for more than 100 languages.
  • It got more than 300+ pull request on Github.
It will be interesting to see how Visual Studio Code move ahead. I have used it for many times and I am quite happy with it.

You can find more information about it at the following link.

If you have not used it then I recommend you to download this and try it from the following location.

That’s it. Hope you like it. Stay tuned for more!
Sunday, April 17, 2016

Global Azure Bootcamp 2016 Ahmedabad–Awesome event recap

Yesterday, Ahmedabad User group has organized the Global Azure Bootcamp 2016 event. It was a fun to be there. The event was very well received and attended by the group of people excited about Azure cloud platform.  Thanks for hosting is Ahmedabad User Group.


Event Recap:

In the morning, Event is started around 10:00 am. Mahesh had made all attendees comfortable and given some insights about Azure platform.


After that, It was my time to present some. I have shared my knowledge about DocumentDB with my presentation about Introduction to DocumentDB.


There well lots of interest in the audience and we had lots of conversation about it. Here is the presentation link for the same.

Here was the agenda for my presentation.

  • What is document databases?
  • What is DocumentDB?
  • Why we should use DocumentDB?
  • How we can use DocumentDB?
You can also find demo code for the presentation at the following location.

Due to the huge interest in people, We promise them to have some advanced level sessions about DocumentDB in forthcoming months.

After that Jagdish has taken over the stage and had presented about Azure mobile services. He presented how we can leverage Azure mobile services with Windows Phone Application and some other cool insights for Azure mobiles services.


Then we had lunch and we had some great interaction and conversation about cloud technologies during lunch.


After lunch, there was post lunch session. It was time for Sanket Shah to rock the stage. He has presented about Architecting Modern solution on Azure.  He had some cool demo and audience was amazed about his demo.


After that, we had another back to back session from Kaushal Bhavsar. He had presented about how we can create a secure two factors authentications with  Azure technologies. He had some given some cool demo using Azure Active directory and Office 365 account authentication about it.


After that we had tea and It’s time for Ahmedabad User group president Mahesh Dhola to rock the stage. Instead of giving demo he chooses to use WhiteBoard and given presentation DevOps and Microsoft Azure. It was fun watching is him presenting the topic that we all love.


After the presentation, it was the time where everybody wants to get some prize. We had the lucky draw and almost everybody gets price.


And this guy was an inspiration for all us.


Then we has some photos of all the speakers. We missed Jagdish here as he needs to go home for some personal work.


Overall, It was a fun event. We had some much fun hacking and learning something new. Thank you, Microsoft and Ahmedabad user group for hosting such a nice event.

Keep rocking!.
Thursday, April 14, 2016

Visual Studio 15 Next version of visual studio is available now

With Build 2016 event, Microsoft has announced the preview of next version of Visual Studio called Visual Studio 15. You can download the next version of VS from below link.

There are two options available for there on above link for download. One is normal Installed which is traditional installer contains all the functionality of earlier version of visual studio.

The second option contains a new lightweight installer which a brand new install from the Microsoft focusing on making visual studio installation faster and-and less impactful. The smallest installation of the Visual studio is now under 500 MB and includes support for more than 20 languages. You can optionally add additional features like desktop development using  Windows forms and WPF.

P.S. This is a preview still Microsoft is working on that so you will not see lots of new features and its in preview so you should never use it for production.
There are some new features announced like Open Folder, Improvements of the debugger and few other stuff. You can see the complete list of features in release note at the following link.

On the same page, you can see the list of new features. That’s it. Hope you like it. Stay tuned for more!.
Wednesday, April 13, 2016

Come join us at - Global Azure Bootcamp 2016 Organized by Ahmedabad User Group

Do you want to be part of largest community event is the world? If Yes, then Global Azure boot camp 2016 is there. Ahmedabad User group is going to organize Global Azure boot camp.

The global azure boot camp is going to be organized at more than 180 locations worldwide. They are going to have some cool Azure lab. So I would encourage you to participate in that.

You can find more information about that following link.

If you want to participate in this one of the greatest event hosted by Ahmedabad User Group then you can register via the following  link.

Please create a pass for yourself and be there. I am also going to be there as a speaker and going to present about document DB.

So see you all there!

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews