在ASP.net中将DropDownList值传递到SQL命令
我有一个在ASP.net中将DropDownList值传递到SQL命令,sql,asp.net,drop-down-menu,html-select,selectedvalue,Sql,Asp.net,Drop Down Menu,Html Select,Selectedvalue,我有一个DropDownList,它从SQL表中获取值 我想从下拉列表中获取所选项目(本例中为课程)的平均值,并将其显示在标签中: 这部分工作- SqlConnection sqlConnection1; sqlConnection1 = new SqlConnection(@"Data Source=HA\SQLEXPRESS; Initial Catalog=Grades1; Integrated Security=True"); SqlCommand C
DropDownList
,它从SQL表中获取值我想从
下拉列表中获取所选项目(本例中为课程)的平均值,并将其显示在标签中:
这部分工作-
SqlConnection sqlConnection1;
sqlConnection1 = new SqlConnection(@"Data Source=HA\SQLEXPRESS; Initial Catalog=Grades1; Integrated Security=True");
SqlCommand Command = null;
Command = new SqlCommand("SELECT Course FROM GradesTable1", sqlConnection1);
Command.Connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(Command);
DataTable dataTble1 = new DataTable();
dataAdapter.Fill(dataTble1);
if (dataTble1.Rows.Count > 0)
{
foreach (DataRow row in dataTble1.Rows)
{
ListItem course1 = new ListItem(row["Course"].ToString());
if (!DropDownList1.Items.Contains(course1))
{
DropDownList1.Items.Add(course1); // showing the 2 courses
}
}
}
Command.Connection.Close();
}
}
这就是问题所在—(我什么都没有,没有数据)
编辑
我尝试了几种变体,如$”从GradesTable1中选择AVG(成绩)作为“ClassAVG”,其中Course=@Course”
和Command.Parameters.AddWithValue(“@Course”,DropDownList1.SelectedItem.Text)
,
或DropDownList1.SelectedValue
我认为问题在于从SQL接收的DropDownlist
值没有硬编码。
有没有正确的方法?在不知道advanced中的“课程”
”的情况下,是否有可能
感谢您的回答,请随时发表您的意见。我在aspx
页面(而不是aspx.cs
页面)的下拉列表中找到了缺少的内容
将其添加到DropDownList中解决了问题。//query=Sql query
query.Select(s => new MusteriNoktaComboItemDTO
{
Label = s.Adi,
Value = s.ID.ToString()
}).ToList();
我认为DropDownList1.SelectedItem将是一个对象。Use.ToString()…DropDownList1.SelectedValue
以前尝试过所有这些方法,但都无效。
query.Select(s => new MusteriNoktaComboItemDTO
{
Label = s.Adi,
Value = s.ID.ToString()
}).ToList();