Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 16th, 2007, 01:47 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default Populate ListView From ComboBox Select

Hello All:

I am trying to perform a sql query once a selection is made in a combobox. I do not have an ok button or anything like that. I would just like to select the item in the drop down and it perform a lookup and populate the listview box. I am able to do a query and to populate the combobox. What the gist of this program is when I select an item in the combobox, it will list all the sets that the item belongs to in the listview box. 'Sets' is the only category in the Listview Box.

I am open for any easier ways to accomplish this. I can do one db lookup (which is probably better) than doing two lookups. I was just trying to use the Select Distinct command to populate the ComboBox with just one item name instead of duplicates.

Thanks all,
Tony
Here is a code snippet:
================================================== ====================
#region Item Lookup
private void ItemLookup()
{
    cbItem.Items.Clear();
    String connString = XXXLib.XXXLogon.XXXLogObj.GetConnectionString();
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();
    try
    {
        SqlCommand cmd = new SqlCommand("select distinct cin.name as Item " +
        "from catalogname as cin " + where cin.isitem = 0",conn);
        SqlDataReader dr = cmd.ExecuteReader();if ( null != dr )
        {
            while (dr.Read())
            {
                 cbItem.Items.Add(dr["Item"].ToString());
            }
            dr.Close();
         }
     }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
}
private void cbItem_DropDown(object sender, System.EventArgs e)
{
        cbItem.Items.Clear();
        lvSets.Items.Clear();
        string sItem = cbItem.SelectedValue.ToString();
        string connString = XXXLib.XXXLogon.XXLogObj.GetConnectionString();
        SqlConnection conn = new SqlConnection(connString);
        conn.Open();
    try
    {
        SqlCommand cmd = new SqlCommand("select distinct ocs.name as Set " +
        "from order as o " +
        "join catalogname as cin on o.catalogmasteritemguid = cin.masteritemguid and cin.name = "+ sItem + " " +
        "join catalogset as ocs on ocs.guid = o.setguid",conn);
        SqlDataReader dr = cmd.ExecuteReader();
        if ( null != dr )
        {
            while (dr.Read())
            {
                ListViewItem lvi = new ListViewItem(dr["Set"].ToString());
                lvOrdersets.Items.Add(lvi);
            }
            dr.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
}
    #endregion

 
Old August 20th, 2007, 12:54 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Tony,

I'm not sure what you are looking for, but here are a couple of suggestions.

1) consider using a Parameter object.
2) consider using a stored procedure on your SQL database.



What you don't know can hurt you!
 
Old August 20th, 2007, 04:26 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default

I figured it out today. I didn't have single quotes around my variable in my SQL statement.

I do have one question:

How do I tell it to display "Nothing Found" if nothing is found in the sql statement.


SqlCommand cmd = new SqlCommand("select distinct ocs.name as Set " +
        "from order as o " +
        "join catalogname as cin on o.catalogmasteritemguid = cin.masteritemguid and cin.name = '"+ sItem + "' " +
        "join catalogset as ocs on ocs.guid = o.setguid",conn);
        SqlDataReader dr = cmd.ExecuteReader();
        if ( null != dr )
        {
            while (dr.Read())
            {
                ListViewItem lvi = new ListViewItem(dr["Set"].ToString());
                lvOrdersets.Items.Add(lvi);
            }
            dr.Close();

==================================
Thanks,

 
Old August 20th, 2007, 04:32 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Check out the HasRows property of the Reader. From there, you can pop up a message, hide the ListView or do whatever it is you need to do to display some message.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 20th, 2007, 06:49 PM
Registered User
 
Join Date: Aug 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Definitely encapsulate that query logic in a stored procedure or at the very least parameterize that query in a separate layer.

http://www.willasrari.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding ComboBox inside ListView problem gunnjamie Windows Presentation Foundation 1 July 17th, 2008 02:13 PM
populate combobox in xsl mausumee XSLT 2 February 23rd, 2007 06:37 PM
Binding a comboBox to update a listView Amethyst1984 VB How-To 2 February 28th, 2006 03:07 PM
Populate Listview yves VB Databases Basics 1 February 6th, 2006 08:38 AM
Embed ListView in Combobox (Urgent) HemaChaudhry VB.NET 0 January 2nd, 2006 09:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.