How do you get the Id of the item in CheckListBox

RedRocket

I have a checkListBox in my windows form application and binded with my database data with SQL. I populate a list of item according to what I select from my comboBox. If I select item 1 from my comboBox, it will display customers' name who is using this item. I want get customers' id according to what I check in my checkListBox.

Here is how I fill my comboBox

 private void fill_comboBox()
        {
            myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
            try
            {
                string query = "select itemId, itemName from item_detail";
                SqlDataAdapter da = new SqlDataAdapter();
                myConn.Open();
                DataTable dt = new DataTable();

                SqlCommand command = new SqlCommand(query, myConn);

                SqlDataReader reader = command.ExecuteReader();

                dt.Load(reader);

                DataRow dr;
                dr= dt.NewRow();
                dr.ItemArray = new object[] { 0, "<----------Select an item-------> " };
                dt.Rows.InsertAt(dr, 0);

                itemComboBox.DataSource = dr;
                itemComboBox.ValueMember = "itemId";
                itemComboBox.DisplayMember = "itemName";

                fill_customerCheckListBox();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Here is how I fill my checkListBox

private void fill_customerCheckListBox()
{
    myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");

    try
    {
        myConn.Open();

        DataSet myDataSet = new DataSet();
        myDataSet.CaseSensitive = true;

        string commandString = "select fullName from[customer_detail] as cd LEFT JOIN[item_customer] as ic ON ic.customerId= cd.customerI WHERE ic.itemId= '" + itemCombBox.SelectedValue + "' ";

        myCommand = new SqlCommand();

        myCommand.Connection = myConn;

        myCommand.CommandText = commandString;

        SqlDataAdapter myDataAdapter = new SqlDataAdapter();

        myDataAdapter.SelectCommand = myCommand;

        myDataAdapter.TableMappings.Add("Table", "customer_detail");

        myDataAdapter.Fill(myDataSet);

        DataTable myDataTable = myDataSet.Tables[0];

        customerCB.Items.Clear();

        string s = "Select All Customer";

        customer.Items.Add(s);

        foreach (DataRow dataRow in myDataTable.Rows)
        {

            customerCB.Items.Add(dataRow["fullName"]);
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I have a praremter for loop that trying to loop through every checked item from my customerCB, but this isn't working. When I am in my debug mode, the code DataRow row = (itemChecked as DataRowView).Row; is causing an exception

Object reference is not set to an instance of an object.

I have no idea how to fix this. Help will be appreciated, thanks

 string parameterList = ""; 



foreach (object itemChecked in customerCB.CheckedItems) 
{ 
     DataRow row = (itemChecked as DataRowView).Row; 
     parameterList += (parameterList.Length == 0) ? row[0].ToString() : ", " + row[0].ToString();
}
The Blue Shirt Developer

Try the Following way !!

foreach(object itemChecked in checkedListBox1.CheckedItems)
    {
         DataRowView castedItem = itemChecked as DataRowView;
         string fullName= castedItem["fullName"];

    }

(OR) You would need to cast or parse the items to their strongly typed equivalents, or use the System.Data.DataSetExtensions namespace to use the DataRowExtensions.Field method demonstrated below:

foreach (var item in checkedListBox1.CheckedItems)
{
    var row = (itemChecked as DataRowView).Row;
    int id = row.Field<int>("ID");
    string name = row.Field<string>("fullName");

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the key(id) of the binded checkListBox

From Dev

How to get the value of the checkListBox

From Dev

How to get the value of the checkListBox

From Dev

How do I get the id of a list item?

From Dev

How do you get an integer number for an item in group in SQL?

From Dev

How do you get an integer number for an item in group in SQL?

From Dev

In ruby, how do you write item.nil? ? nil : item.id.to_s

From Dev

How do I get the ID of a JQuery item mid-function?

From Dev

How Do I Get ID From Item In DropDownList?

From Dev

how do you get window ID for xdotool automatically

From Dev

How do you get the Respondent ID from a Survey Monkey survey?

From Dev

How do you GET the id using mysqli_stmt_fetch()

From Dev

How to get the checked items string in a CheckListBox

From Dev

How to get item with max id?

From Dev

How to get the item ID in vaadin

From Dev

how do you get follower ID's of people who are not following you Twitter API

From Dev

How do you add an item to a TStackPanel at runtime

From Dev

How do you keyboard focus on a specific item?

From Dev

Angularjs - in nested ng-repeaters, how do you get index of outher repeater item within nested repeater

From Dev

How do you get the drawer to slide in after clicking a menu item in Aurelia UI

From Dev

How do you get dependencies?

From Dev

How do you get the "this" of a function?

From Dev

How do you place an item behind another item in android?

From Dev

How to you print the index or get the item of a item in an ArrayList

From Dev

How do i get item by ID from a loop of Rows.count

From Dev

How do i get id of autocomplete textview out of two autocomplete textview on item selected from its list?

From Dev

How do i get item by ID from a loop of Rows.count

From Dev

Programmatically set checked item in CheckListBox

From Dev

How to get id of selected table item in php

Related Related

  1. 1

    How to get the key(id) of the binded checkListBox

  2. 2

    How to get the value of the checkListBox

  3. 3

    How to get the value of the checkListBox

  4. 4

    How do I get the id of a list item?

  5. 5

    How do you get an integer number for an item in group in SQL?

  6. 6

    How do you get an integer number for an item in group in SQL?

  7. 7

    In ruby, how do you write item.nil? ? nil : item.id.to_s

  8. 8

    How do I get the ID of a JQuery item mid-function?

  9. 9

    How Do I Get ID From Item In DropDownList?

  10. 10

    how do you get window ID for xdotool automatically

  11. 11

    How do you get the Respondent ID from a Survey Monkey survey?

  12. 12

    How do you GET the id using mysqli_stmt_fetch()

  13. 13

    How to get the checked items string in a CheckListBox

  14. 14

    How to get item with max id?

  15. 15

    How to get the item ID in vaadin

  16. 16

    how do you get follower ID's of people who are not following you Twitter API

  17. 17

    How do you add an item to a TStackPanel at runtime

  18. 18

    How do you keyboard focus on a specific item?

  19. 19

    Angularjs - in nested ng-repeaters, how do you get index of outher repeater item within nested repeater

  20. 20

    How do you get the drawer to slide in after clicking a menu item in Aurelia UI

  21. 21

    How do you get dependencies?

  22. 22

    How do you get the "this" of a function?

  23. 23

    How do you place an item behind another item in android?

  24. 24

    How to you print the index or get the item of a item in an ArrayList

  25. 25

    How do i get item by ID from a loop of Rows.count

  26. 26

    How do i get id of autocomplete textview out of two autocomplete textview on item selected from its list?

  27. 27

    How do i get item by ID from a loop of Rows.count

  28. 28

    Programmatically set checked item in CheckListBox

  29. 29

    How to get id of selected table item in php

HotTag

Archive