[英]How to populate a dropdown list with SQL values in ASP.net Web application
I'm new to ASP.net and I'm trying to populate a dropdown list with values from a local SQL Database in Visual Studio.我是 ASP.net 的新手,我正在尝试使用 Visual Studio 中本地 SQL 数据库中的值填充下拉列表。
This is the code I have but Its not working, could anyone assist?这是我的代码,但它不起作用,有人可以帮忙吗?
{
SqlConnection PopulateListCon = new SqlConnection(ConnectionString);
try
{
if (PopulateListCon.State == ConnectionState.Closed)
PopulateListCon.Open();
String query = "SELECT * FROM ModuleTable WHERE UserId=@User AND ModuleSemester=@Sem";
SqlCommand sqlCmd = new SqlCommand(query, PopulateListCon);
sqlCmd.Parameters.Add("@User", SqlDbType.VarChar);
sqlCmd.Parameters["@User"].Value = userIdentification;
sqlCmd.Parameters.Add("@Sem", SqlDbType.VarChar);
sqlCmd.Parameters["@Sem"].Value = semester;
SqlDataReader dr1 = sqlCmd.ExecuteReader();
while (dr1.Read())
{
string modName = dr1.GetString(3);
Ddl_Module_Info_Time_Allocation_Module_Code.Items.Add(modName);
}
}
catch (Exception ex)
{
errMsg = ex.Message;
Response.Write("<script>alert('Error: " + errMsg + "')</script>");
}
finally
{
PopulateListCon.Close();
}
}
this is the Code for the Drop Down List:这是下拉列表的代码:
<asp:DropDownList ID="Ddl_Module_Info_Time_Allocation_Module_Code" runat="server" style="z-index: 3; left: 330px; top: 10px; position: absolute" Height="24px" Width="128px" Visible="False"></asp:DropDownList>
If anyone could assist it would be appreciated如果有人可以提供帮助,将不胜感激
Your code is close.你的代码很接近。 but you should show the markup for the drop down list.
但您应该显示下拉列表的标记。
Like desktop (FoxPro, MS-Access, vb.net, c#), a combo box (drop down list) has the ability to deal with TWO columns.与桌面(FoxPro、MS-Access、vb.net、c#)一样,组合框(下拉列表)具有处理两列的能力。
DataValueField = "column name" - this is the "ID" or often PK from table
DataTextField = "column name" - this is the text "display" column from table
So, say I need a drop down of Hotel Names.所以,假设我需要下拉酒店名称。 And say only hotels from a given city.
并且只说来自给定城市的酒店。
So the markup is this:所以标记是这样的:
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="128px"
DataValueField = "ID"
DataTextField = "HoteName"
>
</asp:DropDownList>
<br />
And the above also lets me go SELECT * from some table, since which column is supposed to fill which setting in the drop down list??以上也让我 go SELECT * 来自某个表,因为哪一列应该填写下拉列表中的哪个设置? (so, that's what DataValue and DataText settings above do).
(所以,这就是上面的 DataValue 和 DataText 设置所做的)。
Now, our code would be this:现在,我们的代码将是这样的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadCombo();
}
void LoadCombo()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string testcity = "Banff"; // this could be some user input - whatever
string strSql = "SELECT ID, HotelName FROM tblHotels Where City = @City";
using (SqlCommand cmdSQL = new SqlCommand(strSql, conn))
{
cmdSQL.Parameters.Add("@City", SqlDbType.NVarChar).Value = testcity;
conn.open();
DropDownList1.DataSource = cmdSQL.ExecuteReader();
DropDownList1.DataBind();
}
}
}
So, your code for this could/would be this:因此,您的代码可能/将是这样的:
using (SqlConnection PopulateListCon = new SqlConnection(ConnectionString))
{
string strquery = "SELECT * FROM ModuleTable";
using (SqlCommand sqlCmd = new SqlCommand(strquery, PopulateListCon))
{
PopulateListCon.Open();
Ddl_Module_Info_Time_Allocation_Module_Code.DatSource = sqlCmd.ExecuteReader();
Ddl_Module_Info_Time_Allocation_Module_Code.DataBind();
}
}
The above is without parameters.以上是没有参数的。
For parameters, say your this:对于参数,说你这个:
using (SqlConnection PopulateListCon = new SqlConnection(ConnectionString))
{
string query = "SELECT * FROM ModuleTable WHERE UserId=@User AND ModuleSemester=@Sem";
using (SqlCommand sqlCmd = new SqlCommand(strquery, PopulateListCon))
{
sqlCmd.Parameters.Add("@User", SqlDbType.Int).Value = User_ID;
sqlCmd.Parameters.Add("@Sem", SqlDbType.Int).Value = some sem expression here;
PopulateListCon.Open();
Ddl_Module_Info_Time_Allocation_Module_Code.DatSource = sqlCmd.ExecuteReader();
Ddl_Module_Info_Time_Allocation_Module_Code.DataBind();
}
}
Then our code would be:那么我们的代码将是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.