在下拉列表中有问题 [英] have a problem in dropdownlist

查看:80
本文介绍了在下拉列表中有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码正在运行并在gridview中显示数据。

但是现在如果我在组合框中有2或3个值如何选择那个??

 SqlConnection con; 
con = new SqlConnection(connstring);
con.Open();

string a = DropDownList1.SelectedValue.ToString();
string str = SELECT * FROM [ 'ISB VAS Nodes $'] WHERE HardDisks = + a + ;
// city是表中的列名
SqlCommand cmd;
cmd = new SqlCommand(str,con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{

TextBox1.Text = dr.GetString( 0 );
// 如果city在表的0索引中
GridView1.DataSource =博士;
GridView1.DataBind();
}
else
{
Response.Write( 城市不存在);

}
dr.Close();
con.Close();

解决方案

'] WHERE HardDisks = + a + ;
// city是表中的列名
SqlCommand cmd;
cmd = new SqlCommand(str,con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{

TextBox1.Text = dr.GetString( 0 );
// 如果city在表的0索引中
GridView1.DataSource = dr;
GridView1.DataBind();
}
else
{
Response.Write( 城市不存在);

}
dr.Close();
con.Close();


嗨会员7909184334,



根据我的理解,您需要在组合框中选择多个要在搜索查询中使用的值。



如果是这样,那么你可以在一个数组中获得组合框选择的值,并按如下方式设置查询:



  string  str =   SELECT * FROM ['ISB VAS Nodes 


'] WHERE HardDisks IN( + a [ 0 ]。toString( )+ + a [ 1 ]。toString()+ + a [ 2 ]。toString()+ ;





以上部分显示只有3个选定选项。我们可以使其动态如下:



首先将所有选定的选项放入数组中,我们将其称为 aryOptions



  string   params  =  string  .Empty; 

// 将数组项转换为逗号分隔字符串
for int i = 0 ;我< aryOptions.Length; i ++)
{
params = params + aryOptions [i] + ;
}

// 删除最后一个逗号
if params .Length > 0
{
params = params .SubString( 0 ,parmas.Length - 1 );
}

// 使用WHERE IN子句传递值
string str = SELECT * FROM [ 'ISB增值节点


this code is running and showing the data in gridview.
but now if i have 2 or 3 values in combobox how to pick that??

SqlConnection con;
con = new SqlConnection(connstring);
con.Open();

string a = DropDownList1.SelectedValue.ToString();
string str = "SELECT * FROM ['ISB VAS Nodes$'] WHERE HardDisks=" + a + "";
//city is the column name in table
SqlCommand cmd;
cmd = new SqlCommand(str, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{

   TextBox1.Text = dr.GetString(0);
    //if city is in 0 index of table
    GridView1.DataSource = dr;
    GridView1.DataBind();
}
else
{
    Response.Write("City does not exist");

}
dr.Close();
con.Close();

解决方案

'] WHERE HardDisks=" + a + ""; //city is the column name in table SqlCommand cmd; cmd = new SqlCommand(str, con); SqlDataReader dr; dr = cmd.ExecuteReader(); if (dr.Read()) { TextBox1.Text = dr.GetString(0); //if city is in 0 index of table GridView1.DataSource = dr; GridView1.DataBind(); } else { Response.Write("City does not exist"); } dr.Close(); con.Close();


Hi Member 7909184334,

As per my understanding, you need to select multiple values in a combobox which are to be used in the search query.

If so, then you can get the combobox selected values in an array and set the query as follows:

string str = "SELECT * FROM ['ISB VAS Nodes


'] WHERE HardDisks IN (" + a[0].toString() + ", " + a[1].toString() + ", " + a[2].toString() + ")";



The above part shows that there are only 3 selected options. We can make it dynamic as below:

First get all the selected options into an array, lets call it as aryOptions

string params = string.Empty;

// Get the array items into a comma seperated string
for(int i = 0; i < aryOptions.Length; i++)
{  
     params = params + aryOptions[i] + ",";
}

// Remove the last comma
if(params.Length > 0)
{
   params = params.SubString(0, parmas.Length - 1); 
}

// pass the value with WHERE IN clause
string str = "SELECT * FROM ['ISB VAS Nodes


这篇关于在下拉列表中有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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