Wednesday 18 February 2015

MVC: How to Bind Data to DropDownList from Database using Stored Procedure in MVC 4 Razor

Hi Friends,in this article I will explain about How to Bind Data to DropDownList from Database using Stored Procedure in MVC 4 Razor.
I already explained in the previous articles about How to bind DropDownList from database in C# MVC 4 razor,How to Create Cascading DropDownList in MVC 4 Razor and JQuery and MVC4 Razor:How to call Stored Procedure using C#

Stored Procedure:
CREATE PROCEDURE Get_Name
AS
BEGIN
      SET NOCOUNT ON;
    SELECT * FROM student_data order by firstname Asc
END
GO

Account.cs Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace MvcCRUD.Models
{
    public class Account
    {
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        public DataSet Bind2DDL()
        {
            StudentContext db = new StudentContext();
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Get_Name", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
    }   
}

DropDownList Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCRUD.Models;
using System.Data;
using System.Data.SqlClient;

namespace MvcCRUD.Controllers
{
    public class DropDownListController : Controller
    {
        public ActionResult Bind2Ddl(Account obj)
        {
            DataSet ds = obj.Bind2DDL();
            ViewBag.fname = ds.Tables[0];
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (System.Data.DataRow dr in ViewBag.fname.Rows)
            {
                items.Add(new SelectListItem { Text = @dr["firstname"].ToString(), Value = @dr["firstname"].ToString() });
            }
            ViewBag.firstname = items;
            return View();
        }
    }
}

Bind2Ddl.cshtml
@model MvcCRUD.Models.Account
@{
    ViewBag.Title = "Bind Data to DropDownList from Database using Stored Procedure in MVC 4 Razor";
}
<h3 style='color:Green'>Bind Data to DropDownList from Database using Stored Procedure in MVC 4 Razor</h3><br/>
FirstName
@Html.DropDownList("firstname")


The output of the above code as shown in the below figure.

"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

7 comments:

  1. very nice article,easy to understand for asp.net developers

    ReplyDelete
  2. very good for new developer

    ReplyDelete
  3. very helpful..
    how can we bind multiple dropdown from database using postback in Html page
    please suggest

    ReplyDelete
    Replies
    1. Hi Tanuja,
      Thank You.
      Refer below links
      http://aspdotnet-kishore.blogspot.in/2013/10/how-to-create-cascading-dropdownlist-in.html
      http://aspdotnet-kishore.blogspot.in/2013/10/json-create-cascading-dropdownlist-from.html

      Delete
  4. Азаматсын ким жазса да, Кыргыз балага жардам бердин.

    ReplyDelete
  5. it was helpful.

    Thanks kishore

    ReplyDelete

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.