1

I'm fairly new to coding so am probably missing something obvious. I've tried searching here and various other resources but can't get my code to do what I want so I'm hoping someone can help.

I have a checkbox list which is populated from a SQL query based on user input. My intention is that a user can check one or more items on the list and a SQL stored Procedure will be run for each checked item with the value of the selected item passed as a parameter value. So, if there are six items in the list and three are checked my code will loop through the list and for each checked item will run the SP with the item's value as the parameter value before moving on to the next item in the list.

As a first step in testing the logic I have the code below which is supposed to pass a selected item's text to a label when a button is clicked, however rather than the checked box's text value I am just getting 'System.Data.DataRowView'

int checkCount = chckList.Items.Count;
for (int i = 0; i < checkCount; i++)
{
    if (chckList.GetItemChecked(i))

    {
        String str = chckList.Items[i].ToString();
        label23.Text =  str;
    }
}

If I change the assignment of the String str value to 'String str = chckList.GetItemText(i).ToString();' I just get the number of the item (i.e. if there are six values and the fist is checked then I get '0', if the second is checked I get '1', etc.)

The CheckBoxList is, as I've said, filled from a SQL query based on user input, my method for doing this is:

connection.Open();
        using (SqlDataAdapter adapter = new SqlDataAdapter())
        {
            DataTable datTbl = new DataTable();
            adapter.SelectCommand = myCommand;
                {
                adapter.Fill(datTbl);
                chckList.DataSource = datTbl;
                chckList.DisplayMember = datTbl.Columns[1].ColumnName;

                }
        }
connection.Close();

Can anyone help? What am I doing wrong?

Thanks

1 Answer 1

1

If your Items are a collection of DataRowView objects, then this just gets a specific DataRowView and calls ToString() on it, which returns the fully qualified name of the object:

String str = chckList.Items[i].ToString();

Try accessing a specific column:

String str = ((DataRowView)chckList.Items[i])["SomeColumnName"].ToString();
Sign up to request clarification or add additional context in comments.

Comments

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.