Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 16th, 2007, 01:47 PM
Authorized User
Points: 336, Level: 6
Points: 336, Level: 6 Points: 336, Level: 6 Points: 336, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2006
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old August 20th, 2007, 12:54 PM
Authorized User
 
Join Date: Nov 2006
Location: Valparaiso, IN, USA.
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!
Reply With Quote
  #3 (permalink)  
Old August 20th, 2007, 04:26 PM
Authorized User
Points: 336, Level: 6
Points: 336, Level: 6 Points: 336, Level: 6 Points: 336, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2006
Location: , , .
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,

Reply With Quote
  #4 (permalink)  
Old August 20th, 2007, 04:32 PM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
  #5 (permalink)  
Old August 20th, 2007, 06:49 PM
Registered User
 
Join Date: Aug 2007
Location: Bellingham, WA, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.