public partial class DropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropdownticket();
}
}
string strsqlcon = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
protected void dropdownticket()
{
try
{
SqlConnection sqlconn = new SqlConnection(strsqlcon);
SqlCommand cmd = new SqlCommand("SELECT * FROM SYS.databases", sqlconn);
sqlconn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet sd = new DataSet();
sda.Fill(sd);
Dropdownlist1.DataSource = sd;
Dropdownlist1.DataTextField = "";
Dropdownlist1.DataValueField = "";
Dropdownlist1.DataBind();
Dropdownlist1.Items.Insert(0, new ListItem("--Select--", "0"));
sqlconn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
在上面的代码中,我无法在下拉列表中找到数据库列表。
答案 0 :(得分:1)
您需要指定column
来自DropDownList
查询的SELECT
<{1}}
name
是从databsename
查询中指定SELECT
的列。
试试这个:
Dropdownlist1.DataTextField = "name";
Dropdownlist1.DataValueField = "name";
答案 1 :(得分:1)
您需要指定哪个列应从SELECT查询中分配给DropDownList,该列必须在Dropdownlist的DataTextField和DataValueField字段中添加。
cmd.ExecuteNonQuery();
Dropdownlist1.DataTextField = "name";
Dropdownlist1.DataValueField = "name";