Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How do i select a particular item from a list box and then execute an sql query to display the particular result?

im confused as to where im supposed to write this sql query..
Posted

Refer this for listbox
Enhanced List Box Control
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.aspx
Get Selected item from listbox

C#
int numberSelected = listBox1.SelectedItems.Count;

   for (int i = 0; i < numberSelected; i++)
   {
       ListItem li = (ListItem)listBox1.SelectedItems[i];
       //sql query - pass li.value to query
  }
 
Share this answer
 
v2
Comments
codingisok101 24-Aug-12 3:16am    
but im selecting only one item at a time..
pradiprenushe 24-Aug-12 4:30am    
then
you can directly fire query
if(listBox1.SelectedItems.Count != 0)
string query= "select * from table where column = "+listBox1.SelectedItems[0].Value;
Do it like this:
C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(listBox1.SelectedIndex > -1)
    {

       object selectedValue = listBox1.SelectedValue;
      // I advise you to unbox listbox's selected value to it's proper type
      //i.e. if it is type of int
      //int selectedValue = (int) listBox1.SelectedValue;

      //query code (or call to a method with query code) goes  here      
    }
}


Good luck!
 
Share this answer
 
I think, you are talking about Windows application.

You need to select the selectedItem of the listbox and get the Text from it.
Use the event, SelectedIndexChanged of Listbox.

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //get the selectd text

    // The variable that will keep the index of the selected item
    int index;
    // Get the index of the selected item
    index = listBox1.SelectedIndex;

    //get the selected text
    string selectedText = string.Empty;
    if (index >= 0)
    {
        selectedText = listBox1.GetItemText(listBox1.Items[index]).ToString();


      //write your sql and pass the text as one parameter
      //do sql stuff

      //get the data and show it in any controls like gridview or whatever you want to do with data

    }
}


Hope this helps.
cheers
 
Share this answer
 
v2
Comments
codingisok101 24-Aug-12 2:30am    
my list has 1,2,3 etc.
this refers to the course id in my program
so how do i pass the course id as the item in the list?
VIPR@T 24-Aug-12 8:25am    
can you explain this things in brief?
Sandip.Nascar 24-Aug-12 11:13am    
You gave a listbox, you query a table and populate the data into the list box. Say a beginners example - it shows color - white, black, red etc.
You have another table name it mobile. Mobile table contains Mobile Model and color.
Say - Samsung Model 1 : red color
Samsung Model 2 : white color
Nokia Model 1: black

Now, you need to show all mobile whose color is red or white or black.

So, in the listbox, you populate colors from table "color" and populate the listbox in page_load.

Now, if you select red, the selectedindex_change event will fire.
So, you need to pass the listbox text to the sql

something like, select * from mobile where color=@color;
@color parameter will be set by the selected value in listbox
So, if you select white, you will have the text "white", if you select black in the listbox, you have the value black and s on. Using the value you fire the sql to populate mobile data and show it in a control, could be a gridview.

Is this make sence?
cheers

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