Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two buttons on form 1.

In button A's event, I used sql to select a list of id's and names from db and displayed it in a listbox.
Now what I want to do is to pass the id of the currently selected item in the listbox to bttn B's event.

Below is my code for displaying the names in the listbox.
C#
String connection;  
String sql = "select FileLoc, Lname from apps where dept = '" + cbdept.Text + "' and role = '" + cbrole.Text + "' and rating = '" + cbrating.Text + "' and yearsofexp = '" + exp.Text + "' and thirddeg = '"+edulev+"'";

SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

try
{
    conn.Open();
    reader = cmd.ExecuteReader();
    searchresults.Enabled = true;
                                               
    // ArrayList rslts = new ArrayList();
    while (reader.Read())
    {
    //    rslts.Add(new rslt(reader["Lname"].ToString(), reader["FileLoc"].ToString()));
                   
        searchresults.Items.Add(reader["Lname"].ToString());
        // searchresults.ValueMember = reader["FileLoc"].ToString();
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}
Posted
Updated 12-Nov-10 3:40am
v6
Comments
Dalek Dave 12-Nov-10 9:40am    
Edited for Grammar.

Normally the list would be on the same form as the button so it has access to the list and can retrieve the index itself:
C#
string curItem = listBox1.SelectedItem.ToString();
int index = listBox.SelectedIndex;


Otherwise declare a private variable for the selected item in your form. This private variable is only accessible for methods within your form and both event handlers are so they can access it and exchange data using that.

C#
public class Form1 : System.Windows.Forms.Form
{
  private int selectedIndex;
...
}


Good luck!
 
Share this answer
 
v2
Comments
[no name] 12-Nov-10 9:16am    
selected index pertains to the listbox but i want to select the id of the listbox item as it is in the database
E.F. Nijboer 12-Nov-10 12:27pm    
It would be way more efficient to link it directly the datasource to all the necessary data and even better... link it to the dataset.

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource%28VS.71%29.aspx

http://msdn.microsoft.com/en-us/library/w67sdsex.aspx
One way to resolve your problem would be to create a small class for your data and add instances of it to the ListBox.

Something like:
C#
public class SearchItem
{
  public string LastName {get; set;}
  public int ID {get; set;}

  public override string ToString()
  {
      return this.LastName;
  }
  ..........
  ..........
  ..........
}


Then to use this:
C#
SearchItem newItem = null;
while (reader.Read())
{
  newItem = new SearchItem();
  newItem.LastName = reader["Lname"].ToString();
  newItem.ID = reader["ID"].ToString();
  searchresults.Items.Add(newItem);
}


To access the Id:
C#
int testID = ((SearchItem)searchresults.Items[x]).ID;


Hope this helps. :)
 
Share this answer
 
Create a class for your search results with properties of the query results.
public class Applicant
{
public int ID { get; set; }
public string LName { get; set; }
//the rest of parameters
}

while reading, create new instances of Applicant, fill the parameters and insert these instances to the Listbox:
....
Applicant newApplicant;
using (SqlConnection conn)
{
...
using(SqlDatareader reader = cmd.ExecuteReader())
{
while (reader.Read())
    {
        newApplicant = new Applicant();
        newApplicant.ID = reader.GetInt32(0);
        newApplicant.LName = reader.GetString(1);
        ....
        ListBox.Items.Add(newApplicant);
    }
}
}

By this way, in the listbox you will have instances of Applicant. Under button b, you can get the selected id as:
Applicant selectedApplicant = ListBox1.SelectedItem as Applicant;
int requiredID = selectedApplicant.ID;


As two quick notes, I highly recommend using SqlParameter in sql queries instead of string addition and close any reasources after you are done (like connections, readers, etc)
 
Share this answer
 
v2
its very simple to pass value in c#..
U have to assign a value to the session variable in which u need to pass it..
syntax:

session["variable_name"]= passing_value;

then u have to retrieve the value from another button event..
lik..

local_variable= session["variable_name"].tostring();

try this one it will help u..

best of luck..

if u agreed the above answer click accept answer or put vote for this answer..
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Nov-10 9:13am    
Who's talking about ASP.NET? The original questions is more WinForm like (Code quote: "MessageBox.Show(...)").
[no name] 12-Nov-10 9:14am    
its a windows application, do session variables work in .net windows applications?
MCY 14-Nov-10 7:46am    
session variables do NOT work in .net windows applications

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