6

I am sure that someone has asked this before, but I am not searching for the right terms or something because I just can't seem to find what I am looking for.

I have a class Packages:

class Packages
{
    string PackageID {get; set;}
    string PackageName { get; set; }
}

I have a list of packages in my context.

public class TransferJobContext
{
    public string ImportFile;
    public string WorkSheetName;
    public DataSet TransferData;
    public List<Packages> Packages = new List<Packages>();
}

I have a checkedListBox on my form that is bound to a datasource. Here is the section of code where I get the values into the checkedListBox

using (var connection = my_DB.GetConnection())
{
    try
    {
        connection.Open();
        SqlDataReader rdr = null;
        dt = new DataTable();
        string CommandText = "SELECT ID, Name FROM TABLENAME WHERE UPPER(Import_File_Source) LIKE '%abc%' and STATUS = 1";

        SqlCommand cmd = new System.Data.SqlClient.SqlCommand(CommandText, connection);

        rdr = cmd.ExecuteReader();
        dt.Load(rdr);
        cbPackages.DataSource = dt;
        cbPackages.ValueMember = "ID";
        cbPackages.DisplayMember = "Name";

    }
    catch (Exception E)
    {
        MessageBox.Show(E.Message.ToString());
    }
    connection.Close();
}

How do I add a new Package to the list when an item is checked using the value member and displaymember of the selected items?

Edit: Ok maybe I am going about this completely wrong. Instead of telling you what I have done, I'll tell you what I would like to happen.

I am presenting my user with a checkedlistbox that has a list of names with check boxes next to them. They can select more than one. This results in me having one or more ID(s) to use in my query and name(s) to use as a description. I need to pass around with my context the one or many id/name combinations of my "Package".

What is the best what to capture the ID/Name combination of the selections the user made and pass them into my context?

8
  • 1
    Winforms? Web forms? Mvc? Commented Aug 2, 2013 at 11:10
  • @TheSolution I tagged winforms due to use of CheckedListBox Commented Aug 2, 2013 at 11:10
  • Yes Winforms, sorry I forget to add that - new to C# :) Commented Aug 2, 2013 at 11:15
  • Leslie, have you considered using properties rather than public members? Commented Aug 2, 2013 at 11:16
  • 1
    Can you show how you bind data to CheckedListBox? It does not have value and display members Commented Aug 2, 2013 at 11:18

1 Answer 1

1

First: Fetch existing Packages from your database:

public function GetPackages() as List<Package>
{
 using (var connection = my_DB.GetConnection())
    {
        try
        {
            connection.Open();
            SqlDataReader rdr = null;
            dt = new DataTable();
            string CommandText = "SELECT ID, Name FROM TABLENAME WHERE UPPER(Import_File_Source) LIKE '%abc%' and STATUS = 1";

            SqlCommand cmd = new System.Data.SqlClient.SqlCommand(CommandText, connection);

             var packages = new List<Package>();
             using(var reader = cmd.ExecuteReader())
             {
             do while(reader.Read())
               {
               packages.Add(new Package({ID = reader.GetString(0), Name = reader.GetString(1)})
               }
             }

            cbPackages.DataSource = packages;
            cbPackages.ValueMember = "ID";
            cbPackages.DisplayMember = "Name";
            return packages;
        }
        catch (Exception E)
        {
            MessageBox.Show(E.Message.ToString());
            return new List<Package>();
        }
        connection.Close();
    }

}

Second: Assign the fetched packages to the instance of your context:

yourContext.Packages = GetPackages();

Third: Fill the CheckedListBox with your packages:

cbPackages.DataSource = yourContext.Packages;
cbPackages.ValueMember = "ID";
cbPackages.DisplayMember = "Name";

Finally: After user input, get the checkedpackages and do whatever you want with them:

foreach (Package  item in checkedListBox1.CheckedItems)
{
     MessageBox.Show(Package.ID);
     MessageBox.Show(Package.Name);
     //do whatever you want here e.g. pass it back to the context
}

Sidenote: I would suggest you to rename Packages to Package, PackageID to ID and PackageName to Name (As already proposed in the example code).

Sign up to request clarification or add additional context in comments.

6 Comments

when exactly have I assigned the List to my checkedlistbox control? I don't think I have done anything like that??
@Leslie also showed how you can set the datasource accordingly. ;)
It looks like you are creating a new list - I already have the List in my context, I don't want to create a new list. And I don't want to add all the ID/Name combinations to the list, I only want to add the ones they selected
@Leslie I changed my answer a bit. Hope it helps you better now. If it doesn't, I would be glad if you could post more code about how you handle your TransferJobContext.
I apologize for the delay in getting back to this. Thank you for your help but I don't want my context to have all the package combinations, just the selected ones. I will see if I can modify what you have shown to accomplish that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.