How to populate a drop down box from a DataTable object using C#
bind dropdownlist from datatable c#
void LoadSubject()
{
DataTable dt=new DataTable();
dt = getDataTable(SPName: "USP_Utkarsh_Subject", objParams: new string[] { "@action" }, objValues: new object[] { "get_subject" });
if (dt != null && dt.Rows.Count > 0)
{
ddlSubject.DataSource = dt;
ddlSubject.DataValueField = dt.Columns[0].ToString();
ddlSubject.DataValueField = dt.Columns[1].ToString();
ddlSubject.DataBind();
ddlSubject.Items.Insert(0, "Seelct Subject");
}
}
SQL query : SELECT SubjectId, SubjectName from Utkarsh_Subject with(nolock) order by SubjectId
create proc USP_Utkarsh_Subject
(@action varchar(20)
)
AS BEGIN
IF(@action='get_subject')
BEGIN
SELECT SubjectId,SubjectName from Utkarsh_Subject with(nolock) order by SubjectId
END
END