Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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??
C#
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
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 ?
fak_farrukh 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??
[no name] 22-Apr-13 11:35am    
Do you want to show all column of table in gridview or single one ?

1 solution

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:

C#
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

C#
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
 
Share this answer
 
Comments
Rockstar_ 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900