在SQL Server中存储dropdownlist的所有值 [英] Storing all the values of dropdownlist in SQL server

查看:57
本文介绍了在SQL Server中存储dropdownlist的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asp.net中的下拉列表是动态填充的,我想将它的所有值存储到SQL Server中。首先,我想在代码中获取dropdownlist的所有值?怎么做?

解决方案

< asp:DropDownList ID =   ddlCountry runat =   server /> 



c#code


使用系统;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Web.UI.WebControls;

受保护 void Page_Load(对象发​​件人,EventArgs e)
{
如果(!IsPostBack)
{
BindContrydropdown() ;
}
}
/// < 摘要 >
/// 绑定COuntrydropdown
/// < / summary >
protected void BindContrydropdown()
{
// 数据库的连接路径
使用(SqlConnection con = new SqlConnection( 数据源= maneavinash;集成S. ecurity = true;初始目录= MySampleDB))
{
con.Open();
SqlCommand cmd = new SqlCommand( 选择UserId,UserName FROM UserInformation,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = UserName;
ddlCountry.DataValueField = UserId;
ddlCountry.DataBind();
ddlCountry.Items.Insert( 0 new ListItem( - 选择 - 0));
con.Close();
}
}


你可以通过for循环插入...每次获取一个值,然后将其插入到DB

The dropdownlist in asp.net is filled dynamically & I want to store all it's value to SQL Server. First I want to get all the values to of dropdownlist in code behind? How to do it?

解决方案

<asp:DropDownList ID="ddlCountry" runat="server" />



c# code  


using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindContrydropdown();
}
}
/// <summary>
/// Bind COuntrydropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
using (SqlConnection con = new SqlConnection("Data Source=maneavinash;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserId,UserName FROM UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "UserName";
ddlCountry.DataValueField = "UserId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
}


you can insert by for loop ...by getting a value every time and then insert it into DB


这篇关于在SQL Server中存储dropdownlist的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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