Click here to Skip to main content
Rate this: bad
good
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
Edited 22-Apr-13 1:57am
v2
Comments
Karthik Harve at 22-Apr-13 7:58am
   
[Edit] pre tags added.
Zafar Sultan at 22-Apr-13 8:00am
   
Your question does not explain what you want to achieve? Can you be a little more clear?
Karthik Harve at 22-Apr-13 8:00am
   
do you want to pass more then one value of the dropdownlist instead of only selected one ?
Member 7909184 at 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 at 22-Apr-13 11:35am
   
Do you want to show all column of table in gridview or single one ?
Aner Bautista at 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 at 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
0 OriginalGriff 370
1 Sergey Alexandrovich Kryukov 205
2 Prakriti Goyal 177
3 jlopez788 134
4 _Amy 110
0 OriginalGriff 6,762
1 Sergey Alexandrovich Kryukov 5,539
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
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