简体   繁体   English

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

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

I have searched for most of the day and I believe I may just be not able to explain myself to a search engine. 我一天中大部分时间都在搜索,我相信我可能无法向搜索引擎解释自己。 Please forgive me on this. 请原谅我。 I have one drop down list I use to get the column names with the code below. 我有一个下拉列表,我用下面的代码来获取列名。

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";

Now, the intention of my work is to use the selected item from this to create the sqldatasource of the next drop down. 现在,我的工作目的是使用从中选择的项创建下一个下拉列表的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;
}

However, when I attempt to get this to work, the DropDownList2 doesn't populate. 但是,当我尝试使其工作时,不会填充DropDownList2。 What could i be missing? 我可能会缺少什么? Thank you for looking at my problem. 谢谢你看我的问题。

I tested your code and it works on my machine. 我测试了您的代码,它可以在我的机器上运行。 I'm guessing that, in order for this to happen right after the column is selected, you need to make sure that the AutoPostBack option on DropDownList1 is set to true . 我猜想,为了在选择列之后立即发生这种情况,您需要确保DropDownList1AutoPostBack选项设置为true

暂无
暂无

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

相关问题 从sql数据库中提取特定列并将值保存在asp.net中的变量中c# - fetch specific column from sql database and saving value in variable in asp.net c# C#ASP.NET MVC:如何从Entity Framework中的列表返回的ID列获取元素列表 - C# ASP.NET MVC: How to get list of elements by id column returned from a list in Entity Framework C#ASP.NET从同一mysql命令中选择多个列,并在后面的代码中使用它们 - C# ASP.NET Selecting multiple column from same mysql command and use them in code behind 如何在页面加载时以编程方式从GridView1的第一行第4列单元格中选择数据并将其保存到C#asp.net中的变量 - How to programatically select the data from GridView1's first row 4th column cell on page load and save it to a variable in C# asp.net 如何使用asp.net C#仅在linq中获得一列名称 - how to get only one column names in linq using asp.net c# 如何在asp.net中显示带有两个具有相同列名但具有不同值的表的图表C# - how to show chart in asp.net with two tables with same column names but with different values in it C# 获取表的列名并将它们存储在字符串或 var c# asp.net - Get the column names of a table and store them in a string or var c# asp.net “变量名称在查询批处理或存储过程中必须唯一。”错误C#asp.net - “Variable names must be unique within a query batch or stored procedure.” error c# asp.net ASP.NET柱形图在C#中每列的着色 - asp.net column chart coloring of each column in c# 柱形图上方的ASP.NET(C#)柱形图 - ASP.NET (C#) Column Chart on top of Column Chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM