Introduction :
Here i will explain How to bind the values into Dropdownlist in Asp.net and C# in page load. here Values will be passed to dropdownlist from DataSet.Asp.Net Code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fourth Bottle</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1"
AutoPostBack="false"
runat="server"> </asp:DropDownList>
</div>
</form>
</body>
</html>
C# Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace SolutionName
{
public partial class drop :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
string Conec = ConfigurationManager.ConnectionStrings["Student"].ConnectionString;
SqlConnection con = new
SqlConnection(Conec);
con.Open();
SqlDataAdapter da = new
SqlDataAdapter("select
StudentID from StudentMarks", con);
DataTable dt = new
DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataValueField =
dt.Columns["StudentID"].ColumnName.ToString();
DropDownList1.DataTextField = dt.Columns["StudentID"].ColumnName.ToString();
DropDownList1.DataBind();
}
}
}