繁体   English   中英

使用列名称的变量选择查询返回的列。 (asp.net C#)

[英]Use a variable of the column names to select the column returned from a query. (asp.net c#)

我一天中大部分时间都在搜索,我相信我可能无法向搜索引擎解释自己。 请原谅我。 我有一个下拉列表,我用下面的代码来获取列名。

MySQLDataSource.ConnectionString = matt.GetConnectionString();
MySQLDataSource.SelectCommand = "SELECT name FROM sys.syscolumns WHERE (id = 
      (SELECT id    FROM sys.sysobjects WHERE (name = 'tbl_EquipmentStock')))";
DropDownList1.DataValueField = "name";
DropDownList1.DataTextField = "name";

现在,我的工作目的是使用从中选择的项创建下一个下拉列表的sqldatasource。

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    funtions matt = new funtions(); // funtions is the class that contains my connection string.
    string frank = DropDownList1.SelectedValue;
    a_c_instruction.Text = " Data filtered on: ";
    selectedItem.Text = frank;
    string sql = "SELECT " + frank + " FROM tbl_EquipmentStock"; 
    AnotherSqlDataSource.ConnectionString = matt.GetConnectionString();
    AnotherSqlDataSource.SelectCommand = sql;
    DropDownList2.DataValueField = frank;
    DropDownList2.DataTextField = frank;
    DropDownList2.Visible = true;
}

但是,当我尝试使其工作时,不会填充DropDownList2。 我可能会缺少什么? 谢谢你看我的问题。

我测试了您的代码,它可以在我的机器上运行。 我猜想,为了在选择列之后立即发生这种情况,您需要确保DropDownList1AutoPostBack选项设置为true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM