Feb 4, 2009

Dropdown list values get from DB in ASP.net

in .cs file code
------------------
public DataSet LoadValues(string procName, string valueField, string nameField)
{
string retVal = string.Empty;
DataSet ds = new DataSet();
if (procName == "GetList" || procName == "GetStateName")
{
retVal = "";
}
ds = GetStateValues(procName);
// now construct the option list
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int x = 0; x < ds.Tables[0].Rows.Count; x++) { retVal += "";
}
}
}

return ds;
}
===========
code hehind section add the below code:
--------------------------------------
SessionDB sessionDB = new SessionDB();

DataSet ds = sessionDB.LoadValues("GetList", "UserID", "UserName");

if (ds.Tables[0].Rows.Count > 0)
{
dduserid.DataSource = ds.Tables[0];
dduserid.DataTextField = "UserName";
dduserid.DataValueField = "UserName";
dduserid.DataBind();
dduserid.Items.Insert(0, "----Select----");
}