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.

new-project-visual-studio

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; }
        [ForeignKey("StandardId")]
        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)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
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.

adding-cascading-dropdown-page

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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Cascading dropwon demo</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList runat="server" ID="ddlStandard" CssClass="dropdown" AutoPostBack="true" OnSelectedIndexChanged="ddlStandard_OnSelectedIndexChanged"/>
        </div>
        <br/>
        <div>
            <asp:DropDownList runat="server" ID="ddlStudent" CssClass="dropdown"/>
        </div>
    </form>
</body>
</html>

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)
            {
                BindStandards();
                BindStudents();
            }
           
        }

        private void BindStudents(int? standardId=null)
        {
            ddlStudent.Items.Clear();
            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.DataBind();
            }

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

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

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

        protected void ddlStandard_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            BindStudents(Convert.ToInt32(ddlStandard.SelectedValue));
        }
    }
}
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.

Standard-Cascading-dropdown-data-entity-framework

And student table like following.

student-cascading-dropdown-entity-framework

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

sample-application

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- https://github.com/dotnetjalps/CascadingDropdownEF
Share:
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.

ToolsAndExtensionsinVisualStudio

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.

cloud-explorer-preview

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:
https://visualstudiogallery.msdn.microsoft.com/84e83a7c-9606-4f9f-83dd-0f6182f13add

For Visual Studio 2013:
https://visualstudiogallery.msdn.microsoft.com/d3896fc2-109b-4420-9c38-b6ab1cfa9fe5

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.

cloud-explorer-visual-studio-2015

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.

documnetdb-database-azure-cloud-explorer

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

database-documentdb-cloud-explorer

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

record-in-cloud-explorer

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

json-documnet-cloud-explorer-documentdb

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!.
Share:
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.
2016_04_14_header
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.

https://code.visualstudio.com/blogs
https://www.thurrott.com/dev/66355/microsoft-delivers-visual-studio-code-1-0

If you have not used it then I recommend you to download this and try it from the following location.
https://code.visualstudio.com/Download

That’s it. Hope you like it. Stay tuned for more!
Share:
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.

AUGLogo3

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.

DSCN5888

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

DSCN5893
DSCN5891

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.
https://github.com/dotnetjalps/gab16augdemo

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.

DSCN5894

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

DSCN5897DSCN5895DSCN5905DSCN5896

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.

DSCN5900
DSCN5901

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.

DSCN5906
DSCN5907

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.

DSCN5912
DSCN5914

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

DSCN5917DSCN5924
DSCN5925DSCN5921
DSCN5926DSCN5927

And this guy was an inspiration for all us.

DSCN5919

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

DSCN5933

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!.
Share:
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.

https://www.visualstudio.com/downloads/visual-studio-next-downloads-vs

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.

visualstudiopreview
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.

https://www.visualstudio.com/news/vs15-preview-vs

On the same page, you can see the list of new features. That’s it. Hope you like it. Stay tuned for more!.
Share:
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.

http://global.azurebootcamp.net/

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.

gab16aug.eventbrite.com

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!
Share:

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