Click here to Skip to main content
12,511,526 members (51,445 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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();
Posted 22-Apr-13 1:54am
Updated 22-Apr-13 1:57am
v2
Comments
Karthik Harve 22-Apr-13 7:58am
   
[Edit] pre tags added.
Zafar Sultan 22-Apr-13 8:00am
   
Your question does not explain what you want to achieve? Can you be a little more clear?
Karthik Harve 22-Apr-13 8:00am
   
do you want to pass more then one value of the dropdownlist instead of only selected one ?
Member 7909184 22-Apr-13 8:19am
   
in this i can only get hardDisks Column but if i want to select different value from combox it will not run because in query harddisk column se selected.
so how to get more column??
Susheel Kumar Verma 22-Apr-13 11:35am
   
Do you want to show all column of table in gridview or single one ?
Aner Bautista 22-Apr-13 13:00pm
   
To tell you the truth, I don't get if you want to select more than one value from the dropdownlist or if you want to filter more columns in your query (obviously using other controls on your page).

Please help us, to help you, adding more details.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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$'] WHERE HardDisks IN (" + params  + ")";


Please try this let me know the result.

Thank you,
Vamsi
  Permalink  
Comments
Member61 23-Apr-13 1:50am
   
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100