This article shows how to bind dropdownlist using Entity Framework in ASP.NET web application. Follow these below steps:
CREATE TABLE [dbo].[Tbl_Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[Student_Name] VARCHAR (50) NULL,
[Student_Age] INT NULL,
[Student_Address] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DDLNAME" runat="server" Width="141px"></asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo_Asp_DotNet.Models;
namespace Demo_Asp_DotNet
{
public partial class bIND_DDL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Database1Entities ob = new Database1Entities();
var s = ob.Tbl_Student.ToList();
DDLNAME.DataSource = s;
DDLNAME.DataTextField = "Student_Name";//column name of a table, which you want to display on runtime
DDLNAME.DataValueField = "Student_Id";
DDLNAME.DataBind();
}
}
}
}
Step-1: Create Table
First create a table "dbo.Tbl_Student" in database.CREATE TABLE [dbo].[Tbl_Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[Student_Name] VARCHAR (50) NULL,
[Student_Age] INT NULL,
[Student_Address] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
Step-2: Create a new ASP.Net web application
Now create the one sample application "Demo_Asp_DotNet" as:- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
- Provide the web site a name such as "Demo_Asp_DotNet" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - "bIND_DDL.aspx page".
- Drag and drop required controls on the <form> section of the bIND_DDL.aspx page.
Step-3: Add Entity Data Model in Solution
Then add entity data model in model folder.Step-4: Write Code in Design Page
Write this below code in "bIND_DDL.aspx" design page.<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DDLNAME" runat="server" Width="141px"></asp:DropDownList>
</div>
</form>
</body>
</html>
Step-5: Write Code in .cs page or Back-end Page
Write this below code in "bIND_DDL.aspx.cs" page.using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo_Asp_DotNet.Models;
namespace Demo_Asp_DotNet
{
public partial class bIND_DDL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Database1Entities ob = new Database1Entities();
var s = ob.Tbl_Student.ToList();
DDLNAME.DataSource = s;
DDLNAME.DataTextField = "Student_Name";//column name of a table, which you want to display on runtime
DDLNAME.DataValueField = "Student_Id";
DDLNAME.DataBind();
}
}
}
}
0 Comments