将SelectedItem从SQL Server获取到DropDownList [英] Getting a SelectedItem from SQL server to DropDownList

查看:117
本文介绍了将SelectedItem从SQL Server获取到DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用ASP.Net并使用后面的代码作为C#。现在我想从SQL服务器检索数据到DropDownList控件。所选值应显示在下拉列表中(表单加载)。



请帮助。



此致,

Praveen

解决方案

protected void load_cmbFirstLanguage(int intCourseId,DropDownList cmbFLanguage)

{

ds = new DataSet();

ad = new SqlDataAdapter(select * from Table,cn);

ad.Fill(ds );

if(ds.Tables [0] .Rows.Count> 0)

{

cmbFLanguage.Items.Clear() ;

cmbFLanguage.Items.Add( - 选择第一语言 - );

cmbFLanguage.Items [0] .Value =0;

for(int i = 0; i< ds.Tables [0] .Rows.Count; i ++)

{

cmbFLanguage.Items.Add (ds.Tables [0] .Rows [i] [strSubjectName]。ToString());





}

}

否则

{

lblMsg.ForeColor = Color.Red;

lblMsg.Text =没有找到记录;

}

}


您好Praveen,

 protected void Page_Load(object sender,EventArgs e) 
{
string cs = ConfigurationManager.ConnectionStrings [samplecnstring]。ConnectionString;
SqlConnection cn = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter(select countryname from country,cn);
DataSet ds = new DataSet();
da.Fill(ds,Country);

DropDownList1.DataSource = ds;
DropDownList1.DataTextField =countryname;
DropDownList1.DataValueField =countryname;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, - Select-);
}





祝你有个美好的一天!!!


试试

{

if(!IsPostBack)

{

if(drpdwnlist.SelectedValue == null)

{

Response.Redirect(login.aspx);

}



else

{



ListItem lt;

DataSet ds = new DataSet();

ds = balObj.getCustomerValues();



for(int i = 0; i< ds.Tables [0] .Rows.Count; i ++)

{

lt = new ListItem();

lt.Text = ds.Tables [0] .Rows [i] [ 1] .ToString();

lt.Value = ds.Tables [0] .Rows [i] [0] .ToString();

this.drpdwnlist.Items.Add(lt);



}



this.drpdwnlist.SelectedIndex = 0;

objComp.CompIntlId = int.Parse(this.drpdwnlist。 SelectedValue);

lbldispaly.Text = drpdwnlist.SelectedItem.Value.ToString();

}



}

Hi,

I am using ASP.Net and using code behind as C#. Now I want to Retrieve data from SQL server to DropDownList control. The selected value should be displayed in the dropdown list (Form Load).

Help please.

Regards,
Praveen

解决方案

protected void load_cmbFirstLanguage(int intCourseId,DropDownList cmbFLanguage)
{
ds = new DataSet();
ad = new SqlDataAdapter("select * from Table","cn");
ad.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
cmbFLanguage.Items.Clear();
cmbFLanguage.Items.Add("--Select First Language--");
cmbFLanguage.Items[0].Value = "0";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cmbFLanguage.Items.Add(ds.Tables[0].Rows[i]["strSubjectName"].ToString());


}
}
else
{
lblMsg.ForeColor = Color.Red;
lblMsg.Text = "No Records found";
}
}


Hi Praveen,

protected void Page_Load(object sender, EventArgs e)
   {
       string cs = ConfigurationManager.ConnectionStrings["samplecnstring"].ConnectionString;
       SqlConnection cn = new SqlConnection(cs);
       SqlDataAdapter da = new SqlDataAdapter("select countryname from country", cn);
       DataSet ds = new DataSet();
       da.Fill(ds, "Country");

       DropDownList1.DataSource = ds;
       DropDownList1.DataTextField = "countryname";
       DropDownList1.DataValueField = "countryname";
       DropDownList1.DataBind();
       DropDownList1.Items.Insert(0, "-Select-");
   }



Have a good day!!!


try
{
if (!IsPostBack)
{
if (drpdwnlist.SelectedValue == null)
{
Response.Redirect("login.aspx");
}

else
{

ListItem lt;
DataSet ds = new DataSet();
ds = balObj.getCustomerValues();

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
lt = new ListItem();
lt.Text = ds.Tables[0].Rows[i][1].ToString();
lt.Value = ds.Tables[0].Rows[i][0].ToString();
this.drpdwnlist.Items.Add(lt);

}

this.drpdwnlist.SelectedIndex = 0;
objComp.CompIntlId = int.Parse(this.drpdwnlist.SelectedValue);
lbldispaly.Text = drpdwnlist.SelectedItem.Value.ToString();
}

}


这篇关于将SelectedItem从SQL Server获取到DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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