Click here to Skip to main content
15,888,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm trying to pass the SelectedValue from my listBox to be used as a filter for my DataGridView. Both objects use my Azure SQL database to get their data.

C#
namespace myNameSpace
{
    public partial class CompanyForm : Form
    {
        public CompanyForm()
        {
            InitializeComponent();
            Load += new EventHandler(dataEmployees_Load);
            Load += new EventHandler(companyList_Load);
        }
        //Connection String
        string cs = ConfigurationManager.ConnectionStrings["MyCS"].ConnectionString;
        
        // Load all companies
        private void companyList_Load(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlCommand cmd;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            string sql = "Select companyName from dbo.Company";
            con = new SqlConnection(cs);
            try
            {
                con.Open();
                cmd = new SqlCommand(sql, con);
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);
                adapter.Dispose();
                cmd.Dispose();
                con.Close();
                companyList.DataSource = ds.Tables[0];
                companyList.ValueMember = "companyName";

            }
            catch (Exception ex)
            {
                MessageBox.Show("There are no companies to display");
            }
        }

The code above is the code to generate the listbox from my SQL Database

C#
// Load all employees
private void dataEmployees_Load(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(cs))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand
        (
            "Select fname,ename
             FROM dbo.Users u
             inner join dbo.Company c on c.companyName = u.company", con
        ); //I would like to add to the select clause the variable "selectedValue" that can work in my conditional logic to filter the data in the datagrid
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        dataEmployees.DataSource = dt;
    }
}


So a user creates a company with employees, then wants to select a company and only get the ones employed in that particular company in the DataGridView

What I have tried:

I have tried some different approaches, with trying to put it into a variable but then the data does not get loaded into the listbox or the DataGridView.
Posted
Updated 10-Jan-17 11:10am

1 solution

A simple way could be the following:

- Build the SQL statement without conditions
- If company is selected modify the SQL to contain the WHERE clause and add the parameter
- complete the command and execute

C#
SqlCommand cmd = new SqlCommand(); 
cmd.Connection = con;
string sql  = @"Select fname,ename 
                FROM dbo.Users u 
                inner join dbo.Company c on c.companyName = u.company"
if (companyList.SelectedIndex > -1) {
   sql = sql + " WHERE c.companyName = @companyname";
   cmd.Parameters.AddWithvalue("@companyname", companyList.SelectedItem);
}
cmd.CommandText = sql;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
...
 
Share this answer
 

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