如何从数据库到下拉列表的下拉列表中获取值 [英] how to get the values from dropdown coming from database to dropdown

查看:86
本文介绍了如何从数据库到下拉列表的下拉列表中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库到下拉列表的下拉列表中获取值

how to get the values from dropdown coming from database to dropdown

推荐答案

使用sqlsource或使用编码..


这是通过编码.

use sqlsource or using coding..


this is by coding.

DataSet ds = new DataSet();
SqlDataAdapter myda = new SqlDataAdapter("select query",connection object );
myda.Fill(ds);
dropdown_name.DataSource = ds;
dropdown_name.DataValueField = "one field from select query";
dropdown_name.DataBind();
dropdown_name.Items.Insert(0, new ListItem("Select", "0"));


Try this: here ddl1 is dropdownlist and con is ur connection string to database 


con.Open();
SqlCommand cmd = new SqlCommand("select e_id, e_name from emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
ddl1.DataValueField = dt.Columns[0].ToString();
ddl1.DataTextField = dt.Columns[1].ToString();
ddl1.DataSource = dt;
ddl1.DataBind();
ddl1.Items.Insert(0, "--SELECT--");
con.Close();


这篇关于如何从数据库到下拉列表的下拉列表中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆