Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I would like to apply the following exception to my script..

VB.NET
ListBoxKlanten.Items.Count == 0


The idea is that when the listbox "ListBoxKlanten.Items.Count == 0" that the "listBoxKlantenContact" is made blank. (it is possible that there ready records in "listBoxKlantenContact") How do I apply this exception and what code can I best use for this option


C#
private void KlantenContactBOX()
{

    string queryKLantID = "SELECT IDKlantContactpersonen, FK_ID_Klant, Naam From Klantencontact WHERE FK_ID_Klant = @SELKlantID";

    using (connection = new SqlConnection(connectionstring))
    using (SqlCommand command = new SqlCommand(queryKLantID, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))

    {
        command.Parameters.AddWithValue("@SELKlantID", ListBoxKlanten.SelectedValue);

        DataTable KlantenContactlijst = new DataTable();
        adapter.Fill(KlantenContactlijst);

            listBoxKlantenContact.DisplayMember = "Naam";
            listBoxKlantenContact.ValueMember = "IDKlantContactpersonen";
            listBoxKlantenContact.DataSource = KlantenContactlijst;
   }
}
Posted
Comments
phil.o 23-Nov-15 5:19am    
There is no such thing as 'applying an exception'. Why don't you just test for the Count property, and act accordingly to the result?
MaikelO1 23-Nov-15 6:03am    
Okay, :( I came upon this solution because the two list boxes are linked.

I use the list as parameter ListBoxKlanten.SelectedValue, but the problem is that the SelectedValue hold the last known value. When someone mistyped in the filter of the and there are not results the ListBoxKlanten.SelectedValue hold the last known result. But actually he should not run this query.

How do I fix than this??
phil.o 23-Nov-15 6:10am    
That is the perfect job for a debugging session: this way you will be able to check the value of your variables, and react accordingly.
Sinisa Hajnal 23-Nov-15 6:15am    
React to https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx
selected index changed. That way, if the item is not changed, nothing will change the original.

By your description, the system works as intended, if the user didn't select anything, old value stands.
Sinisa Hajnal 23-Nov-15 6:09am    
Please rephrase your question. It is incomprehensible. If you just want to check if you list is empty, use the code from the start of your question, just add "if" in front of it

1 solution

Problem solved, Thank you for your help!!

if (ListBoxKlanten.Items.Count == 0)
               {
                   command.Parameters.AddWithValue("@SELKlantcontactinfoID", DBNull.Value);
               }
               else
               {
                   command.Parameters.AddWithValue("@SELKlantcontactinfoID", listBoxKlantenContact.SelectedValue);
               }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900