使用C#从下拉菜单中选择语句箱通过选择价值 [英] Pass selected value from drop down box in select statement using C#

查看:119
本文介绍了使用C#从下拉菜单中选择语句箱通过选择价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下拉,显示的位置名称和下降的数据源带来向下同时LOC_NM和LOC_ID但用户只能查看位置名称。因此,我想要做的就是通过LOC_ID在我的选择查询,我会怎么做呢?
这里是code为下拉:

I have drop down that shows the location name and the data source for the drop down brings both the LOC_NM and LOC_ID but the user will see only the Location Name. So what I want to do is pass the LOC_ID in my select query, how would I do that? Here is the code for the drop down:

<asp:DropDownList ID="locatioin"  DataSourceID="dd_source" DataTextField="LOC_NM" DataValueField="LOC_ID" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Select Location" Value="Select Location"></asp:ListItem>
</asp:DropDownList>

和这里的背后是对我的SELECT语句中的code:

and here is the code behind for my select statement:

 SqlDataAdapter da = new SqlDataAdapter("select Company, Location from MainTable
    where Location = selected LOC_ID")

你可以看到我想通过LOC ID为我的查询中选择的价值,但我有麻烦:其中位置=选择LOC_ID

as you can see i want to pass the LOC ID for the selected value in my query but i am having trouble with that: where Location = selected LOC_ID

推荐答案

这是快速的解决方案:

var tmp = "SELECT Company, Location FROM MainTable WHERE LocationColumn = {0}");
var query = string.Format(tmp,location.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(query);

这是安全的解决方案:

SqlConnection conn = new SqlConnection(_connectionString);
conn.Open();
string s = "SELECT Company, Location " + 
           "FROM MainTable " +
           "WHERE LocationColumn = @location";
SqlCommand cmd = new SqlCommand(s);
cmd.Parameters.Add("@location", location.SelectedValue);
SqlDataReader reader = cmd.ExecuteReader();

信贷以最好的编码站点之一:的编码恐怖:给我SQL参数化,毋宁死

这篇关于使用C#从下拉菜单中选择语句箱通过选择价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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